首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法显示图像,因为它包含php gd中的错误。

无法显示图像,因为它包含php gd中的错误。
EN

Stack Overflow用户
提问于 2012-12-26 05:22:44
回答 3查看 9.5K关注 0票数 4

这是我创建映像所遵循的链接,但是我的GD在使用第一个示例进行测试时安装和工作正常,但是这个代码中断并标记为“图像无法显示,因为它包含错误”。

代码-

代码语言:javascript
运行
复制
<?php
//phpinfo();

//Report any errors
ini_set("display_errors", "1");
error_reporting(E_ALL); 

//Set the content type
header('content-type: image/png');

//Create our basic image stream 300x300 pixels
$image = imagecreate(300, 300);
//$image = imagecreatetruecolor(300, 300);
//Set up some colors, use a dark gray as the background color
$dark_grey = imagecolorallocate($image, 102, 102, 102);
$white = imagecolorallocate($image, 255, 255, 255);

//Set the path to our true type font 
//$font_path = 'advent_light';
//$font_path = 'advent_light.ttf';
//$font_path = 'arial';
$font_path = 'arial.ttf';

//Set our text string 
$string = 'Swapnesh!';

//Write our text to the existing image.
imagettftext($image, 50, 0, 10, 160, $white, $font_path, $string);

//Create our final image 
imagepng($image);

//Clear up memory 
imagedestroy($image);
?>

我尝试并搜索过但没有解决方案的内容如下- :(

  • 选中php标记之前的空白/空格(如果有的话)。
  • 删除header()方法之前的所有代码和空格。
  • imagecreate()更改为imagecreatetruecolor(),如代码中的注释中所示。
  • 检查font_path并按照注释设置字体路径。
  • 跟踪 &

我的PHP版本-PHPVersion5.3.8

仍然找不到问题:

误差

编辑-

1.0版

这就是我开始存钱的原因。

更多关于图像的信息--

保存页面时--这是保存文件的名称-- create_png.php.png

1.1版

代码语言:javascript
运行
复制
<br />
<b>Warning</b>:  imagettftext() [<a href='function.imagettftext'>function.imagettftext</a>]: Invalid font filename in <b>C:\xampp\htdocs\Core\CoreFiles\create_png.php</b> on line <b>20</b><br />
<br />
<b>Warning</b>:  imagettftext() [<a href='function.imagettftext'>function.imagettftext</a>]: Invalid font filename in <b>C:\xampp\htdocs\Core\CoreFiles\create_png.php</b> on line <b>23</b><br />

感谢@cryptic@user1711126

溶液 --

字体文件实际上缺少,我们需要将我们的.ttf文件放在文件夹下,以使此代码工作或设置路径使其工作。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-26 05:27:10

保存错误的图像文件,并在文本编辑器(如记事本或等效文件)中打开该文件,查看其中是否存在PHP错误。一个错误正在发生,然后输出文本,这会破坏文件。这样做,你就会发现错误是什么。

票数 4
EN

Stack Overflow用户

发布于 2012-12-26 05:53:02

我严重怀疑你的字体路径,请在同一个目录下检查你的字体和文件,如果是的话,就像这样改变路径,

代码语言:javascript
运行
复制
$font_path = './arial.ttf';

编辑(根据聊天中的讨论)

您必须需要一个ttf文件才能使用imagettftext()函数,将arial.ttf文件放在文件所在的文件夹中

票数 2
EN

Stack Overflow用户

发布于 2012-12-26 06:00:26

我认为@隐秘是对的,你需要使用真正的字体路径,比如

代码语言:javascript
运行
复制
$font_path = basename(dirname(__FILE__)).'/arial.ttf'; 

并请确保字体存在于目录中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14036298

复制
相关文章

相似问题

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