首页  >  文章  >  后端开发  >  PHP 获取图片的主色调

PHP 获取图片的主色调

WBOY
WBOY原创
2016-08-08 09:22:141336浏览

代码中用到了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教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn