2016-07-18 5 views
0

Я пытаюсь создать рамку с фотографиями, где будет светло-красный оттенок и выравниваемый по тексту центр. Я беру фактический размер изображений, а затем пытаюсь создать новое изображение программно с рамкой. Ниже приведен пример, который я сделал до сих пор:Образец фоторамки - светло-красный оттенок и выравнивание текста

enter image description here

В изображении, есть белая граница, которую я использовал для демонстрационных целей. Я хочу, чтобы светло-красный оттенок не проходил над белой рамкой, и текст должен быть точно посередине для каждого изображения. Когда я работаю с фактическим размером изображения, тень и текст не остаются в определенном положении. На некоторых изображениях светло-красный оттенок и текст не отображаются. Я сделал следующий код, чтобы заставить его работать, но кажется, что что-то отсутствует:

$size = getimagesize($file_tmp); //Gets the image file size  
$width = $size[0];  
$height = $size[1]; 

$images_orig = imagecreatefromjpeg($file_tmp); //Creates jpeg image from tmp file location  
$photoX = imagesx($images_orig);  
$photoY = imagesy($images_orig);   
$images_fin = imageCreateTrueColor($width, $height); 
$color = imagecolorallocate($images_fin, 255, 255, 255); 
$lightRed = imagecolorallocatealpha($images_fin, 255, 0, 0, 100); 
$black = imagecolorallocate($images_fin, 0, 0, 0);   
$text = 'I am from China';  
$font = 'MyriadPro-Light.ttf'; 

$positions_redline = ($height/4)*3; 

ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width, $height, $photoX, $photoY);  
Imagefilledrectangle($images_fin, 0, $positions_redline, $width, $height, $lightRed);  

$font_size = 10;   

/*Starts - This is what I tried to fit the text into the image specifically in the center*/ 
$bbox = imagettfbbox($font_size, 0, $font, $text); 
$tbwidth = $bbox[2]; 
$factor = round(($width/$tbwidth), 0, PHP_ROUND_HALF_DOWN); 
$newFontSize = $font_size * $factor; 
/*Finishes - This is what I tried to fit the text into the image specifically in the center*/ 


print_r($bbox[2]); 
print_r("<br/>".$factor);   

// Get your Text Width and Height 
$text_width = $bbox[2] - $bbox[6]; 
$text_height = $bbox[3] - $bbox[7]; 
// Calculate coordinates of the text  

$x = ($photoX/2) - ($text_width/2); 
$y = (($height - $positions_redline)/2) - ($text_height/2); 

Imagettftext($images_fin, $newFontSize, 0, $x, $y + $positions_redline, $color, $font , $text); //Trying to write text and align it center here 
$new_images = 'testImageResult.jpg'; 
ImageJPEG($images_fin,"Images/".$new_images); 
+0

ли у попытаться мой источник? xD –

+0

Светло-красный оттенок отлично работает, но шрифты отображаются неправильно. Я использую два примера, где размер шрифта не одинаковый: i) https://postimg.org/image/ns3jqgpoh/ ii) https://postimg.org/image/3k7t49wkh/. Есть ли способ сделать размер шрифта таким же или выглядеть соответствующим образом и размер? Кстати, знаете ли вы, как добавлять приложения в facebook? Если возможно, дайте мне знать. Благодарю. –

+0

Эта функция imagettfbbox на PHP не работает, поэтому вы получаете эту проблему. Выберите один лучший шрифт, чтобы у вас был лучший результат. С App Facebook? Вы можете прочитать больше Grap Facebook API –

ответ

0

Попробуйте этот источник

$file_tmp = '/var/www/html/testGuzzle/testImage.jpg'; 
    $size = getimagesize($file_tmp); //Gets the image file size  
    $width = $size[0];  
    $height = $size[1]; 

    $images_orig = imagecreatefromjpeg($file_tmp); //Creates jpeg image from tmp file location  
    $photoX = imagesx($images_orig);  
    $photoY = imagesy($images_orig);   
    $images_fin = ImageCreateTrueColor($width, $height); 
    $color = imagecolorallocate($images_fin, 255, 255, 255); 
    $lightRed = imagecolorallocatealpha($images_fin, 255, 0, 0, 100); 
    $black = imagecolorallocate($images_fin, 0, 0, 0);   
    $text = 'Im from VietNam';  
    $font = '/var/www/html/testGuzzle/OpenSans-SemiboldItalic.ttf'; 

    $positions_redline = ($height/4)*3; 

    ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width, $height, $photoX, $photoY);  
    Imagefilledrectangle($images_fin, 0, $positions_redline, $width, $height, $lightRed);  

    $font_size = 20; 
    $bbox = imagettfbbox($font_size, 0, $font, $text); 
    print_r($bbox); 
    // Get your Text Width and Height 
    $text_width = $bbox[2]-$bbox[0]; 
    $text_height = $bbox[7]-$bbox[1]; 
    // Calculate coordinates of the text 

    $x = ($photoX/2) - ($text_width/2); 
    $y = ($height-$positions_redline)/2) - ($text_height/2); 

    Imagettftext($images_fin, 40, 0, $x, $y+$positions_redline, $color, $font , $text); //Trying to write text and align it center here 
    $new_images = 'testImageResult.jpg'; 
    ImageJPEG($images_fin,"/var/www/html/testGuzzle/".$new_images); 

 Смежные вопросы

  • Нет связанных вопросов^_^