首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >imagettftext不显示图像

imagettftext不显示图像
EN

Stack Overflow用户
提问于 2013-06-09 20:11:55
回答 3查看 1.9K关注 0票数 0

我在test.php文件中使用以下代码从文本生成图像。

代码语言:javascript
复制
<?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中显示图像,如下所示

代码语言:javascript
复制
<?php
echo "<img src=\"/test.php\" />";
?>

所有我得到的都是默认的破碎图像图标。字体文件和图像url的路径正确。所有文件权限均为777。服务器上确实有GD库。

我可能做错了什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-06-12 22:02:00

找到答案了。按照Danak的建议,我使用notepad++将文件保存为UTF-8 Without BOM。然后简单地开始正确地显示图像。

票数 0
EN

Stack Overflow用户

发布于 2013-06-09 20:18:52

这是由于缺少字体造成的。请复制test.php目录下的字体文件并更改代码:

代码语言:javascript
复制
$font = '/home/axxxxxxx/public_html/font.ttf';

代码语言:javascript
复制
$font = 'font.ttf';

希望能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2015-01-23 23:50:33

我的问题是一个错误的补偿。图像没有显示任何内容,没有文本,源代码中没有错误,只显示了一个空白文件。路径是正确的。我原以为ttf字体有误,但结果是定位错误。

以下是帮助我看了一些文本的内容:

代码语言:javascript
复制
imagettftext($im, 20, 0, 20, 20, $fg, $font, $text);

这在右上角显示了一些文本。

完整的工作代码:

代码语言:javascript
复制
    putenv('GDFONTPATH=' . dirname(__FILE__));
    $font = 'arial'; // located next to the script
    imagettftext($im, 20, 0, 20, 20, $fg, $font, $text);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17009467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档