php获取图片RGB颜色值的例子,
php获取图片RGB颜色值的例子
很多图片站点都会根据用户上传的图片检索出图片的主要颜色值,然后在通过颜色搜索相关的图片。
之前按照网上的方法将图片缩放(或者马赛克)然后遍历每个像素点,然后统计处RGB次数最多的值,这做法效率太低而且取到的RGB值不够精确。之后才发现使用Imagick的quantizeImage方法能够很方便的取到图片中平均的RGB值.
<span>$average</span> = <span>new</span> Imagick("xiaocai.jpg"<span>); </span><span>$average</span>->quantizeImage( 10, Imagick::COLORSPACE_RGB, 0, <span>false</span>, <span>false</span><span> ); </span><span>$average</span>-><span>uniqueImageColors(); </span><span>function</span> GetImagesColor( Imagick <span>$im</span><span> ){ </span><span>$colorarr</span> = <span>array</span><span>(); </span><span>$it</span> = <span>$im</span>-><span>getPixelIterator(); </span><span>$it</span>-><span>resetIterator(); </span><span>while</span>( <span>$row</span> = <span>$it</span>-><span>getNextIteratorRow() ){ </span><span>foreach</span> ( <span>$row</span> <span>as</span> <span>$pixel</span><span> ){ // www.jbxue.com </span><span>$colorarr</span>[] = <span>$pixel</span>-><span>getColor(); } } </span><span>return</span> <span>$colorarr</span><span>; } </span><span>$colorarr</span> = GetImagesColor(<span>$average</span><span>); </span><span>foreach</span>(<span>$colorarr</span> <span>as</span> <span>$val</span><span>){ </span><span>echo</span> "<div style='background-color: rgb({<span>$val</span>['r']},{<span>$val</span>['g']},{<span>$val</span>['b']});width:50px;height:50px;float:left;'></div>"<span>; }</span><span> </span>
您可能感兴趣的文章:
- php扫描图片像素获取每像素颜色值并输出
- php获取图片颜色值有哪些方法?
- php获取图片颜色值的例子
- php颜色值转换 php实现16进制数字与颜色值的转换

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool