• php对图片反色处理


    今天有个需求用php对图片进行反色,和转灰,之前不知道可不可行,后来看到了imagefilter()函数,用来转灰绰绰有余,好强大;
    
    imagefilter($im, IMG_FILTER_GRAYSCALE)
    
    当然也有人在css里面设置变灰
    
        <style type="text/css">
        img { 
        -webkit-filter: grayscale(1);/* Webkit */ 
        filter:gray;/* IE6-9 */ 
        filter: grayscale(1);/* W3C */ 
        } 
        </style>
    
    
    php转色代码:
    
        <?php
        /**
        * 主要用于图片的处理函数
        */
        //图片的反色功能
        function color($url) {
        //获取图片的信息
          list($width, $height, $type, $attr)= getimagesize($url);
          $imagetype = strtolower(image_type_to_extension($type,false));
          $fun = 'imagecreatefrom'.($imagetype == 'jpg'?'jpeg':$imagetype);
          $img = $fun($url);
          for ($y=0; $y < $height; $y++) { 
              for ($x=0; $x <$width; $x++) { 
                //获取颜色的所以值
                $index = imagecolorat($img, $x, $y);
                //获取颜色的数组
                $color = imagecolorsforindex($img, $index);
                //颜色值的反转
                $red = 256 - $color['red'];
                $green = 256 - $color['green'];
                $blue = 256 - $color['blue'];
                $hex = imagecolorallocate($img, $red, $green, $blue);
                //给每一个像素分配颜色值
                imagesetpixel($img, $x, $y, $hex);
            }
          }
          //输出图片
          switch ($imagetype) {
              case 'gif':
              imagegif($img);
              break;
            case 'jpeg':
              imagejpeg($img);
              break;
            case 'png':
              imagepng($img);
              break;
            default:
              break;
          }
      }
  • 相关阅读:
    MySQL Show Processlist 整理
    由linkedin数据库泄漏引发的思考[转发]
    MySQL 严重 Bug 用户登陆漏洞[转发]
    关于博客的开始
    Linux共享库位置配置
    getopt详解
    git使用
    ArrayList读源码心得
    HashMap内部实现机制及优化第一篇
    如何才能做一个好网站系列 第三篇 最初的思路
  • 原文地址:https://www.cnblogs.com/pengcz/p/5646594.html
Copyright © 2020-2023  润新知