可以一起使用 PHP GD 的 imagecreatefromgif() 和 imagettftext() 吗?
基本上我正在寻找一种在 GIF 上编写动态文本的方法 [最好通过 PHP GD]。但我似乎无法让这两个函数很好地发挥作用。
供参考: imagecreatefromgif & imagettftext
function load_gif($imgname)
{
$im = @imagecreatefromgif($imgname);
if(!$im)
{
$im = imagecreatetruecolor(150,30);
$bgc = imagecolorallocate($im,255,255,255);
$tc = imagecolorallocate($im,0,0,0);
imagefilledrectangle($im,0,0,150,30,$bgc);
imagestring($im,1,5,5,'Error loading ' . $imgname,$tc);
}
return $im;
}
if ($_GET['color'] == 'red')
{
header('Content-Type:image/gif');
//$img = imagecreatetruecolor(51,32); // THIS IS NEEDED FOR TEXT TO SHOW
$img = load_gif('map-bubble-' . $_GET['color'] . '.gif');
$black = imagecolorallocate($img,0,0,0);
$white = imagecolorallocate($img,255,255,255);
imagefilledrectangle($img,0,0,51,32,$black);
imagettftext($img,14,0,0,0,$white,'../arial.ttf','test');
imagegif($img);
imagedestroy($img);
}
Basically I am looking for a way to write dynamic text on top of a GIF [preferably via PHP GD.] But I can't seem to get these two functions to play nice.
For reference: imagecreatefromgif & imagettftext
function load_gif($imgname)
{
$im = @imagecreatefromgif($imgname);
if(!$im)
{
$im = imagecreatetruecolor(150,30);
$bgc = imagecolorallocate($im,255,255,255);
$tc = imagecolorallocate($im,0,0,0);
imagefilledrectangle($im,0,0,150,30,$bgc);
imagestring($im,1,5,5,'Error loading ' . $imgname,$tc);
}
return $im;
}
if ($_GET['color'] == 'red')
{
header('Content-Type:image/gif');
//$img = imagecreatetruecolor(51,32); // THIS IS NEEDED FOR TEXT TO SHOW
$img = load_gif('map-bubble-' . $_GET['color'] . '.gif');
$black = imagecolorallocate($img,0,0,0);
$white = imagecolorallocate($img,255,255,255);
imagefilledrectangle($img,0,0,51,32,$black);
imagettftext($img,14,0,0,0,$white,'../arial.ttf','test');
imagegif($img);
imagedestroy($img);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您创建一个新的真彩色图像,将 gif 导入其中,添加文本,然后再次将结果输出为 gif,那么您可能会更幸运。
What if you create a new truecolour image, import the gif into that, add the text, then output the result again as a gif - you may have more luck that way.
只需将 imagecreatefromgif 存储到变量中,运行 imagettftext,然后使用内容类型标头输出图像即可。查看参数,并使用 imagecreatefromgif 的输出填充 $image 参数。
Simply store the imagecreatefromgif into a variable, run imagettftext, then output the image using an content-type header. Take a look at the arguments, and fill in the $image argument with the outptu from imagecreatefromgif.
这就是我最终所做的:
Here's what I ended up doing: