imagecolorstotal
GETS the number of colors in an image's palette.
This function returns the number of colors from the $image which is created by imagecreate or imagecreatetruecolor for example.
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.
This function returns the number of colors if is a PALETTE IMAGE.
This function returns 0, (ZERO), if is TRUECOLOR IMAGE.
<?php
int imagecolorstotal( GdImage $image )
where,
$image = The image identifier
?>
$image
The image identifier.
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$tst_img01a = "gif/GIF 001 01.gif";
$tst_img01b = "png/PNG 001 01.png";
echo $tst_img01a; ?><br><br>Existing PALETTE Image
<br><img src="<?php echo $tst_img01a; ?>"
alt="<?php echo $tst_img01a; ?>"><br><br>
<?php
$id_01a = imagecreatefromgif($tst_img01a);
$nbcolors01a = imagecolorstotal($id_01a);
echo 'Number total of colors<br>
in this image: ' . $nbcolors01a . '<br><br><br>';
echo $tst_img01b; ?><br><br>Existing TRUECOLOR Image
<br><img src="<?php echo $tst_img01b; ?>"
alt="<?php echo $tst_img01b; ?>"><br><br>
<?php
$id_01b = imagecreatefrompng($tst_img01b);
$nbcolors01b = imagecolorstotal($id_01b);
echo 'Number total of colors<br>
in this image: ' . $nbcolors01b . '<br><br><br>';
?>
RESULT GIF 001 01.gifExisting PALETTE ImageNumber total of colors
in this image: 1.PNG 001 01.pngExisting TRUECOLOR ImageNumber total of colors
in this image: 0, (ZERO).
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$tst_img02a = "gif/GIF Gago Coutinho.gif";
$tst_img02b = "png/PNG Gago Coutinho.png";
echo $tst_img02a; ?><br><br>Existing PALETTE Image
<br><img src="<?php echo $tst_img02a; ?>"
alt="<?php echo $tst_img02a; ?>"><br><br>
<?php
$id_02a = imagecreatefromgif($tst_img02a);
$nbcolors02a = imagecolorstotal($id_02a);
echo 'Number total of colors<br>
in this image: ' . $nbcolors02a . '<br><br><br>';
echo $tst_img02b; ?><br><br>Existing TRUECOLOR Image
<br><img src="<?php echo $tst_img02b; ?>"
alt="<?php echo $tst_img02b; ?>" width="400"><br><br>
<?php
$id_02b = imagecreatefrompng($tst_img02b);
$nbcolors02b = imagecolorstotal($id_02b);
echo 'Number total of colors<br>
in this image: ' . $nbcolors02b . '<br><br><br>';
?>
RESULT GIF Gago Coutinho.gifExisting PALETTE ImageNumber total of colors
in this image: 256.PNG Gago Coutinho.pngExisting TRUECOLOR ImageNumber total of colors
in this image: 0, (ZERO).
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$tst_img03a = "gif/GIF 003 03.gif";
$tst_img03b = "jpeg/JPEG 003 03.jpg";
echo $tst_img03a; ?><br><br>Existing PALETTE Image
<br><img src="<?php echo $tst_img03a; ?>"
alt="<?php echo $tst_img03a; ?>"><br><br>
<?php
$id_03a = imagecreatefromgif($tst_img03a);
$nbcolors03a = imagecolorstotal($id_03a);
echo 'Number total of colors<br>
in this image: ' . $nbcolors03a . '<br><br><br>';
echo $tst_img03b; ?><br><br>Existing TRUECOLOR Image
<br><img src="<?php echo $tst_img03b; ?>"
alt="<?php echo $tst_img03b; ?>" width="400"><br><br>
<?php
$id_03b = imagecreatefromjpeg($tst_img03b);
$nbcolors03b = imagecolorstotal($id_03b);
echo 'Number total of colors<br>
in this image: ' . $nbcolors03b . '<br><br><br>';
?>
RESULT GIF 003 03.gifExisting PALETTE ImageNumber total of colors
in this image: 1.JPEG 003 03.jpgExisting TRUECOLOR ImageNumber total of colors
in this image: 0, (ZERO).