<?php
function get_image_color($img) {
$img=imagecreatefromjpeg($img);
$w = imagesx($img);
$h = imagesy($img);
$r=$g=$b=0;
for($y=0; $y < $h; $y++) {
for($x = 0; $x < $w; $x++) {
$rgb = imagecolorat($img, $x, $y);
$r += $rgb >> 16;
$g += $rgb >> 8 & 255;
$b += $rgb & 255;
}
}
$pxls = $w * $h;
$r = dechex(round($r / $pxls));
$g = dechex(round($g / $pxls));
$b = dechex(round($b / $pxls));
if(strlen($r) < 2)$r = 0 . $r;
if(strlen($g) < 2)$g = 0 . $g;
if(strlen($b) < 2)$b = 0 . $b;
return "#" . $r . $g . $b;
}
$color=get_image_color("color2.jpg");
echo "<div style='background:$color;width:100px;height:100px;'></div><img src='color2.jpg' />";
转载于:https://my.oschina.net/yecao/blog/174310