<?php
// image_colours_of.php
// RJM Programming
// November, 2018
$nhead="><h1>Image Colour Count</h1><h3>RJM Programming - November, 2018 ... thanks to <a target=_blank title='Great coding here, thanks' href='//stackoverflow.com/questions/25400805/how-to-know-howmany-colors-are-exist-in-the-image'>https://stackoverflow.com/questions/25400805/how-to-know-howmany-colors-are-exist-in-the-image</a></h3>";
$nform="<br><form action='./image_colours_of.php' method='POST'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image URL: <input style=width:50%; name='image' id='image' type='text' value=''></input>&nbsp;&nbsp;<input style=background-color:yellow; type='submit' value='Colours Count'></input><input type=hidden name=background value='yes'></input></form>";

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="<p>" . $x . $image . " contains " . $colourCount . " colours.</p>";
//} else {
 $img = @imagecreatefromstring(file_get_contents($image));
//}

if (!$img) {
//$x = " error ";
$prefix="<p>" . $x . $image . " contains " . $colourCount . " colours.</p>";
} 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("><h1", " style=\"background-image: linear-gradient(rgba(255,255,255,0.6),rgba(255,255,255,0.6)), url('" . $image . "'); background-repeat:repeat;\"><div style=background-color:rgba(255,255,255,0.7);><h1",$nhead);
$nform.="</div>";
$prefix="<p><a target=_blank href='" . $image . "' title='New window'>" . $image . "</a> contains " . $colourCount . " colours.</p><br>"; //<img src='" . $image . "'></img>";
} else {
$prefix="<p><a target=_blank href='" . $image . "' title='New window'>" . $image . "</a> contains " . $colourCount . " colours.</p><br><img src='" . $image . "'></img>";
}
}

// Free image
imagedestroy($img);
}
}

echo "<!doctype html><html><head><title>Image Colour Count - RJM Programming - November, 2018 ... thanks to https://stackoverflow.com/questions/25400805/how-to-know-howmany-colors-are-exist-in-the-image</title></head><body" . $nhead . $prefix . $nform . "</body></html>";

?>
