search for in the  
<imagecolorsforindeximagecolortransparent>
Last updated: Thu, 19 May 2005

imagecolorstotal

(PHP 3, PHP 4, PHP 5)

imagecolorstotal -- Find out the number of colors in an image's palette

Description

int imagecolorstotal ( resource image )

This returns the number of colors in the specified image's palette.

See also imagecolorat() and imagecolorsforindex().



User Contributed Notes
imagecolorstotal
Felix Rusu <webmaster at betania dot ro>
10-Apr-2004 04:19
Actually that is not true.
When you use for example imagecreatefromjpeg() you create a truecolor image resource (as with the other truecolor image types). If you try to get the number of colors using imagecolorstotal() you get 0 (thats what happened to me at least). Instead you must convert the trucolor resource to pallete resource. You have to do that like this:

<?
$simg
=createfromjpeg("somejpeg.jpg");
imagetruecolortopalette($simg, false, 256);
$colors=imagecolorstotal($simg);
?>

Check the manual for the function signature.

Cheers.
love at tobe dot spamed
26-Jan-2004 03:33
to mgi : imagecolorstotal() function use a resource image. So doesn't take care about the image file format used. Even if you use a non-palette file format such as jpeg, it should work because it uses the palette built in the resource image... I'm wondering if I'm true.
black-eye at web dot de
01-Nov-2003 09:34
I created a function to count the colors in a .gif file automatically. Works perfect on Win2k Servers

<?PHP
function bildinfo($bild)
{
/*
******* FUNCTION BILDINFO() ******* ******* ******* *******
******* ******* ******* ******* ******* ******* ******* *******
******* Copyright 2003 by www.BLACKEYE.org ******* *******
******* ******* ******* ******* ******* ******* ******* *******
Please don't change the copyright in this file. You can modify
this file, but NEVER remove the copyright !
If YOU were the creator of this function, you would say the
same, eh ? :)

For more informations / How to use:
HOMEPAGE: www.blackeye.org
******* ******* ******* ******* ******* ******* ******* *******
*/
$fp = fopen($bild, "rb"); // Open image in binary mode (for windows)
$f = fread($fp, filesize($bild));
$c = bin2hex($f); // Binary file -> Hex
$c = ereg_replace("5c30", "0000", $c); // In gif-files "5c30" stands for real "00" values. why ? i dont know
$b = $c;

$pos = strpos($c, "fe0102"); // 6 Byte termination for the color table
if ($pos > 0)
{
$c = substr($c, 26);
$c = substr($c, 0, strpos($c, "fe0102")); // Termination for the color table of non-transparent images
}
else
{
$c = substr($c, 26);
$c = substr($c, 0, strpos($c, "21f904")); // Termination for the color table
}

echo
"<table border='0'>";

$i = 0;
$y = 0;
$str = "";
$pix = chunk_split($c, 6, ";"); // 6 Char long block (color)
$pix = explode(";",$pix);
sort($pix);
foreach(
$pix as $col)
{
if (
$col && !ereg($col.";", $str) && strlen($col) == 6) // So that no color is twice in the list
{
$str .= $col.";";
$i++; // Für die Table anzeige
$y++; // Anzahl Farben
if ($i == 1) { echo ""; }
echo
"<td width='6' height='8' bgcolor='#$col' style='border:1px solid #000000;font-size:1px;' title='©2003 by www.BLACKEYE.org'>";
echo
" ";
echo
"</td>";
if (
$i == 10)
{
echo
""; $i = 0;
}
}
}

echo
"</table>";
echo
$y." visible colors";
}

$bild = ""; // Imagefile
bildinfo($bild);
?>
Only copy & paste this text into your .php file, where you want to show the image.
mgi at humbug dot net
16-Jul-2003 12:54
I would assume that since it says "number of colors in an image's palette" that the behaviour is undefined for non-paletted image types (for example, JPEG). It would probably only make sense to use it for 256 (8-bit) indexed images.

<imagecolorsforindeximagecolortransparent>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT