我有一台Ubuntu服务器和PHP5,以及PHP脚本文件,所有输出都是UTF-8格式的。我正在尝试将图像发送到输出流,但输出中只显示乱码的中文字符:
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
有什么建议吗?
发布于 2009-08-25 18:43:39
你的代码在我的机器上运行得很好:
<?php
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
die;
?>您确定您没有在该代码之前或之后输出任何吗?即使是任何类型的空格都会成为麻烦的来源。
或者你的脚本正在做其他的事情呢?
如果它仍然不起作用,也许可以尝试使用imagettftext,使用比imagestring使用的字体“更好”/更完整的字体可能会有所帮助?
例如,使用这样的东西:
$font = '/usr/share/fonts/truetype/msttcorefonts/arial.ttf';
imagettftext($im, 20, 0, 10, 20, $text_color, $font, 'A Simple éléphant String');顺便说一句,你有没有试过没有那些台词:
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);如果出现错误/警告/通知,删除这些行可能会帮助您查看这些行。
顺便说一句:对包含某些文本的图像使用JPEG通常不会得到很好的结果,因为JPEG是一种破坏性压缩机制。在这种情况下,使用PNG可能会得到更好的结果;-)
发布于 2009-08-25 18:41:47
尝试删除UTF-8字节顺序标记,因为它会优先于JPEG图像的内容,从而使其无效。
https://stackoverflow.com/questions/1330138
复制相似问题