imagecolorresolve
(PHP 4, PHP 5, PHP 7, PHP 8)
imagecolorresolve — 取得指定颜色的索引值或有可能得到的最接近的替代值
说明
本函数可以保证对所请求的颜色返回颜色索引,要么是准确的颜色要么是最接近的颜色。
如果图象由文件创建,只有该图象使用到的颜色会被解析。仅存在于调色板中的颜色不会被解析。
返回值
返回颜色索引值。
示例
示例 #1 使用 imagecoloresolve() 从图像中获取颜色
<?php
// 加载图像
$im = imagecreatefromgif('phplogo.gif');
// 从图像中获取最接近的颜色
$colors = array();
$colors[] = imagecolorresolve($im, 255, 255, 255);
$colors[] = imagecolorresolve($im, 0, 0, 200);
// 输出
print_r($colors);
?>
以上示例的输出类似于:
Array ( [0] => 89 [1] => 85 )
参见
- imagecolorclosest() - 取得与指定的颜色最接近的颜色索引值
+添加备注
用户贡献的备注 1 note
ceo at l-i-e dot com ¶
23 years ago
Okay, so sometimes it's really IMPORTANT to get the exact color you want, only it's not *IN* the Image, and ImageColorResolve just isn't "close enough".
The following code is a disgustingly gross hack, rather slow, but it does that.
$colorcount = array();
for ($x = 0; $x < $width; $x++){
for ($y = 0; $y < $height; $y++){
$colorindex = imagecolorat($jpg, $x, $y);
if (!isset($colorcount[$colorindex])){
$colorcount[$colorindex] = 1;
}
else{
$colorcount[$colorindex]++;
}
}
}
asort($colorcount);
reset($colorcount);
$black = imagecolorexact($jpg, 0, 0, 0);
if ($black == -1){
$goner = key($colorcount);
$rgb = imagecolorsforindex($jpg, $goner);
#error_log("Need black: About to kill $goner ($rgb[red], $rgb[green], $rgb[blue]) which was only used in $colorcount[$goner] pixels", 0);
unset($colorcount[$goner]);
imagecolordeallocate($jpg, $goner);
$black = imagecolorallocate($jpg, 0, 0, 0);
}
if ($black == -1){
$black = imagecolorresolve($jpg, 0, 0, 0);
#error_log("Damn! STILL couldn't allocate the color!", 0);
}
备份地址:http://www.lvesu.com/blog/php/function.imagecolorresolve.php