Skip to content

Instantly share code, notes, and snippets.

@mekkoo
Last active November 16, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mekkoo/9a39eca7499b89523692 to your computer and use it in GitHub Desktop.
Save mekkoo/9a39eca7499b89523692 to your computer and use it in GitHub Desktop.
色抽出 - 平均色
<?php
class SenseOfColor
{
private $filepath;
public function __construct($filepath)
{
$this->filepath = $filepath;
}
public function getColor()
{
$img = imagecreatefrompng($this->filepath);
$imgX = imagesx($img);
$imgY = imagesy($img);
$imgXY = $imgX*$imgY;
$rSum = '';
$gSum = '';
$bSum = '';
for ($y = 0; $y < $imgY; $y++) {
for ($x = 0; $x < $imgX; $x++) {
$rgb = imagecolorat($img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$rSum += $r;
$gSum += $g;
$bSum += $b;
}
}
return '#'.dechex($rSum/$imgXY).dechex($gSum/$imgXY).dechex($bSum/$imgXY);
}
}
class SenseOfColorFactory
{
public static function create($filepath)
{
return new SenseOfColor($filepath);
}
}
$imgColor = SenseOfColorFactory::create('img/orange_white.png');
print($imgColor->getColor());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment