侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

功能强大的PHP图片处理类(水印、透明度、旋转)

php  /  管理员 发布于 7年前   152

非常强大的php图片处理类,可以自定义图片水印、透明度、图片缩放、图片锐化、图片旋转、图片翻转、图片剪切、图片反色。

 * 图片处理函数功能:缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色
* 处理并保存历史记录的思路:当有图片有改动时自动生成一张新图片,命名方式可以考虑在原图片的基础上加上步骤,例如:图片名称+__第几步

具体代码如下:

 '没有找到相关图片!'     );     /**    * 构造函数:函数初始化    */    function __construct($PICTURE_URL){          $this -> get_info($PICTURE_URL);          }    function get_info($PICTURE_URL){     /**      * 处理原图片的信息,先检测图片是否存在,不存在则给出相应的信息      */      @$SIZE = getimagesize($PICTURE_URL);      if(!$SIZE){        exit($this -> ERROR['unalviable']);        }          // 得到原图片的信息类型、宽度、高度     $this -> PICTURE_MIME = $SIZE['mime'];      $this -> PICTURE_WIDTH = $SIZE[0];      $this -> PICTURE_HEIGHT = $SIZE[1];          // 创建图片     switch($SIZE[2]){      case 1:        $this -> PICTURE_CREATE = imagecreatefromgif($PICTURE_URL);        $this -> PICTURE_TYPE = "imagejpeg";        $this -> PICTURE_EXT = "jpg";        break;      case 2:        $this -> PICTURE_CREATE = imagecreatefromjpeg($PICTURE_URL);        $this -> PICTURE_TYPE = "imagegif";        $this -> PICTURE_EXT = "gif";        break;      case 3:        $this -> PICTURE_CREATE = imagecreatefrompng($PICTURE_URL);        $this -> PICTURE_TYPE = "imagepng";        $this -> PICTURE_EXT = "png";        break;        }         /**      * 文字颜色转换16进制转换成10进制      */      preg_match_all("/([0-f]){2,2}/i", $this -> FONT_COLOR, $MATCHES);      if(count($MATCHES) == 3){      $this -> RED = hexdec($MATCHES[0][0]);      $this -> GREEN = hexdec($MATCHES[0][1]);      $this -> BLUE = hexdec($MATCHES[0][2]);      }    }   # end of __construct /**  * 将16进制的颜色转换成10进制的(R,G,B)  */ function hex2dec(){    preg_match_all("/([0-f]){2,2}/i", $this -> FONT_COLOR, $MATCHES);    if(count($MATCHES) == 3){      $this -> RED = hexdec($MATCHES[0][0]);      $this -> GREEN = hexdec($MATCHES[0][1]);      $this -> BLUE = hexdec($MATCHES[0][2]);      }    }   // 缩放类型 function zoom_type($ZOOM_TYPE){    $this -> ZOOM = $ZOOM_TYPE;    }   // 对图片进行缩放,如果不指定高度和宽度就进行缩放 function zoom(){    // 缩放的大小   if($this -> ZOOM == 0){      $this -> ZOOM_WIDTH = $this -> PICTURE_WIDTH * $this -> ZOOM_MULTIPLE;      $this -> ZOOM_HEIGHT = $this -> PICTURE_HEIGHT * $this -> ZOOM_MULTIPLE;      }    // 新建一个真彩图象   $this -> TRUE_COLOR = imagecreatetruecolor($this -> ZOOM_WIDTH, $this -> ZOOM_HEIGHT);    $WHITE = imagecolorallocate($this -> TRUE_COLOR, 255, 255, 255);    imagefilledrectangle($this -> TRUE_COLOR, 0, 0, $this -> ZOOM_WIDTH, $this -> ZOOM_HEIGHT, $WHITE);    imagecopyresized($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> ZOOM_WIDTH, $this -> ZOOM_HEIGHT, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);    }   # end of zoom // 裁切图片,按坐标或自动 function cut(){    $this -> TRUE_COLOR = imagecreatetruecolor($this -> CUT_WIDTH, $this -> CUT_WIDTH);    imagecopy($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, 0, $this -> CUT_X, $this -> CUT_Y, $this -> CUT_WIDTH, $this -> CUT_HEIGHT);    }   # end of cut /**  * 在图片上放文字或图片  * 水印文字  */ function _mark_text(){    $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);    $this -> WORD = mb_convert_encoding($this -> FONT_WORD, 'utf-8', 'gb2312');   /**    * 取得使用 TrueType 字体的文本的范围    */    $TEMP = imagettfbbox($this -> FONT_SIZE, 0, $this -> FONT_PATH, $this -> WORD);    $WORD_LENGTH = strlen($this -> WORD);    $WORD_WIDTH = $TEMP[2] - $TEMP[6];    $WORD_HEIGHT = $TEMP[3] - $TEMP[7];   /**    * 文字水印的默认位置为右下角    */    if($this -> WORD_X == ""){      $this -> WORD_X = $this -> PICTURE_WIDTH - $WORD_WIDTH;      }    if($this -> WORD_Y == ""){      $this -> WORD_Y = $this -> PICTURE_HEIGHT - $WORD_HEIGHT;      }    imagesettile($this -> TRUE_COLOR, $this -> PICTURE_CREATE);    imagefilledrectangle($this -> TRUE_COLOR, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT, IMG_COLOR_TILED);    $TEXT2 = imagecolorallocate($this -> TRUE_COLOR, $this -> RED, $this -> GREEN, $this -> Blue);    imagettftext($this -> TRUE_COLOR, $this -> FONT_SIZE, $this -> ANGLE, $this -> WORD_X, $this -> WORD_Y, $TEXT2, $this -> FONT_PATH, $this -> WORD);    }  /**  * 水印图片  */  function _mark_picture(){     /**    * 获取水印图片的信息    */    @$SIZE = getimagesize($this -> FORCE_URL);    if(!$SIZE){      exit($this -> ERROR['unalviable']);      }    $FORCE_PICTURE_WIDTH = $SIZE[0];    $FORCE_PICTURE_HEIGHT = $SIZE[1];    // 创建水印图片   switch($SIZE[2]){    case 1:      $FORCE_PICTURE_CREATE = imagecreatefromgif($this -> FORCE_URL);      $FORCE_PICTURE_TYPE = "gif";      break;    case 2:      $FORCE_PICTURE_CREATE = imagecreatefromjpeg($this -> FORCE_URL);      $FORCE_PICTURE_TYPE = "jpg";      break;    case 3:      $FORCE_PICTURE_CREATE = imagecreatefrompng($this -> FORCE_URL);      $FORCE_PICTURE_TYPE = "png";      break;      }   /**    * 判断水印图片的大小,并生成目标图片的大小,如果水印比图片大,则生成图片大小为水印图片的大小。否则生成的图片大小为原图片大小。    */    $this -> NEW_PICTURE = $this -> PICTURE_CREATE;    if($FORCE_PICTURE_WIDTH > $this -> PICTURE_WIDTH){    $CREATE_WIDTH = $FORCE_PICTURE_WIDTH - $this -> FORCE_START_X;    }else{    $CREATE_WIDTH = $this -> PICTURE_WIDTH;    }  if($FORCE_PICTURE_HEIGHT > $this -> PICTURE_HEIGHT){    $CREATE_HEIGHT = $FORCE_PICTURE_HEIGHT - $this -> FORCE_START_Y;    }else{    $CREATE_HEIGHT = $this -> PICTURE_HEIGHT;    } /**  * 创建一个画布  */  $NEW_PICTURE_CREATE = imagecreatetruecolor($CREATE_WIDTH, $CREATE_HEIGHT);  $WHITE = imagecolorallocate($NEW_PICTURE_CREATE, 255, 255, 255); /**  * 将背景图拷贝到画布中  */  imagecopy($NEW_PICTURE_CREATE, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  /**  * 将目标图片拷贝到背景图片上  */  imagecopy($NEW_PICTURE_CREATE, $FORCE_PICTURE_CREATE, $this -> FORCE_X, $this -> FORCE_Y, $this -> FORCE_START_X, $this -> FORCE_START_Y, $FORCE_PICTURE_WIDTH, $FORCE_PICTURE_HEIGHT);  $this -> TRUE_COLOR = $NEW_PICTURE_CREATE;  }  # end of mark function alpha_(){  $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  $rgb = "#CDCDCD";  $tran_color = "#000000";  for($j = 0;$j <= $this -> PICTURE_HEIGHT-1;$j++){    for ($i = 0;$i <= $this -> PICTURE_WIDTH-1;$i++)   {      $rgb = imagecolorat($this -> PICTURE_CREATE, $i, $j);      $r = ($rgb >> 16) & 0xFF;      $g = ($rgb >> 8) & 0xFF;      $b = $rgb & 0xFF;      $now_color = imagecolorallocate($this -> PICTURE_CREATE, $r, $g, $b);      if ($now_color == $tran_color)     {        continue;        }     else       {        $color = imagecolorallocatealpha($this -> PICTURE_CREATE, $r, $g, $b, $ALPHA);        imagesetpixel($this -> PICTURE_CREATE, $ALPHA_X + $i, $ALPHA_Y + $j, $color);        }      $this -> TRUE_COLOR = $this -> PICTURE_CREATE;          }    }  }  /**  * 图片旋转:  * 沿y轴旋转  */  function turn_y(){  $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  for ($x = 0; $x < $this -> PICTURE_WIDTH; $x++) {    imagecopy($this -> TRUE_COLOR, $this -> PICTURE_CREATE, $this -> PICTURE_WIDTH - $x - 1, 0, $x, 0, 1, $this -> PICTURE_HEIGHT);    }  } /**  * 沿X轴旋转  */  function turn_x(){    $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);    for ($y = 0; $y < $this -> PICTURE_HEIGHT; $y++){      imagecopy($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, $this -> PICTURE_HEIGHT - $y - 1, 0, $y, $this -> PICTURE_WIDTH, 1);    }  }  /**  * 任意角度旋转  */  function turn(){    $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);    imageCopyResized($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);    $WHITE = imagecolorallocate($this -> TRUE_COLOR, 255, 255, 255);    $this -> TRUE_COLOR = imagerotate ($this -> TRUE_COLOR, $this -> CIRCUMROTATE, $WHITE);  } /**  * 图片锐化  */  function sharp(){    $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);    $cnt = 0;    for ($x = 0; $x < $this -> PICTURE_WIDTH; $x++){      for ($y = 0; $y < $this -> PICTURE_HEIGHT; $y++){        $src_clr1 = imagecolorsforindex($this -> TRUE_COLOR, imagecolorat($this -> PICTURE_CREATE, $x-1, $y-1));        $src_clr2 = imagecolorsforindex($this -> TRUE_COLOR, imagecolorat($this -> PICTURE_CREATE, $x, $y));        $r = intval($src_clr2["red"] + $this -> SHARP * ($src_clr2["red"] - $src_clr1["red"]));        $g = intval($src_clr2["green"] + $this -> SHARP * ($src_clr2["green"] - $src_clr1["green"]));        $b = intval($src_clr2["blue"] + $this -> SHARP * ($src_clr2["blue"] - $src_clr1["blue"]));        $r = min(255, max($r, 0));        $g = min(255, max($g, 0));        $b = min(255, max($b, 0));        if (($DST_CLR = imagecolorexact($this -> PICTURE_CREATE, $r, $g, $b)) == -1)          $DST_CLR = imagecolorallocate($this -> PICTURE_CREATE, $r, $g, $b);        $cnt++;        if ($DST_CLR == -1) die("color allocate faile at $x, $y ($cnt).");        imagesetpixel($this -> TRUE_COLOR, $x, $y, $DST_CLR);     }   }  } /**  * 将图片反色处理??  */  function return_color(){ /**  * 创建一个画布  */  $NEW_PICTURE_CREATE = imagecreate($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  $WHITE = imagecolorallocate($NEW_PICTURE_CREATE, 255, 255, 255); /**  * 将背景图拷贝到画布中  */  imagecopy($NEW_PICTURE_CREATE, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  $this -> TRUE_COLOR = $NEW_PICTURE_CREATE;  }  /**  * 生成目标图片并显示  */  function show(){  // 判断浏览器,若是IE就不发送头 if(isset($_SERVER['HTTP_USER_AGENT']))   {    $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);    if(!preg_match('/^.*MSIE.*\)$/i', $ua))     {      header("Content-type:$this->PICTURE_MIME");      }    }  $OUT = $this -> PICTURE_TYPE;  $OUT($this -> TRUE_COLOR);  }  /**  * 生成目标图片并保存  */  function save_picture(){  // 以 JPEG 格式将图像输出到浏览器或文件 $OUT = $this -> PICTURE_TYPE;  if(function_exists($OUT)){    // 判断浏览器,若是IE就不发送头   if(isset($_SERVER['HTTP_USER_AGENT']))     {      $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);      if(!preg_match('/^.*MSIE.*\)$/i', $ua))       {        header("Content-type:$this->PICTURE_MIME");        }      }    if(!$this -> TRUE_COLOR){      exit($this -> ERROR['unavilable']);      }else{      $OUT($this -> TRUE_COLOR, $this -> DEST_URL);      $OUT($this -> TRUE_COLOR);      }    }  } /**  * 析构函数:释放图片  */  function __destruct(){ /**  * 释放图片  */  imagedestroy($this -> TRUE_COLOR);  imagedestroy($this -> PICTURE_CREATE);  }  # end of class } ?> 

这就是非常强大的php图片处理类,好好收藏,亲,相信以后一定会派上用场的。

您可能感兴趣的文章:

  • ThinkPHP水印功能实现修复PNG透明水印并增加JPEG图片质量可调整
  • PHP实现可添加水印与生成缩略图的图片处理工具类
  • PHP图片水印类的封装
  • PHP实现图片的等比缩放和Logo水印功能示例
  • php利用gd库为图片添加水印
  • PHP图片添加水印功能示例小结
  • php实现图片上传时添加文字和图片水印技巧
  • PHP Imagick完美实现图片裁切、生成缩略图、添加水印
  • 用来给图片加水印的PHP类
  • php给图片添加文字水印方法汇总
  • PHP添加PNG图片背景透明水印操作类定义与用法示例


  • 上一条:
    为你总结一些php系统类函数
    下一条:
    使用xampp搭建运行php虚拟主机的详细步骤
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客