imagecreatetruecolor
CREATE a new truecolor image.
TRUECOLOR IMAGE
is the designation term for images with a great number of colors.
For this type of image, a maximum of 16,777,216 colors are accepted.
Under PHP 7 or lower, this function returns a RESOURCE, however, under PHP 8 it returns an OBJECT.
We will call both RESOURCE and OBJECT, simply, IDENTIFIER that can also be represented by: GdImage.
<?php
GdImage|false imagecreatetruecolor ( int $width , int $height )
where,
$width = The image width in pixels
$height = The image height in pixels
?>
$width
The image width in pixels.
$height
The image height in pixels.
EXERCISE
<?php
/*
This exercise does not display any image,
it just displays the identifier: GdImage
This will be displayed using another function
So be patient!
*/
$w01 = 120;
$h01 = 120;
$img01 = imagecreatetruecolor( $w01, $h01 );
if(PHP_MAJOR_VERSION <= 7)
{
echo PHP_VERSION . '<br>';
echo $img01;
}
else
{
echo PHP_VERSION . '<br>';
var_dump($img01);
}
?>
EXERCISE
<?php
/*
This exercise does not display any image,
it just displays the identifier: GdImage
This will be displayed using another function
So be patient!
*/
$w02 = 110;
$h02 = 90;
$img02 = imagecreatetruecolor( $w02, $h02 );
var_dump($img02);
?>
EXERCISE
<?php
/*
This exercise does not display any image,
it just displays the identifier: GdImage
This will be displayed using another function
So be patient!
*/
if (!extension_loaded("gd"))
die("skip GD not present");
if (!function_exists("imagecreatetruecolor"))
die("skip GD Version not compatible");
function trycatch_dump(...$tests) {
foreach ($tests as $test) {
try {
var_dump($test());
echo '<br><br>';
}
catch (\Error $e) {
echo '!! [' . get_class($e) . '] ' .
$e->getMessage() . "<br><br>";
}
}
}
trycatch_dump(
fn() => imagecreatetruecolor(10, 30),
fn() => imagecreatetruecolor(-1, 30),
fn() => imagecreatetruecolor(30, -1)
);
?>
RESULT
Caution ...
Negative values are not accepted ...
... for both width and length.