将imagettftext()与'&'一起使用时出错在文本中

时间:2016-05-20 04:31:49

标签: php html-entities imagettftext

我使用下面的代码来编写文本并更改颜色,大小,字体并将其添加到图像上。

<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(400, 30);
$color1 = imagecolorallocate($im, $rgb1[0], $rgb1[1], $rgb1[2]);

$text = 'Testing...'; // Working fine
imagettftext($im, 20, 0, 10, 20, $color1 , "arial.ttf", $text);

$newtext = 'Testing & Demo'; // Not Working
imagettftext($im, 20, 0, 10, 20, $color1 , "arial.ttf", $newtext);

imagepng($im);
imagedestroy($im);
?>

输出错误:

test &amp; Demo

enter image description here

正确输出

test & Demo

我已经尝试htmlentities(),trim(),urlencode(),但没有获得欲望输出。

请帮我解决一下。提前谢谢!

1 个答案:

答案 0 :(得分:0)

我能看到的唯一问题(除了在图像中使用htmlentities())是你实际上传递了一个字符串而不是一个整数。这对我来说很好......

<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(400, 60);
$colour = imagecolorallocate($im, 255, 255, 255);
$font = '/path/to/font.ttf';

$text = 'Testing...';
imagettftext($im, 20, 0, 10, 20, $colour, $font, $text);

$newtext = 'Testing & Demo';
imagettftext($im, 20, 0, 10, 40, $colour, $font, $newtext);

imagepng($im);
imagedestroy($im);

enter image description here