Maison  >  Article  >  développement back-end  >  PHP 获取图片的主色调

PHP 获取图片的主色调

WBOY
WBOYoriginal
2016-08-08 09:22:141330parcourir

代码中用到了php的图片扩展,所以使用之前,需要先安装PHP的Imagick扩展,具体安装如下(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>"</divr></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>

以上就介绍了PHP 获取图片的主色调,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn