Image Colour Count

RJM Programming - November, 2018 ... thanks to https://stackoverflow.com/questions/25400805/how-to-know-howmany-colors-are-exist-in-the-image

"; $nform="
        Image URL:   
"; if (isset($_GET['image'])) { $image = str_replace("+"," ",urldecode($_GET['image'])); } else if (isset($_POST['image'])) { $image = str_replace("+"," ",urldecode($_POST['image'])); } else { $image = ""; //"/Applications/MAMP/htdocs/clouds.jpg"; } $x=""; $colourCount = 0; $prefix = ""; if ($image != "") { //if (1 == 2 && strpos(strtolower($image), ".jp") !== false) { // $img = @imagecreatefromjpeg($image); // $prefix="

" . $x . $image . " contains " . $colourCount . " colours.

"; //} else { $img = @imagecreatefromstring(file_get_contents($image)); //} if (!$img) { //$x = " error "; $prefix="

" . $x . $image . " contains " . $colourCount . " colours.

"; } else { $colourCount = imagecolorstotal($img); if ($colourCount == 0) { // with code below, thanks to https://stackoverflow.com/questions/25400805/how-to-know-howmany-colors-are-exist-in-the-image //echo "yes"; //exit; $w = imagesx($img); $h = imagesy($img); // capture the raw data of the image ob_start(); imagegd2($img, null, $w); $data = ob_get_clean(); $totalLength = strlen($data); // calculate the length of the actual pixel data // from that we can derive the header size $pixelDataLength = $w * $h * 4; $headerLength = $totalLength - $pixelDataLength; // use each four-byte segment as the key to a hash table $counts = array(); for($i = $headerLength; $i < $totalLength; $i += 4) { $pixel = substr($data, $i, 4); $count =& $counts[$pixel]; $count += 1; } $colourCount = count($counts); //echo $colourCount; if (isset($_POST['background']) || isset($_GET['background'])) { $nhead=str_replace(">
" . $image . " contains " . $colourCount . " colours.


"; //"; } else { $prefix="

" . $image . " contains " . $colourCount . " colours.


"; } } // Free image imagedestroy($img); } } echo "Image Colour Count - RJM Programming - November, 2018 ... thanks to https://stackoverflow.com/questions/25400805/how-to-know-howmany-colors-are-exist-in-the-image"; ?>