Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Image Functions : imagecolorstotal

imagecolorstotal

Find out the number of colors in an image's palette (PHP 4, PHP 5)
int imagecolorstotal ( resource image )

Returns the number of colors in an image palette.

Parameters

image

An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().

Return Values

Returns the number of colors in the specified image's palette or 0 for truecolor images.

Code Examples / Notes » imagecolorstotal

bisqwit

This code shows the palettes for all .gif files in the current working directory. (Output is HTML.)
<?php
print '<pre>';
foreach(glob('*.gif') as $src_fn)  
{
 $im2 = ImageCreateFromGif($src_fn);
 
 echo '<a href="',htmlspecialchars($src_fn),'">';
 print sprintf('%-30s', $src_fn);
 echo '</a>';
 $cc = ImageColorsTotal($im2);
 for($n=0; $n<$cc; ++$n)
 {
   $c = ImageColorsForIndex($im2, $n);
   print
     sprintf('<span style="background:#%02X%02X%02X">&nbsp;</span>',
      $c['red'], $c['green'], $c['blue']);
 }
 ImageDestroy($im2);
 print "\n";
}
print '</pre>';
?>


black-eye

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.


Change Language


Follow Navioo On Twitter
gd_info
getimagesize
image_type_to_extension
image_type_to_mime_type
image2wbmp
imagealphablending
imageantialias
imagearc
imagechar
imagecharup
imagecolorallocate
imagecolorallocatealpha
imagecolorat
imagecolorclosest
imagecolorclosestalpha
imagecolorclosesthwb
imagecolordeallocate
imagecolorexact
imagecolorexactalpha
imagecolormatch
imagecolorresolve
imagecolorresolvealpha
imagecolorset
imagecolorsforindex
imagecolorstotal
imagecolortransparent
imageconvolution
imagecopy
imagecopymerge
imagecopymergegray
imagecopyresampled
imagecopyresized
imagecreate
imagecreatefromgd2
imagecreatefromgd2part
imagecreatefromgd
imagecreatefromgif
imagecreatefromjpeg
imagecreatefrompng
imagecreatefromstring
imagecreatefromwbmp
imagecreatefromxbm
imagecreatefromxpm
imagecreatetruecolor
imagedashedline
imagedestroy
imageellipse
imagefill
imagefilledarc
imagefilledellipse
imagefilledpolygon
imagefilledrectangle
imagefilltoborder
imagefilter
imagefontheight
imagefontwidth
imageftbbox
imagefttext
imagegammacorrect
imagegd2
imagegd
imagegif
imagegrabscreen
imagegrabwindow
imageinterlace
imageistruecolor
imagejpeg
imagelayereffect
imageline
imageloadfont
imagepalettecopy
imagepng
imagepolygon
imagepsbbox
imagepsencodefont
imagepsextendfont
imagepsfreefont
imagepsloadfont
imagepsslantfont
imagepstext
imagerectangle
imagerotate
imagesavealpha
imagesetbrush
imagesetpixel
imagesetstyle
imagesetthickness
imagesettile
imagestring
imagestringup
imagesx
imagesy
imagetruecolortopalette
imagettfbbox
imagettftext
imagetypes
imagewbmp
imagexbm
iptcembed
iptcparse
jpeg2wbmp
png2wbmp
eXTReMe Tracker