imagecolorallocate ALLOCATE a particular color for an image.
PALETTE IMAGE
is the designation term for images with a small number of colors.
For this type of image, a maximum of 256 colors are accepted.
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.
$image symbolized by a resource or object returned by one of the image creation functions, such as imagecreate or imagecreatetruecolor.
The first call to this function attaches the background color in palette images created using imagecreate.
The first call to this function don't attaches any background color in truecolor images created using imagecreatetruecolor.
$red, $green & $blue:
0 ≥ $red ≤ 255, 0 ≥ $green ≤ 255 & 0 ≥ $blue ≤ 255
or
0x00 ≥ $red ≤ 0xff, 0x00 ≥ $green ≤ 0xff & 0x00 ≥ $blue ≤ 0xff
or
0x00 ≥ $red ≤ 0xFF, 0x00 ≥ $green ≤ 0xFF & 0x00 ≥ $blue ≤ 0xFF
This function may return FALSE if the allocation failed, but may also return a non-Boolean value which evaluates to FALSE.
We recommed to use the operator === or var_dump for testing the returned value of this function.
<?php
int|false imagecolorallocate ( GdImage $image,
int $red,
int $$green,
int $blue )
where,
$image = An image identifier
$red = The value of red component of the color
$green = The value of green component of the color
$blue = The value of blue component of the color
?>
$image
An image identifier.
$red
The value of red component of the color .
$green
The value of green component of the color.
$blue
The value of blue component of the color.
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - -
RUN this exercise to see the result
The use of the functions seen so far
does not yet allow the display of
the generated images.
This will be seen closely
with the use of other functions.
Patience, then!
- - - - - - - - - - - - - - - - - - - - - - */
$w01 = 100;
$h01 = 100;
$img01p = imagecreate($w01, $h01);
$img01t = imagecreatetruecolor($w01, $h01);
$col_red_01p = imagecolorallocate($img01p, 255, 0, 0);
$col_red_01t = imagecolorallocate($img01t, 255, 0, 0);
if(PHP_MAJOR_VERSION <= 7)
{
echo PHP_VERSION . '<br>';
echo '<br><br>PALETE COLOR image resouce<br><br>';
var_dump($img01p);
echo '<br><br>TRUE COLOR image resouce<br><br>';
var_dump($img01t);
echo '<br><br>PALETE COLOR imagecolorallocate<br><br>';
var_dump($col_red_01p);
echo '<br><br>TRUE COLOR imagecolorallocate<br><br>';
var_dump($col_red_01t);
}
else
{
echo PHP_VERSION . '<br>';
echo '<br><br>PALETE COLOR image object<br><br>';
var_dump($img01p);
echo '<br><br>TRUE COLOR image object<br><br>';
var_dump($img01t);
echo '<br><br>PALETE COLOR imagecolorallocate<br><br>';
var_dump($col_red_01p);
echo '<br><br>TRUE COLOR imagecolorallocate<br><br>';
var_dump($col_red_01t);
}
?>
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - -
RUN this exercise to see the result
The use of the functions seen so far
does not yet allow the display of
the generated images.
This will be seen closely
with the use of other functions.
Patience, then!
- - - - - - - - - - - - - - - - - - - - - - */
if(!extension_loaded('gd')) {
die('skip gd extension is not loaded');
}
if(!function_exists('imagecreatetruecolor')) {
die('skip imagecreatetruecolor
function is not available');
}
echo "Testing imagecolorallocate() :
basic functionality.<br><br><br>";
$im = imagecreatetruecolor(200, 200);
var_dump($im);
echo '<br><br>';
// Calling imagecolorallocate()
// with all possible arguments
var_dump( imagecolorallocate($im, 255, 0, 0) );
echo '<br><br>';
var_dump( imagecolorallocate($im, 0, 255, 0) );
echo '<br><br>';
var_dump( imagecolorallocate($im, 0, 0, 255) );
echo '<br><br>';
var_dump( imagecolorallocate($im, 255, 255, 255) );
?>