当前位置:Gxlcms > PHP教程 > php获取图片平均颜色值的二种方法

php获取图片平均颜色值的二种方法

时间:2021-07-01 10:21:17 帮助过:45人阅读

php获取图片平均颜色值的二种方法 :

  1. $i=imagecreatefromjpeg("jbxue.jpg");
  2. for($x=0;$x<imagesx($i);$x++){
  3. for($y=0;$y<imagesy($i);$y++){
  4. $rgb=imagecolorat($i,$x,$y);
  5. $r=($rgb>>16)&0xff;
  6. $g=($rgb>>4)&0xff;
  7. $b=$rgb&0xff;
  8. $rTotal+=$r;
  9. $gToal+=$g;
  10. $bToal+=$b;
  11. $total++;
  12. }
  13. }
  14. $rAverage=round($rTotal/$total);
  15. $gAverage=round($gTotal/$total);
  16. $bAverage=round($bTotal/$total);

例2,获取图片颜色的值。

  1. <?php
  2. /*
  3. *文件:image_get_point.php
  4. *功能:获取图片指定某点的颜色值
  5. *整理:bbs.it-home.org
  6. */
  7. function dec2hex($dec)
  8. {
  9. return strtoupper($dec>15?dechex($dec):('0'.dechex($dec)));
  10. }
  11. $im = imagecreatefrompng('http://localhost/image_arc.php');
  12. $rgb = imagecolorat($im,20,20);
  13. $r = ($rgb >> 16) & 0xFF;
  14. $g = ($rgb >> 8) & 0xFF;
  15. $b = $rgb & 0xFF;
  16. $RGB = dec2hex($r).dec2hex($g).dec2hex($b);
  17. echo "dec:$r-$g-$b<br />hex:#$RGB";
  18. ?>

以上就是php获取图片平均颜色值的二种方法 的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!

人气教程排行