I have installed GD on my PHP Version 5.6.32
server.
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.8.0
GIF Read Support enabled
GIF Create Support enabled
JPEG Support enabled
libJPEG Version unknown
PNG Support enabled
libPNG Version 1.6.29
WBMP Support enabled
XPM Support enabled
libXpm Version 30411
XBM Support enabled
Here is the function creating the image:
// 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"); // <<== This is the line 467
// Create the image in the best jpeg quality
imagejpeg($image, NULL, 100);
// Destroy the image
imagedestroy($image);
}
This above function is called by the script:
<?PHP
require_once("website.php");
ob_end_flush(); // <<== Added for debugging purposes
$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);
}
?>
But it won't display any image.
You can see it in action at http://www.thalasoft.com/engine/system/utils/printBarImage.php?color=%2392299FF&width=30&height=100
My phpinfo
page shows the default_charset
to be UTF-8
.
But the file is in the charset:
$ uchardet system/utils/printBarImage.php
ascii/unknown
The file contains no BOM character:
$ head -c 3 system/utils/printBarImage.php | hexdump -C
00000000 3c 3f 50 |<?P|
00000003
The same issue occurs with the functions imagepng
or imagejpeg
.
I know the image is created for if I call the function as imagepng($image, "/usr/bin/learnintouch/engine/image.jpeg");
then it creates the /usr/bin/learnintouch/engine/image.jpeg
file on the file system and I can open it in another browser tab and see the image.
Even saving the image, it cannot be read later on, no image is displayed:
//imagejpeg($image, "/usr/bin/learnintouch/engine/image.jpeg", 100);
readfile("/usr/bin/learnintouch/engine/image.jpeg");
Following Phil's advice, I added an ob_end_flush();
call, which gives the following error (the line number is marked above in the source code as well) in my dev environment:
Error message: Cannot modify header information - headers already sent by (output started at /usr/bin/learnintouch/engine/system/utils/printBarImage.php:5)
Filename: /usr/bin/learnintouch/engine/lib/image.php
Line: 467
On the public test environment at http://www.thalasoft.com/engine/system/utils/printBarImage.php?color=%2392299FF&width=30&height=100
it displays garbled characters.
UPDATE: Commenting on the solution... I added the ob_end_clean();
function call to the script file, and the image then was displayed all right. The script file now reads:
<?PHP
require_once("website.php");
// Prevent any possible previous header from being sent before the following image header that must be the first
ob_end_clean();
$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);
}
?>