程式碼中用到了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教程有興趣的朋友有所幫助。