No ja obecnie zrobiłem to chyba prościej:
<?php
class avgColor {
var $resampledSize=300; //Img scaling to X pixels [keeps aspect ratio]
var $samplingAvg=10; //Number of results to averaging
private $gdResImg; //Loaded GD2 resource image
private $imgResY; //Img height [axis Y]
private $imgResX; //Img width [axis X]
var $colorMap; //Table of color in image
private function resizeResource() {
$max = $this->resampledSize;
$ratio = $this->imgResX/$this->imgResY;
$newX=$newY=$max;
if ($ratio < 1) $newX = $max*$ratio; else $newY = $max/$ratio; //Cares about ratio
$tmpRes = imagecreatetruecolor($newX, $newY);
imagecopyresampled($tmpRes, $this->gdResImg, 0, 0, 0, 0, $newX, $newY, $this->imgResX, $this->imgResY);
$this->imgRes = $tmpRes;
$this->imgResX = $newX;
$this->imgResY = $newY;
}
function __construct($imgPath) {
return false;
}
$this->imgResY = $imgInfo[1];
$this->imgResX = $imgInfo[0];
switch($imgInfo["mime"]) {
case "image/jpeg": $this->gdResImg = imagecreatefromjpeg($imgPath); break;
case "image/png": $this->gdResImg = imagecreatefrompng($imgPath); break;
case "image/gif": $this->gdResImg = imagecreatefromgif($imgPath); break;
case "image/jpeg": $this->gdResImg = imagecreatefromjpeg($imgPath); break;
}
$this->resizeResource(); //Resize res for sampling
}
private function trcArr(&$arr, $elements=10) { //truncate array to specified nums of elements
$i=1;
foreach($arr as $key=>$val) {
if($i<=$elements) $ret[$key] = $val;
$i++;
}
$arr=$ret;
}
private function mapColors($force=false) { //Make colors map for image; 1st param is force rerun function even if the map is already done
if(!empty($this->colorMap)&&!$force) return; //If map is already done do nothing
for ($x=0;$x<$this->imgResX;$x++) { //Loop for "rows"
for ($y=0;$y<$this->imgResY;$y++) { //Loop for every pixel in "row"
$rgb = imagecolorat($this->gdResImg, $x, $y);
@$this->colorMap[(($rgb >> 16) & 0xFF)."-".(($rgb >> 8) & 0xFF)."-".($rgb & 0xFF)]++; //Hide notices about index
}
}
arsort($this->colorMap); //Sort arr $this->trcArr($this->colorMap, $this->samplingAvg); //Strip array
}
private function avgColor() { //Better method for avg color
$this->mapColors(); //Make map w/o force
$color=array("r"=>0,"g"=>0,"b"=>0,"rgb"=>"fff");
foreach($this->colorMap as $key=>$val) { //Sum colors
$tmp = explode("-", $key); //Extract from R-G-B notation $color["r"] += $tmp[0]; //Red
$color["g"] += $tmp[1]; //Green
$color["b"] += $tmp[2]; //Blue
}
$color["r"] = $color["r"]/$this->samplingAvg;
$color["g"] = $color["g"]/$this->samplingAvg;
$color["b"] = $color["b"]/$this->samplingAvg;
return $color;
}
function get() {
return $this->avgColor();
}
}
?>
/************/
<?php
/********************************************************************************
*/
function _st() {
return $mtime[1] + $mtime[0];
}
function _et($_st) {
$execT = (($mtime[1] + $mtime[0]) - $_st);
echo "\n\n".str_repeat("-", 40)."\nExec time: ".(($execT<1
)?
($execT*1000)."ms":$execT."s")."\n\n"; }
/********************************************************************************
*/
require_once("sim.class.php");
$_sim = new avgColor("imgs/647636.jpg");
$_st=_st();
_et($_st);
?>
I obrazek 12Mpx robi w 15ms. Pokaż obrazek abyśmy testowali na tym samym (IMG:
style_emoticons/default/winksmiley.jpg)