Last active
November 16, 2015 05:09
-
-
Save mekkoo/9a39eca7499b89523692 to your computer and use it in GitHub Desktop.
色抽出 - 平均色
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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