Php GD库,图像上的文本不工作和错误
我试图在图像上输出文本,页面加载为空白,设置错误报告时出现以下错误。知道它为什么这样做吗?我如何解决它Php GD库,图像上的文本不工作和错误,php,gd,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
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);
?>
…我认为你的图像的给定路径不起作用,因为imagecreatefrom。。。如果无法创建图像,则返回布尔值FALSE。 在任何情况下,在将事情移交给您的流程之前检查它们都是一种节省,这样您就可以与例外进行交互/使去bug更容易。。。
如果您执行了操作,请查看您的代码得到了什么。很可能是错误的文件路径。@图像和字体中的Mikk as在错误路径中?它们与index.php位于同一目录中…您的错误表明变量$jpg_image等于false,如果文件不可读,则imagecreatefromjpeg返回false。如果文件不包含GD库已知的图像数据,则返回false。检查明显的问题-大多数unix系统下的区分大小写的文件路径,或者可能是无效的文件权限。是否应该使用/var/www/。。。路径还是相对路径?谢谢@Mikkboth,相对路径和绝对路径应该可以。您可以执行var_dumpis_文件'image.jpg'和&is_可读'image.jpg';查看文件是否存在且可读。