问题
I have a script that used to create a true color image.
But after changing of server the new PHP installation does not show the image.
Here is the example: http://www.europasprak.com/engine/system/utils/printBarImage.php?color=%239999FF&width=3&height=10
The source code is:
<?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);
}
?>
My PHP Version 5.6.20 info has this to say:
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
UPDATE: I commented out the image output and added an existing external image to debug:
// imagejpeg($image, NULL, 100);
$img=imagecreatefromjpeg("http://www.google.com/logos/doodles/2016/faten-hamamas-85th-birthday-4804306106580992-hp2x.jpg");
imagejpeg($img, NULL, 100);
And the issue remains the same.
So it is probably not related to my GD setup, but lies with the content type.
I removed any BOM characters:
tail --bytes=+4 lib/image.php
tail --bytes=+4 system/utils/printBarImage.php
I tried different charsets:
iconv -f ISO-8859-1 -t utf-8 ... -o ...
EDIT: I upgraded my GD library to:
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
Using error_log() I can see that the $image = imagecreatetruecolor($width, $height);
function call creates a Resource id #678 and that the $color = imagecolorallocate($image, $r, $g, $b);
function call returns a color code 6737049.
This issue remained the same though.
I also tried using another function in place of imagejpeg($image, NULL, 100);
and so I tried first imagepng($image);
and then imagegif($image);
but the issue remained the same again.
If I comment out the header("Content-type:image/jpeg");
then the output looks like: ����JFIF``��
with source code:
����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��������������������������������������������������������������������������?�@����(��
(��?�
My browser of choice is Chrome.
Now, if I open the image in Firefox, it shows this: The image ... cannot be displayed because it contains errors.
.
EDIT: I added some error reporting and the log had this to show:
PHP Parse error: imagepng(): gd-png error: cannot allocate libpng main struct
回答1:
I upgraded PHP to 5.6.30 and it solved the issue.
来源:https://stackoverflow.com/questions/36820434/my-php-cannot-create-a-true-color-image