Prova questo.
codice:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
define ("IMMED_STOP_IF_MORE_COLORS",true);
class CountColors {
var $image = "";
var $im;
var $arrayColors = array();
var $countColors = 0;
function CountColors($imgname,$stopIfMoreThanOneColor=false) {
//------ Testo se l'immagine è .gif o .jpg
if (eregi("[.](.{3})$",$imgname,$regs)) {
if ($regs[1] == "gif") {
$this->im = @imagecreatefromgif($imgname);
} else {
if ($regs[1] == "jpg") {
$this->im = @imagecreatefromjpeg($imgname);
} else {
die("Errore : tipo di immagine sconosciuto - sono validi i files con estensione .gif o .jpg");
} // if ($regs[1] == "jpg")
} // if ($regs[1] == "gif")
} // if (eregi("[.](.{3})$",$imgname,$regs))
//------ Verifica
if ($this->im) {
$width = imagesx($this->im);
$height = imagesy($this->im);
for ($i=0;$i<$width;$i++) {
for ($j=0;$j<$height;$j++) {
$colorIndex = imagecolorat($this->im,$i,$j);
if (! in_array($colorIndex, $this->arrayColors)) {
$this->arrayColors[] = $colorIndex;
$this->countColors++;
if ($stopIfMoreThanOneColor and $this->countColors > 1) {
break 2;
} // if ($stopIfMoreThanOneColor and $countColors > 1)
} // if (! in_array($colorIndex, $arrayColors))
} // for ($j=0;$j<$height;$j++)
} // for ($i=0;$i<$width;$i++)
} else {
die("Errore : immagine non trovata");
} // if ($this->im) {
} // function CountColors($imgname,$stopIfMoreThanOneColor=false)
function getColorsArray() {
$arrColors = array();
foreach ($this->arrayColors as $key => $value) {
$arr = imagecolorsforindex ( $this->im, $value);
//
$red = dechex($arr['red']);
$red = strlen($red) == 1 ? "0".$red : $red;
$green = dechex($arr['green']);
$green = strlen($green) == 1 ? "0".$green : $green;
$blue = dechex($arr['blue']);
$blue = strlen($blue) == 1 ? "0".$blue : $blue;
$arrColors[] = strtoupper('#'.$red.$green.$blue);
} // foreach ($this->arrayColors as $key => $value)
sort($arrColors);
return $arrColors;
} // function getColors()
function getColorsNumber() {
return $this->countColors;
}
} // class CountColors
//------- Si ferma dopo avere trattato tutti i pixels dell'immagine
$a = new CountColors("http://forum.html.it/forum/logo.gif");
print "Immagine GIF : Si ferma dopo avere trattato tutti i pixels dell'immagine
";
print "Numero di colori ".$a->getColorsNumber();
print "
Lista dei colori :";
print_r($a->getColorsArray());
print "<hr>";
//------- Si ferma dopo avere trattato tutti i pixels dell'immagine
$c = new CountColors("Image11.jpg");
print "Immagine JPG : Si ferma dopo avere trattato tutti i pixels dell'immagine
";
print "Numero di colori ".$c->getColorsNumber();
print "
Lista dei colori :";
print_r($c->getColorsArray());
print "<hr>";
//------- Si ferma immediatamente se c'è + di un colore
$b = new CountColors("http://forum.html.it/forum/logo.gif",IMMED_STOP_IF_MORE_COLORS);
print "Immagine GIF : Si ferma immediatamente se c'è + di un colore
";
print "Numero di colori ".$b->getColorsNumber();
?>
</body>
</html>