Home  >  Article  >  Backend Development  >  PHP gets the main color of the image

PHP gets the main color of the image

WBOY
WBOYOriginal
2016-08-08 09:22:141330browse

The image extension of PHP is used in the code, so before using it, you need to install the Imagick extension of PHP. The specific installation is as follows (under window):
http://blog.csdn.net/shaerdong/article/details/46639245

<code><span><span><?php</span><span>$average</span> = <span>new</span> Imagick(<span>"./fruit/143511081321676_593.jpg"</span>);
<span>$average</span>->quantizeImage( <span>10</span>, Imagick::COLORSPACE_RGB, <span>0</span>, <span>false</span>, <span>false</span> );
<span>$average</span>->uniqueImageColors();
<span><span>function</span><span>GetImagesColor</span><span>( Imagick <span>$im</span> )</span>{</span><span>$colorarr</span> = <span>array</span>();
    <span>$it</span> = <span>$im</span>->getPixelIterator();
    <span>$it</span>->resetIterator();
    <span>while</span>( <span>$row</span> = <span>$it</span>->getNextIteratorRow() ){
        <span>foreach</span> ( <span>$row</span><span>as</span><span>$pixel</span> ){
            <span>$colorarr</span>[] = <span>$pixel</span>->getColor();
        }
    }
    <span>return</span><span>$colorarr</span>;
}
<span>$colorarr</span> = GetImagesColor(<span>$average</span>);
<span>foreach</span>(<span>$colorarr</span><span>as</span><span>$val</span>){
    <span>$r</span> += <span>$val</span>[<span>'r'</span>];
    <span>$g</span> += <span>$val</span>[<span>'g'</span>];
    <span>$b</span> += <span>$val</span>[<span>'b'</span>];
    <span>echo</span><span>"<divr']},{$val['g']},{$val['b']});width:50px;height:50px;float:left;'></div>"</span>;
}
<span>$r</span> = round(<span>$r</span>/<span>10</span>);
<span>$g</span> = round(<span>$g</span>/<span>10</span>);
<span>$b</span> = round(<span>$b</span>/<span>10</span>);
<span>echo</span><span>"<br><div></div>"</span>;
<span>?></span></span></span></code>

The above introduces PHP to obtain the main color of the picture, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn