Home >Backend Development >PHP Tutorial >[PHP] Identify the main color of the picture_PHP tutorial
1. Applicable scenarios: List the corresponding pictures according to color.
2. Main program and API explanation:
[php]
class MajorColor
{
//Reference color
protected $_colors = null;
//Tolerance
protected $_tolerance = 80;
//Ignored colors
protected $_ignoreColors = array();
//Supported image types
protected $_funcs = array('image/png' => 'imagecreatefrompng', 'image/jpeg' => 'imagecreatefromjpeg', 'image/gif' => 'imagecreatefromgif');
Public function __construct(array $colors = null) {
If(null !== $colors) {
$this->_colors = $colors;
}
}
Public function setColors(array $colors) {
$this->_colors = $colors;
}
Public function setTolerance($tolerance) {
$this->_tolerance = $tolerance;
}
Public function setIgnoreColors($colors) {
$this->_ignoreColors = $colors;
}
Public function _isValidColor($confVal, $val) {
If(is_array($confVal)) {
return $val >= $confVal[0] && $val <= $confVal[1];
} else {
return $val >= $confVal - $this->_tolerance && $val <= $confVal + $this->_tolerance;
}
}
Public function getOrderedColors($pic) {
$size = getimagesize($pic);
If(!$size) {
return false;
}
$width = $size[0];
$height = $size[1];
$mime = $size['mime'];
$func = isset($this->_funcs[$mime]) ? $this->_funcs[$mime] : null;
If(!$func) {
return false;
}
$im = $func($pic);
If(!$im) {
return false;
}
$total = $width * $height;
$nums = array();
for($i = 0; $i < $width; $i++) {
for($m = 0; $m < $height; $m++) {
$color_index = imagecolorat($im, $i, $m);
$color_tran = imagecolorsforindex($im, $color_index);
$alpha = $color_tran['alpha'];
unset($color_tran['alpha']);
if(100 < $alpha || in_array($color_tran, $this->_ignoreColors)) {
continue;
}
foreach ($this->_colors as $colorid => $color) {
if($this->_isValidColor($color['red'], $color_tran['red'])
&& $this->_isValidColor($color['green'], $color_tran['green'])
&& $this->_isValidColor($color['blue'], $color_tran['blue'])
) {
$nums[$colorid] = isset($nums[$colorid]) ? $nums[$colorid] + 1 : 1;
}
}
}
}
imagedestroy($im);
arsort($nums);
return $nums;
}
public function getMajorColor($pic) {
$nums = $this->getOrderedColors($pic);
$keys = array_keys($nums);
return $keys[0];
}
}
1.void setColors(array $colors)
设置可选颜色,即上图中“全部颜色”下的所有颜色(白、灰、黑...)
2.void setTolerance(int $tolerance)
设置容差,比如绿色的RGB值为(0,255,0),如果设置容差为40,那么-40<=R<<40 && 215<=G<=295 && -40<=B<=40范围内的所有颜色将被视为绿色。
此方法用于大致区别各颜色。
3.void setIgnoreColors(array $colors)
设置不需考虑的颜色。如大多图片的背景是白色,而我们显然不希望结果是白色,此时可调用此方法简略白色。
4.array getOrderedColors($pic)
根据$pic获取各种颜色(用setColors设置的颜色)的匹配数量,按匹配量由高到低排列
参数$pic是待检测图片的路径
5.mix getMajorColor($pic)
内部调用getOrderedColors,返回匹配量最高的颜色的key
三、$colors的格式及范围确定
1.如果$colors中的各种颜色差别很明显,我们只需简单的传入颜色值,内部会根据setTolerance设置的容差来区别各颜色。
[php]
$colors = array(
1 => array('red' => 0xff, 'green' => 0xff, 'blue' => 0xff),
2 => array('red' => 0xc0, 'green' => 0xc0, 'blue' => 0xc0),
2 => array('red' => 0x00, 'green' => 0x00, 'blue' => 0x00),
);
2. The setTolerance method of setting tolerance can only roughly distinguish various colors. If you need more precise control, you need to set the R, G, and B ranges of a certain color respectively www.2cto.com
[php]
$colors = array(
1 => array('red' => array(189, 230), 'green' => array(189, 230), 'blue' => array(189, 230)),
2 => array('red' => array(0, 37), 'green' => array(0, 37), 'blue' => array(0, 37)),
3 => array('red' => array(128, 255), 'green' => array(0, 76), 'blue' => array(0, 100)),
);
A series of fine-tuning is required until the various colors are clearly distinguishable.
Author: xiaodao1986