我的PHP无法创建真彩色图像
我有一个用于创建真彩色图像的脚本。我的PHP无法创建真彩色图像
但更改服务器后,新的PHP安装不显示图像。
这里是例子:http://www.europasprak.com/engine/system/utils/printBarImage.php?color=%239999FF&width=3&height=10
的源代码是:
<?PHP
require_once("website.php");
$color = LibEnv::getEnvHttpGET("color");
$width = LibEnv::getEnvHttpGET("width");
$height = LibEnv::getEnvHttpGET("height");
$color = urldecode($color);
if ($color && $width > 0 && $height> 0) {
LibImage::printBarImage($color, $width, $height);
}
// Print a bar image
static function printBarImage($color, $width, $height) {
// Create a blank image
$image = imagecreatetruecolor($width, $height);
if (strlen($color) == 6) {
$color = "#" . $color;
}
$r = intval(substr($color, 1, 2), 16);
$g = intval(substr($color, 3, 2), 16);
$b = intval(substr($color, 5, 2), 16);
$color = imagecolorallocate($image, $r, $g, $b);
// Fill up the image background
imagefilledrectangle($image, 0, 0, $width, $height, $color);
// Header indicating the image type
header("Content-type:image/jpeg");
// Create the image in the best jpeg quality
imagejpeg($image, NULL, 100);
// Destroy the image
imagedestroy($image);
}
?>
我的PHP版本5.6.20信息有这样一段话:
GD Support enabled
GD Version bundled (2.1.0 compatible)
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.5.2
GIF Read Support enabled
GIF Create Support enabled
JPEG Support enabled
libJPEG Version 6b
PNG Support enabled
libPNG Version 1.2.50
WBMP Support enabled
XBM Support enabled
更新:我注释掉图像输出并添加一个现有的外部图像进行调试:
// imagejpeg($image, NULL, 100);
$img=imagecreatefromjpeg("http://www.google.com/logos/doodles/2016/faten-hamamas-85th-birthday-4804306106580992-hp2x.jpg");
imagejpeg($img, NULL, 100);
而这个问题仍然是一样的。
所以它可能与我的GD设置无关,但与内容类型有关。
我删除任何BOM字符:
tail --bytes=+4 lib/image.php
tail --bytes=+4 system/utils/printBarImage.php
我尝试了不同的字符集:
iconv -f ISO-8859-1 -t utf-8 ... -o ...
编辑:我升级我的GD库:
GD Support enabled
GD headers Version 2.2.4
GD library Version 2.2.4
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.7.1
GIF Read Support enabled
GIF Create Support enabled
JPEG Support enabled
libJPEG Version unknown
PNG Support enabled
libPNG Version 1.6.28
WBMP Support enabled
XPM Support enabled
libXpm Version 30411
XBM Support enabled
使用error_log中()我可以看到$image = imagecreatetruecolor($width, $height);
函数调用创建了一个资源ID#678,并且该$color = imagecolorallocate($image, $r, $g, $b);
函数调用返回一个颜色代码6737049.
虽然这个问题保持不变。
我也尝试过使用另一个函数代替imagejpeg($image, NULL, 100);
,所以我先尝试imagepng($image);
然后imagegif($image);
,但问题仍然保持不变。
如果我注释掉header("Content-type:image/jpeg");
,那么输出的样子:����JFIF``��
源代码:
����JFIF``��<CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 100
��C��C��
��
���}!1AQa"q2���#B��R��$3br�
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������
���w!1AQaq"2�B���� #3R�br�
$4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������?�@����(��
(��?�
我选择的浏览器是Chrome浏览器
现在,如果我在Firefox中打开图像,它会显示:The image ... cannot be displayed because it contains errors.
。
编辑:我加了一些错误报告和日志是这样显示:
PHP Parse error: imagepng(): gd-png error: cannot allocate libpng main struct
我升级到PHP和30年5月6日解决了这个问题。
你得到了什么错误? – timclutton
日志保持空白。 – Stephane
您最后一个代码示例适用于PHP 5。 – Jocelyn