2012-12-03 25 views
1

这是可能创建一个GIF图像与各种文字字体,风格,格式和对齐在PHP中的背景图像?如何使用不同的文本格式在php中使用背景图像创建gif图像?

比如我有一个像 enter image description here

背景图像和图像这是我想要插入背景图片是

enter image description here

和输出GIF图像看起来应该像 enter image description here

我试过这个代码

<?php 
// Create a new image instance 
$im = imagecreatetruecolor(300, 250); 
$img = imagecreatefrompng('back.png'); 
// Make the background white 
imagefilledrectangle($im, 0, 0, 300, 250, 0xEFEFEF); 

// Draw a text string on the image 

imagestring($im, 20,20, 40, 'Heading1', 0x000000); 
imagecopymerge($img, $im, 0, 0, 0, 0, 300, 250, 70); 
// Output the image to browser 
header('Content-Type: image/gif'); 

imagegif($img,'test.gif'); 
imagedestroy($im); 
?> 

但不幸的是,它不会提供所需的输出,我想添加更多的内容到背景图像,如我在输出图像中所示。

回答

0

这种设计:

image

您可以使用此代码:

<?php 
// Create a new image instance 
$img = imagecreatefrompng('back.png'); 

// Draw a text string on the image 
// imagestring (resource $image , int $font , int $x , int $y , string $string , int $color) 
imagestring($img, 5, 20, 30, 'Heading1', 0x000000); 
imagestring($img, 3, 180, 30, 'Heading 2 test desc', 0x000000); 

imagestring($img, 3, 20, 80, 'Text info 3', 0xFF0000); //red 
imagestring($img, 1, 180, 80, 'Demonstration text', 0x000000); 

imagestring($img, 5, 400, 170, 'Price here', 0xFF0000); //red 

$im = imagecreatefrompng('plane.png'); //load pic 2 
imagecopymerge($img, $im, 0, 130, 0, 0, 165, 71, 70); 
imagedestroy($im); 

// Output the image to browser 
header('Content-Type: image/gif'); 
imagegif($img); 
imagedestroy($img); 
?> 

imagestring()功能仅限于内置的,而丑陋的字体,仅为大小选项1到5.您可以使用另一种TTF字体功能,如下所述。

要更改字体,您需要提供自己的TTF格式字体,将它们放在与脚本相同的文件夹中,而不是将imagestring()函数替换为该字体。这些参数非常相似,并且您自己替换该代码应该是微不足道的。这里是解释如何使用imagettftext()功能的链接。

http://php.net/manual/en/function.imagettftext.php