1

这是否可以在 php 中创建具有各种文本字体、样式、格式和对齐方式的背景图像的 gif 图像?

例如我有一个背景图片 在此处输入图像描述

我想在背景图像上插入的图像是

在此处输入图像描述

输出的 gif 图像应该看起来像 在此处输入图像描述

我为此尝试了代码

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

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

4

1 回答 1

0

对于这个设计:

图片

您可以使用以下代码:

<?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()-function的链接。

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

于 2012-12-03T09:02:36.467 回答