GD库,图像上的文字不起作用和错误

时间:2014-08-04 00:17:50

标签: php gd

我尝试在图像上输出文本,并且页面加载为空白,在设置错误报告时出现以下错误。知道为什么要这样做以及如何解决它?

PHP message: PHP Warning:  imagecolorallocate() expects parameter 1 to be resource, boolean given in /var/www/gd-text/index.php on line 13
PHP message: PHP Warning:  imagettftext() expects parameter 1 to be resource, boolean given in /var/www/gd-text/index.php on line 22
PHP message: PHP Warning:  imagejpeg() expects parameter 1 to be resource, boolean given in /var/www/gd-text/index.php on line 25
PHP message: PHP Warning:  imagedestroy() expects parameter 1 to be resource, boolean given in /var/www/gd-text/index.php on line 28" while reading response header from upstream, client: 203.176.102.98, server: myserver.com, request: "GET /gd-text/index.php HTTP/1.1", upstream: "fastcgi://unix:/dev/shm/php-fpm-www.sock:", host: "myserver.com"

我的PHP:

<?php

//error_reporting(E_ALL);
//ini_set("display_errors", 1);

  //Set the Content Type
  header('Content-type: image/jpeg');

  // Create Image From Existing File
  $jpg_image = imagecreatefromjpeg('image.jpg');

  // Allocate A Color For The Text
  $black = imagecolorallocate($jpg_image, 0, 0, 0);

  // Set Path to Font File
  $font_path = 'myfont.ttf';

  // Set Text to Be Printed On Image
  $text = "Text on image test";

  // Print Text On Image
  imagettftext($jpg_image, 25, 0, 75, 300, $black, $font_path, $text);

  // Send Image to Browser
  imagejpeg($jpg_image);

  // Clear Memory
  imagedestroy($jpg_image);
?>

1 个答案:

答案 0 :(得分:0)

..我认为图像的给定路径不起作用,因为如果不能创建图像,imagecreatefrom ...返回布尔值FALSE。
在任何情况下,在将它们交给您的过程之前检查一些东西是很安全的,这样您就可以与excepteion进行交互/使得更容易进行更换... 如果您执行<? ... var_dump(is_file($jpg)); ?>

,请查看您的代码内容