PHP “imagettftext” 返回null图像(不工作)
问题描述:
我已经测试上的LocalServer和在线网络服务器...但返回一个空的图片PHP “imagettftext” 返回null图像(不工作)
检查返回结果... http://s8.postimage.org/sthztzj5x/null_image.jpg
<?php
Set the content-type
header('Content-Type: image/png');
$im = imagecreatetruecolor(400, 30);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = 'Testing...';
$font = 'arial.ttf';
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im);
?>
答
这正确地为我工作。
http://www.laprbass.com/RAY_temp_rameez.php
<?php // RAY_temp_rameez.php
error_reporting(E_ALL);
// Set the content-type
header('Content-Type: image/png');
$im = imagecreatetruecolor(400, 30);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = 'Testing...';
$font = 'fonts/verdanaz.ttf';
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im);
答
此脚本的第二行会导致PHP解析错误。双斜杠添加到它的面前是这样的:
//设置内容类型
你不检查那些*返回值的任何*。其中一项功能可能失败。注释掉'header'调用,添加'error_reporting(-1); ini_set('display_errors',true);'然后重试。我敢打赌,你会看到一个巨大的PHP错误,警告或通知。 – Charles
您需要在PHP中启用错误日志记录(记录非常重要,因为图像不会向您显示文本)。将报告设置为最高级别。然后按照错误日志,它会告诉你错误在哪里。另请参见[我如何使用打印调试PHP图像?](http://stackoverflow.com/q/11900852/367456)和[PHP无效图像和错误处理](http://stackoverflow.com/q/2574713/367456) – hakre