Определение доминирующих тонов на изображении PHP

Скрипт определяет основные цвета изображения.

class GeneratorImageColorPalette {
	function getImageColor($imageFile_URL, $numColors, $image_granularity = 5) {
   		$image_granularity = max(1, abs((int)$image_granularity));
   		$colors = array();
   		$size = @getimagesize($imageFile_URL);
   		if($size === false)	{
      		user_error("Unable to get image size data");
      		return false;
   		}
		$image_ext = strtolower(strrchr($imageFile_URL,"."));
		if ($image_ext == ".gif") { $filetype = 1;
		} elseif ($image_ext == ".jpg") { $filetype = 2;
		} elseif ($image_ext == ".jpeg") { $filetype = 2;
		} elseif ($image_ext == ".png") { $filetype = 3;
		} else { $filetype = false; }
		if ($filetype == 1) { $img = imagecreatefromgif($imageFile_URL); }
   		elseif ($filetype == 2) { $img = imagecreatefromjpeg($imageFile_URL); }
   		elseif ($filetype == 3) { $img = imagecreatefrompng($imageFile_URL); }
   		if(!$img) {
   	  		user_error("Unable to open image file");
   		   return false;
   		}
   		for($x = 0; $x < $size[0]; $x += $image_granularity) {
      		for($y = 0; $y < $size[1]; $y += $image_granularity) {
         		$thisColor = imagecolorat($img, $x, $y);
         		$rgb = imagecolorsforindex($img, $thisColor);
        		$red = round(round(($rgb['red'] / 0x33)) * 0x33);
         		$green = round(round(($rgb['green'] / 0x33)) * 0x33);
         		$blue = round(round(($rgb['blue'] / 0x33)) * 0x33);
         		$thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue);
         		if(array_key_exists($thisRGB, $colors))
         		{
           			 $colors[$thisRGB]++;
         		}
         		else
         		{
           			 $colors[$thisRGB] = 1;
         		}
      		}
   		}
   		arsort($colors);
   		return array_slice(array_keys($colors), 0, $numColors);
	}
	function getHtml2Rgb($str_color) {
    	if ($str_color[0] == '#')
        	$str_color = substr($str_color, 1);

  	  	if (strlen($str_color) == 6)
        	list($r, $g, $b) = array($str_color[0].$str_color[1],
                                 $str_color[2].$str_color[3],
                                 $str_color[4].$str_color[5]);
    	elseif (strlen($str_color) == 3)
        	list($r, $g, $b) = array($str_color[0].$str_color[0], $str_color[1].$str_color[1], $str_color[2].$str_color[2]);
    	else
        	return false;

    	$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
    	$arr_rgb = array($r, $g, $b);
    	return $arr_rgb;
	}
}


Параметр $url ссылка на изображения, а $count — количество доминирующих цветов.

if (isset($_GET['url']) && !empty($_GET['url'])) {
$img = new GeneratorImageColorPalette();
$url = $_GET['url'];
$count = 10;
$colors = $img->getImageColor($url, $count, $image_granularity = 5);
echo "<center><img src='".$url."'></center>";
	for ($i = 0; $i<$count; $i++) {
		echo "<br><div id='color' style='float:right;width:60%;height:20px;border:1px solid black;background-color:#".$colors[$i].";'>&nbsp; #".$colors[$i]."</div>";	
	}
echo "<br><br><br>";	
}



Просмотров: 3072
13.02.2015, 22:41 -

Категория: Статьи » Программирование » PHP

Коментарии к Определение доминирующих тонов на изображении PHP:

Andrey.P
11 декабря 2017 04:22
Вжух! И все четко работает! Автору респект