imagecolorstotal 



gd apg

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.



  1 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.gif

Existing PALETTE Image

GIF 001 01.gif apr

Number total of colors
in this image: 1.



PNG 001 01.png

Existing TRUECOLOR Image

PNG 001 01.png apr

Number total of colors
in this image: 0, (ZERO).


  2 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.gif

Existing PALETTE Image

GIF Gago Coutinho.gif apr

Number total of colors
in this image: 256.



PNG Gago Coutinho.png

Existing TRUECOLOR Image

PNG Gago Coutinho.png apr

Number total of colors
in this image: 0, (ZERO).


  3 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.gif

Existing PALETTE Image

GIF 003 03.gif apr

Number total of colors
in this image: 1.



JPEG 003 03.jpg

Existing TRUECOLOR Image

JPEG 003 03.jpg apr

Number total of colors
in this image: 0, (ZERO).