首頁 > 後端開發 > php教程 > PHP 取得圖片的主色調

PHP 取得圖片的主色調

WBOY
發布: 2016-08-08 09:22:14
原創
1356 人瀏覽過

程式碼中用到了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']},{$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>
登入後複製

以上就介紹了PHP 獲取圖片的主色調,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板