相框样本 - 浅红色阴影和文本对齐中心

时间:2016-07-18 13:08:17

标签: php gd

我正在尝试创建一个相框,其中将有一个浅红色阴影和一个文本对齐的中心。我正在拍摄图像的实际大小,然后尝试使用框架以编程方式创建新图像。以下是我到目前为止所做的一个示例:

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);  

1 个答案:

答案 0 :(得分: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);