2016-07-18 34 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

有U试试我的来源? xD –

+0

浅红色阴影效果很好,但字体显示不正确。我分享两个字体大小不一样的样本:i)https://postimg.org/image/ns3jqgpoh/ ii)https://postimg.org/image/3k7t49wkh/。有什么方法可以使字体大小相同或以适当的方式和大小查找?顺便说一下,你有什么想法如何在Facebook中添加应用程序?如果可能的话,让我知道。谢谢。 –

+0

PHP中的这个函数imagettfbbox工作不正常,所以你得到这个问题。请选择一种最佳字体,以便获得最佳效果。随着应用程序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);