[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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
