【问题标题】:Image can not be displayed because it contains error in php gd图片无法显示,因为它在 php gd 中包含错误
【发布时间】:2012-12-26 05:22:44
【问题描述】:

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

代码 -

<?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(),就像代码中的 cmets 一样。
  • 检查 font_path 并根据 cmets 设置字体路径。
  • 已关注this & this

我的 PHP 版本 - PHP 版本 5.3.8

还是找不到问题:(

错误

编辑-

1.0 版

这就是我要保存的内容。

更多关于图片的信息 --

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

1.1 版

<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 文件放在文件夹下以使此代码工作或设置路径以使其工作。

【问题讨论】:

    标签: php gd


    【解决方案1】:

    保存错误的图像文件,然后在记事本等文本编辑器中打开它,看看里面是否有任何 PHP 错误。发生错误并输出文本,然后损坏文件。这样做,您会发现错误所在。

    【讨论】:

    • @swapnesh 不,将图像保存到计算机硬盘驱动器,然后右键单击硬盘驱动器上的文件并在记事本或某些文本编辑器等程序中打开它。并查找表明 PHP 错误的文本。如果您不能这样做,请让我们可以访问该文件,以便我们可以检查该文件。将文件作为文本文档打开的最简单方法是将其从 create_png.php.png 重命名为 create_png.php.txt 并打开它。
    • @cryptic..awesome 一些更有意义的东西最后..但我仍然需要你的帮助..请关注我的新编辑:)
    • @swapnesh $font_path 的值使其成为字体文件的完整路径,例如.. C:\xampp\htdocs\Core\CoreFiles\arial.ttf。
    • @swapnesh 请参阅fontfile us3.php.net/manual/en/… 的参数文档
    • 我试过了,但还是没有成功:( -- putenv('GDFONTPATH=' . realpath('.')); $font = 'arial'; && putenv('GDFONTPATH=' . realpath('.')); $font = 'arial.ttf';
    【解决方案2】:

    我认为@cryptic 是正确的,你需要使用真正的路径来使用类似的字体

    $font_path = basename(dirname(__FILE__)).'/arial.ttf'; 
    

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

    【讨论】:

      【解决方案3】:

      我严重怀疑你的字体路径,请仔细检查你的字体和同一目录下的文件,如果是,然后像这样更改路径,

      $font_path = './arial.ttf';
      

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

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

      【讨论】:

      • 谢谢回答 :) 它确实对我们解决问题有很大帮助 +1 为您提供所有帮助 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 2012-03-28
      • 1970-01-01
      • 2011-03-24
      相关资源
      最近更新 更多