我在test.php文件中使用以下代码从文本生成图像。
<?php
error_reporting(E_ALL);
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$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);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = '/home/axxxxxxx/public_html/font.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>然后,我尝试在test2.php中显示图像,如下所示
<?php
echo "<img src=\"/test.php\" />";
?>所有我得到的都是默认的破碎图像图标。字体文件和图像url的路径正确。所有文件权限均为777。服务器上确实有GD库。
我可能做错了什么?
发布于 2013-06-12 22:02:00
找到答案了。按照Danak的建议,我使用notepad++将文件保存为UTF-8 Without BOM。然后简单地开始正确地显示图像。
发布于 2013-06-09 20:18:52
这是由于缺少字体造成的。请复制test.php目录下的字体文件并更改代码:
$font = '/home/axxxxxxx/public_html/font.ttf';至
$font = 'font.ttf';希望能有所帮助。
发布于 2015-01-23 23:50:33
我的问题是一个错误的补偿。图像没有显示任何内容,没有文本,源代码中没有错误,只显示了一个空白文件。路径是正确的。我原以为ttf字体有误,但结果是定位错误。
以下是帮助我看了一些文本的内容:
imagettftext($im, 20, 0, 20, 20, $fg, $font, $text);这在右上角显示了一些文本。
完整的工作代码:
putenv('GDFONTPATH=' . dirname(__FILE__));
$font = 'arial'; // located next to the script
imagettftext($im, 20, 0, 20, 20, $fg, $font, $text);https://stackoverflow.com/questions/17009467
复制相似问题