查看
GD functions.
1px方法的一个例子(现在包括测试页):
$filename = $_GET['filename'];
$image = imagecreatefromjpeg($filename);
$width = imagesx($image);
$height = imagesy($image);
$pixel = imagecreatetruecolor(1, 1);
imagecopyresampled($pixel, $image, 0, 0, 0, 0, 1, 1, $width, $height);
$rgb = imagecolorat($pixel, 0, 0);
$color = imagecolorsforindex($pixel, $rgb);
?>
Test Image Average Color<?php echo $color['green'] ?>, <?php echo $color['blue'] ?>)'>
下面是一些用于查找平均边框颜色的示例代码,类似于第一个链接.为了您的使用,这可能会更好(我知道这段代码效率低下,但希望很容易遵循):
$filename = $_GET['filename'];
$image = imagecreatefromjpeg($filename);
$width = imagesx($image);
$height = imagesy($image);
for($y = 0; $y < $height; $y++){
$rgb = imagecolorat($image, 0, $y);
$color = imagecolorsforindex($image, $rgb);
$red += $color['red'];
$green += $color['green'];
$blue += $color['blue'];
$rgb = imagecolorat($image, $width -1, $y);
$color = imagecolorsforindex($image, $rgb);
$red += $color['red'];
$green += $color['green'];
$blue += $color['blue'];
}
for($x = 0; $x < $height; $x++){
$rgb = imagecolorat($image, $x, 0);
$color = imagecolorsforindex($image, $rgb);
$red += $color['red'];
$green += $color['green'];
$blue += $color['blue'];
$rgb = imagecolorat($image, $x, $height -1);
$color = imagecolorsforindex($image, $rgb);
$red += $color['red'];
$green += $color['green'];
$blue += $color['blue'];
}
$borderSize = ($height=$width)*2;
$color['red'] = intval($red/$borderSize);
$color['green'] = intval($green/$borderSize);
$color['blue'] = intval($blue/$borderSize);
?>
更新:我把一些more refined code on github.这包括平均边框,平均整个图像.应该注意的是,调整到1px的资源比扫描每个像素要好得多(虽然我没有运行任何实时测试),但代码确实显示了三种不同的方法.