PHP 获取图片的主色调

WBOY
发布: 2016-08-08 09:22:14
原创
1338 人浏览过

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

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板