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


Posted in PHP onOctober 21, 2015

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

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

具体代码如下:

<?php 
class picture{ 
  var $PICTURE_URL; //要处理的图片 
   var $DEST_URL = "temp__01.jpg"; //生成目标图片位置 
   var $PICTURE_CREATE; //要创建的图片 
   var $TURE_COLOR; //新建一个真彩图象 
  
  
   var $PICTURE_WIDTH; //原图片宽度 
   var $PICTURE_HEIGHT; //原图片高度 
  
  
  /** 
   * 水印的类型,默认的为水印文字 
   */ 
   var $MARK_TYPE = 1; 
   var $WORD; //经过UTF-8后的文字 
   var $WORD_X; //文字横坐标 
   var $WORD_Y; //文字纵坐标 
   var $FONT_TYPE; //字体类型 
   var $FONT_SIZE = "12"; //字体大小 
   var $FONT_WORD; //文字 
   var $ANGLE = 0; //文字的角度,默认为0 
   var $FONT_COLOR = "#000000"; //文字颜色 
   var $FONT_PATH = "font/simkai.ttf"; //字体库,默认为宋体 
   var $FORCE_URL; //水印图片 
   var $FORCE_X = 0; //水印横坐标 
   var $FORCE_Y = 0; //水印纵坐标 
   var $FORCE_START_X = 0; //切起水印的图片横坐标 
   var $FORCE_START_Y = 0; //切起水印的图片纵坐标 
  
  
   var $PICTURE_TYPE; //图片类型 
   var $PICTURE_MIME; //输出的头部 
  
  
  /** 
   * 缩放比例为1的话就按缩放高度和宽度缩放 
   */ 
   var $ZOOM = 1; //缩放类型 
   var $ZOOM_MULTIPLE; //缩放比例 
   var $ZOOM_WIDTH; //缩放宽度 
   var $ZOOM_HEIGHT; //缩放高度 
  
  
  /** 
   * 裁切,按比例和固定长度、宽度 
   */ 
   var $CUT_TYPE = 1; //裁切类型 
   var $CUT_X = 0; //裁切的横坐标 
   var $CUT_Y = 0; //裁切的纵坐标 
   var $CUT_WIDTH = 100; //裁切的宽度 
   var $CUT_HEIGHT = 100; //裁切的高度 
  
  
  /** 
   * 锐化 
   */ 
   var $SHARP = "7.0"; //锐化程度 
  
  
  /** 
   * 透明度处理 
   */ 
   var $ALPHA = '100'; //透明度在0-127之间 
   var $ALPHA_X = "90"; 
   var $ALPHA_Y = "50"; 
  
  /** 
   * 任意角度旋转 
   */ 
   var $CIRCUMROTATE = "90.0"; //注意,必须为浮点数 
  
  
  /** 
   * 出错信息 
   */ 
   var $ERROR = array( 
    'unalviable' => '没有找到相关图片!' 
    ); 
  
  /** 
   * 构造函数:函数初始化 
   */ 
   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图片处理类,好好收藏,亲,相信以后一定会派上用场的。

PHP 相关文章推荐
php使用Smarty的相关注意事项及访问变量的几种方式
Dec 08 PHP
将酷狗krc歌词解析并转换为lrc歌词php源码
Jun 20 PHP
PHP图片库imagemagick安装方法
Sep 23 PHP
PHP生成网站桌面快捷方式代码分享
Oct 11 PHP
一个经典的PHP文件上传类分享
Nov 18 PHP
php最简单的删除目录与文件实现方法
Nov 28 PHP
10个超级有用值得收藏的PHP代码片段
Jan 22 PHP
php 浮点数比较方法详解
May 05 PHP
php使用ftp实现文件上传与下载功能
Jul 21 PHP
thinkphp5 migrate数据库迁移工具
Feb 20 PHP
JSON PHP中,Json字符串反序列化成对象/数组的方法
May 31 PHP
php设计模式之正面模式实例分析【星际争霸游戏案例】
Mar 24 PHP
使用xampp搭建运行php虚拟主机的详细步骤
Oct 21 #PHP
php获取汉字拼音首字母的方法
Oct 21 #PHP
PHP中使用substr()截取字符串出现中文乱码问题该怎么办
Oct 21 #PHP
PHP中的switch语句的用法实例详解
Oct 21 #PHP
PHP 实现的将图片转换为TXT
Oct 21 #PHP
PHP实现清除wordpress里恶意代码
Oct 21 #PHP
表单提交错误后返回内容消失问题的解决方法(PHP网站)
Oct 20 #PHP
You might like
全国FM电台频率大全 - 31 新疆维吾尔族自治区
2020/03/11 无线电
PHP面向对象程序设计方法实例详解
2016/12/24 PHP
在Laravel5.6中使用Swoole的协程数据库查询
2018/06/15 PHP
Thinkphp 5.0实现微信企业付款到零钱
2018/09/30 PHP
php使用curl模拟浏览器表单上传文件或者图片的方法
2018/11/10 PHP
JavaScript操作cookie类实例
2015/03/31 Javascript
Adapter适配器模式在JavaScript设计模式编程中的运用分析
2016/05/18 Javascript
javascript实现的上下无缝滚动效果
2016/09/19 Javascript
jQuery插件扩展实例【添加回调函数】
2016/11/26 Javascript
JavaScript轻松创建级联函数的方法示例
2017/02/10 Javascript
判断div滑动到底部的scroll实例代码
2017/11/15 Javascript
微信小程序中使用ECharts 异步加载数据的方法
2018/06/27 Javascript
微信小程序全局变量的设置、使用、修改过程解析
2019/09/24 Javascript
JS代码优化的8点建议
2020/02/04 Javascript
Javascript节流函数throttle和防抖函数debounce
2020/12/03 Javascript
Python实现扫描局域网活动ip(扫描在线电脑)
2015/04/28 Python
Python下调用Linux的Shell命令的方法
2018/06/12 Python
python pandas读取csv后,获取列标签的方法
2018/11/12 Python
python+opencv 读取文件夹下的所有图像并批量保存ROI的方法
2019/01/10 Python
最小二乘法及其python实现详解
2020/02/24 Python
Pycharm中如何关掉python console
2020/10/27 Python
利用简洁的图片预加载组件提升html5移动页面的用户体验
2016/03/11 HTML / CSS
DKNY品牌官网:纽约大都会时尚风格
2016/10/20 全球购物
Vans荷兰官方网站:美国南加州的原创极限运动潮牌
2018/01/23 全球购物
Nordgreen美国官网:在线购买极简主义斯堪的纳维亚手表
2019/07/24 全球购物
Antonioli美国在线商店:时尚前卫奢华
2019/07/29 全球购物
马来西亚排名第一的宠物用品店:Pets Wonderland
2020/04/16 全球购物
高中自我鉴定范文
2013/11/03 职场文书
表扬信格式
2014/01/12 职场文书
教师节促销方案
2014/03/22 职场文书
农村文化建设标语
2014/10/07 职场文书
党的群众路线教育实践活动个人对照检查材料(医生)
2014/11/05 职场文书
让生命充满爱观后感
2015/06/08 职场文书
2016年主题党日活动总结
2016/04/05 职场文书
祝福语集锦:送给毕业同学祝福语
2019/11/21 职场文书
oracle delete误删除表数据后如何恢复
2022/06/28 Oracle