imagegd CREATE a particular GD image to be shown in a browser or stored in a file.
This function, since PHP 7.2.0, allows to output truecolor images.
Formerly, these have been implicitly converted to palette.
The GD format generated by this function is used only to facilitate loading of images, or part of them.
Currently can only be used by compatible applications.
The GD and GD2 image formats are proprietary image formats of libgd.
They have to be regarded obsolete, and should only be used for development and testing purposes.
As of PHP 8.0.3 it is possible to set $file = NULL.
However, if libgd fail to output the image, this function returns TRUE.
This function returns TRUE on success os FALSE on failure.
<?php
bool imagegd (GdImage $image, ?string $file = null)
where,
$image = The image identifier
$file = The path or stream resource
?>
$image
The image identifier.
This function, since PHP 7.2.0, allows to output truecolor images.
Formerly, these have been implicitly converted to palette.
$file
The path or stream resource which is automatically being closed after this function returns to save the file to.
If not set or null, the raw image stream will be output directly; since PHP 8.0.3.
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$im01 = imagecreate (200, 200);
$blue = imagecolorallocate($im01, 0, 0, 8);
$img_file_name = 'gd/GD 001 01.gd';
$bool01 = imagegd($im01, $img_file_name);
if ($bool01 == TRUE)
{
$mess01 = 'The image was created, however,
<br>CAN NOT BE DISPLAYED by this browser!';
echo '$bool01 = ';
var_dump($bool01);
echo '<br><br>' . $mess01;
imagedestroy($im01);
}
else
{
$mess01 = 'The image COULD NOT BE CREATED!';
echo $mess01;
}
?>
RESULT
GD 001 01.gd
$bool01 = bool(true)
The image was created, however,
CAN NOT BE DISPLAYED by this browser!
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$im02 = imagecreate (200, 200);
$blue = imagecolorallocate($im02, 0, 0, 8);
$green = imagecolorallocate($im02, 0, 127, 0);
$img_file_name = 'gd/GD 002 02.gd';
$bool02 = imagegd($im02, $img_file_name);
if ($bool02 == TRUE)
{
$mess02 = 'The image was created, however,
<br>CAN NOT BE DISPLAYED by this browser!';
echo '$bool02 = ';
var_dump($bool02);
echo '<br><br>' . $mess02;
imagedestroy($im02);
}
else
{
$mess02 = 'The image COULD NOT BE CREATED!';
echo $mess02;
}
?>
RESULT
GD 002 02.gd
$bool02 = bool(true)
The image was created, however,
CAN NOT BE DISPLAYED by this browser!
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$im03 = imagecreatetruecolor (200, 200);
$blue = imagecolorallocate($im03, 0, 0, 8);
$green = imagecolorallocate($im03, 0, 127, 0);
$img_file_name = 'gd/GD 003 03.gd';
$bool03 = imagegd($im03, $img_file_name);
if ($bool03 == TRUE)
{
$mess03 = 'The image was created, however,
<br>CAN NOT BE DISPLAYED by this browser!';
echo '$bool03 = ';
var_dump($bool03);
echo '<br><br>' . $mess03;
imagedestroy($im03);
}
else
{
$mess03 = 'The image COULD NOT BE CREATED!';
echo $mess03;
}
?>
RESULT
GD 003 03.gd
$bool03 = bool(true)
The image was created, however,
CAN NOT BE DISPLAYED by this browser!
EXERCISE
<?php
// Try this code
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$im04 = imagecreatetruecolor (200, 200);
$blue = imagecolorallocate($im04, 0, 0, 8);
$green = imagecolorallocate($im04, 0, 127, 0);
$img_file_name = null;
imagegd($im04, $img_file_name);
?>