public static function average_color($image_url, $x1 = 0, $y1 = 0, $x2 = NULL, $y2 = NULL) { $im = self::getImageFromUrl($image_url); //Begin getting average $width = is_null($x2) ? imagesx($im) : $x2; $height = is_null($y2) ? imagesy($im) : $y2; $total = $r = $g = $b = $a = 0; for ($x = $x1; $x < $width; $x++) { for ($y = $y1; $y < $height; $y++) { //get rgba array at index $index = imagecolorat($im, $x, $y); $rgba = imagecolorsforindex($im, $index); //add total for each color $r += $rgba['red']; $g += $rgba['green']; $b += $rgba['blue']; $a += $rgba['alpha']; $total++; unset($index); unset($rgba); } // end for $y } // end for $x unset($im); $avg = array('red' => round($r / $total), 'green' => round($g / $total), 'blue' => round($b / $total), 'alpha' => round($a / $total)); $rgba = Rgba::create($avg); unset($r); unset($g); unset($b); unset($a); // return (array)$avg; return $rgba; }
public static function createRgba(Rgba $rgba) { return new Color($rgba->toArray()); }