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="
         or Image URL:   
"; $byes=false; if (isset($_POST['background']) || isset($_GET['background'])) { $byes=true; } $jscr=" var form=null; var xhr=null; var cnt=0; var clicked=[]; var ourmimetype=''; var types = ['video/mp4','image/svg','audio/wav','audio/x-wav','audio/x-pn-realaudio','audio/x-mpegurl','audio/x-aiff','audio/mpeg','audio/mid', 'audio/basic','audio/ogg','video/x-sgi-movie','video/x-msvideo','video/quicktime','audio/mp3','video/mp4','video/mpeg', 'video/x-la-asf','video/ogg','video/webm','audio/mp4', 'image/jpeg', 'image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/tif', 'text/html', 'text/html', 'text/html', 'text/javascript', 'text/css', 'text/plain', 'text/xml', 'text/csv', 'application/vnd.ms-word', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/x-php', 'application/pdf', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd.ms-powerpoint', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']; var exts = ['.mp4','.svg','.wav','.wav','.ram','.m3u','.aiff','.mp3','.rmi', '.snd','.ogg','.movie','.avi','.mov','.mp3','.m4v','.mpeg', '.lsx','.ogv','.webm','.m4a', '.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tif', '.htm', '.html', '.htmls', '.js', '.css', '.txt', '.xml', '.csv', '.doc', '.docx', '.php', '.pdf', '.pptx', '.ppt', '.xls', '.xlsx']; function onl() { var input = document.getElementById('cimage'); // .querySelector('input[type=file]'); // .getElementById('cimage'); // see Example 4 input.onchange = function () { var file = input.files; //[0]; for (var ii=0; ii')[0].split('')[0].split(' "; while (("" . $suffix) != "") { $suffix=trim($suffix); if (isset($_POST['filename' . $suffix])) { if (!file_exists($_FILES['images' . $suffix]['tmp_name']) || !is_uploaded_file($_FILES['images' . $suffix]['tmp_name'])) { if (1 == 2) { file_put_contents('qpm.qpm','No upload'); } //echo ''; exit; } $upload=true; $fni=dirname(__FILE__) . "/" . str_replace("+", " ", urldecode($_POST['filename' . $suffix])); $ii=0; while (file_exists($fni)) { $fni=str_replace(".", "" . $ii . ".", $fni); $ii++; } move_uploaded_file($_FILES['images' . $suffix]['tmp_name'], $fni); $image = $fni; $datau="data:" . str_replace('+',' ',urldecode($_POST['types' . $suffix])) . ";base64," . base64_encode(file_get_contents($image)); $style=" style='background:\"url(" . datau . ")\";'"; if (("" . $suffix) == "") { $suffix=1; if (!isset($_POST['filename' . $suffix])) { $suffix=""; } } else { $suffix++; if (!isset($_POST['filename' . $suffix])) { $suffix=""; } } $jsuffix=$suffix; } else 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; 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 ($upload) { unlink($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 ($byes) { $nhead=str_replace(">
" . $image . " contains " . $colourCount . " colours.



"; } else { $prefix.="

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


"; //"; } $byes=false; } else { if ($datau != "") { $prefix.="

" . $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" . $jscr . ""; ?>