隐藏

强大的PHP 图片处理类(水印、透明度、缩放、锐化、旋转、翻转、剪切、反色)

发布:2017/5/10 20:46:12作者:管理员 来源:本站 浏览次数:815

  1. <?php  
  2. /** 
  3.  * 图片处理函数功能:缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色 
  4.  * 处理并保存历史记录的思路:当有图片有改动时自动生成一张新图片,命名方式可以考虑在原图片的基础上加上步骤,例如:图片名称+__第几步 
  5.  * 网址:http://www.phpernote.com 
  6.  */  
  7. class picture{  
  8.      var $PICTURE_URL//要处理的图片  
  9.      var $DEST_URL = "temp__01.jpg"//生成目标图片位置  
  10.      var $PICTURE_CREATE//要创建的图片  
  11.      var $TURE_COLOR//新建一个真彩图象  
  12.      
  13.      
  14.      var $PICTURE_WIDTH//原图片宽度  
  15.      var $PICTURE_HEIGHT//原图片高度  
  16.      
  17.      
  18.     /** 
  19.      * 水印的类型,默认的为水印文字 
  20.      */  
  21.      var $MARK_TYPE = 1;  
  22.      var $WORD//经过UTF-8后的文字  
  23.      var $WORD_X//文字横坐标  
  24.      var $WORD_Y//文字纵坐标  
  25.      var $FONT_TYPE//字体类型  
  26.      var $FONT_SIZE = "12"//字体大小  
  27.      var $FONT_WORD//文字  
  28.      var $ANGLE = 0; //文字的角度,默认为0  
  29.      var $FONT_COLOR = "#000000"//文字颜色  
  30.      var $FONT_PATH = "font/simkai.ttf"//字体库,默认为宋体  
  31.      var $FORCE_URL//水印图片  
  32.      var $FORCE_X = 0; //水印横坐标  
  33.      var $FORCE_Y = 0; //水印纵坐标  
  34.      var $FORCE_START_X = 0; //切起水印的图片横坐标  
  35.      var $FORCE_START_Y = 0; //切起水印的图片纵坐标  
  36.      
  37.      
  38.      var $PICTURE_TYPE//图片类型  
  39.      var $PICTURE_MIME//输出的头部  
  40.      
  41.      
  42.     /** 
  43.      * 缩放比例为1的话就按缩放高度和宽度缩放 
  44.      */  
  45.      var $ZOOM = 1; //缩放类型  
  46.      var $ZOOM_MULTIPLE//缩放比例  
  47.      var $ZOOM_WIDTH//缩放宽度  
  48.      var $ZOOM_HEIGHT//缩放高度  
  49.      
  50.      
  51.     /** 
  52.      * 裁切,按比例和固定长度、宽度 
  53.      */  
  54.      var $CUT_TYPE = 1; //裁切类型  
  55.      var $CUT_X = 0; //裁切的横坐标  
  56.      var $CUT_Y = 0; //裁切的纵坐标  
  57.      var $CUT_WIDTH = 100; //裁切的宽度  
  58.      var $CUT_HEIGHT = 100; //裁切的高度  
  59.      
  60.      
  61.     /** 
  62.      * 锐化 
  63.      */  
  64.      var $SHARP = "7.0"//锐化程度  
  65.      
  66.      
  67.     /** 
  68.      * 透明度处理 
  69.      */  
  70.      var $ALPHA = '100'//透明度在0-127之间  
  71.      var $ALPHA_X = "90";  
  72.      var $ALPHA_Y = "50";  
  73.      
  74.     /** 
  75.      * 任意角度旋转 
  76.      */  
  77.      var $CIRCUMROTATE = "90.0"//注意,必须为浮点数  
  78.      
  79.      
  80.     /** 
  81.      * 出错信息 
  82.      */  
  83.      var $ERROR = array(  
  84.         'unalviable' => '没有找到相关图片!'  
  85.         );  
  86.      
  87.     /** 
  88.      * 构造函数:函数初始化 
  89.      */  
  90.      function __construct($PICTURE_URL){  
  91.          
  92.          $this -> get_info($PICTURE_URL);  
  93.          
  94.          }  
  95.      function get_info($PICTURE_URL){  
  96.         /** 
  97.          * 处理原图片的信息,先检测图片是否存在,不存在则给出相应的信息 
  98.          */  
  99.          @$SIZE = getimagesize($PICTURE_URL);  
  100.          if(!$SIZE){  
  101.              exit($this -> ERROR['unalviable']);  
  102.              }  
  103.          
  104.          // 得到原图片的信息类型、宽度、高度  
  105.         $this -> PICTURE_MIME = $SIZE['mime'];  
  106.          $this -> PICTURE_WIDTH = $SIZE[0];  
  107.          $this -> PICTURE_HEIGHT = $SIZE[1];  
  108.          
  109.          // 创建图片  
  110.         switch($SIZE[2]){  
  111.          case 1:  
  112.              $this -> PICTURE_CREATE = imagecreatefromgif($PICTURE_URL);  
  113.              $this -> PICTURE_TYPE = "imagejpeg";  
  114.              $this -> PICTURE_EXT = "jpg";  
  115.              break;  
  116.          case 2:  
  117.              $this -> PICTURE_CREATE = imagecreatefromjpeg($PICTURE_URL);  
  118.              $this -> PICTURE_TYPE = "imagegif";  
  119.              $this -> PICTURE_EXT = "gif";  
  120.              break;  
  121.          case 3:  
  122.              $this -> PICTURE_CREATE = imagecreatefrompng($PICTURE_URL);  
  123.              $this -> PICTURE_TYPE = "imagepng";  
  124.              $this -> PICTURE_EXT = "png";  
  125.              break;  
  126.              }  
  127.          
  128.         /** 
  129.          * 文字颜色转换16进制转换成10进制 
  130.          */  
  131.          preg_match_all("/([0-f]){2,2}/i"$this -> FONT_COLOR, $MATCHES);  
  132.          if(count($MATCHES) == 3){  
  133.          $this -> RED = hexdec($MATCHES[0][0]);  
  134.          $this -> GREEN = hexdec($MATCHES[0][1]);  
  135.          $this -> BLUE = hexdec($MATCHES[0][2]);  
  136.          }  
  137.      }  
  138.   
  139.  # end of __construct  
  140. /** 
  141.  * 将16进制的颜色转换成10进制的(R,G,B) 
  142.  */  
  143. function hex2dec(){  
  144.      preg_match_all("/([0-f]){2,2}/i"$this -> FONT_COLOR, $MATCHES);  
  145.      if(count($MATCHES) == 3){  
  146.          $this -> RED = hexdec($MATCHES[0][0]);  
  147.          $this -> GREEN = hexdec($MATCHES[0][1]);  
  148.          $this -> BLUE = hexdec($MATCHES[0][2]);  
  149.          }  
  150.      }  
  151.   
  152.  // 缩放类型  
  153. function zoom_type($ZOOM_TYPE){  
  154.      $this -> ZOOM = $ZOOM_TYPE;  
  155.      }  
  156.   
  157.  // 对图片进行缩放,如果不指定高度和宽度就进行缩放  
  158. function zoom(){  
  159.      // 缩放的大小  
  160.     if($this -> ZOOM == 0){  
  161.          $this -> ZOOM_WIDTH = $this -> PICTURE_WIDTH * $this -> ZOOM_MULTIPLE;  
  162.          $this -> ZOOM_HEIGHT = $this -> PICTURE_HEIGHT * $this -> ZOOM_MULTIPLE;  
  163.          }  
  164.      // 新建一个真彩图象  
  165.     $this -> TRUE_COLOR = imagecreatetruecolor($this -> ZOOM_WIDTH, $this -> ZOOM_HEIGHT);  
  166.      $WHITE = imagecolorallocate($this -> TRUE_COLOR, 255, 255, 255);  
  167.      imagefilledrectangle($this -> TRUE_COLOR, 0, 0, $this -> ZOOM_WIDTH, $this -> ZOOM_HEIGHT, $WHITE);  
  168.      imagecopyresized($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> ZOOM_WIDTH, $this -> ZOOM_HEIGHT, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  
  169.      }  
  170.   
  171.  # end of zoom  
  172. // 裁切图片,按坐标或自动  
  173. function cut(){  
  174.      $this -> TRUE_COLOR = imagecreatetruecolor($this -> CUT_WIDTH, $this -> CUT_WIDTH);  
  175.      imagecopy($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, 0, $this -> CUT_X, $this -> CUT_Y, $this -> CUT_WIDTH, $this -> CUT_HEIGHT);  
  176.      }  
  177.   
  178.  # end of cut  
  179. /** 
  180.  * 在图片上放文字或图片 
  181.  * 水印文字 
  182.  */  
  183. function _mark_text(){  
  184.      $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  
  185.      $this -> WORD = mb_convert_encoding($this -> FONT_WORD, 'utf-8''gb2312');  
  186.     /** 
  187.      * 取得使用 TrueType 字体的文本的范围 
  188.      */  
  189.      $TEMP = imagettfbbox($this -> FONT_SIZE, 0, $this -> FONT_PATH, $this -> WORD);  
  190.      $WORD_LENGTH = strlen($this -> WORD);  
  191.      $WORD_WIDTH = $TEMP[2] - $TEMP[6];  
  192.      $WORD_HEIGHT = $TEMP[3] - $TEMP[7];  
  193.     /** 
  194.      * 文字水印的默认位置为右下角 
  195.      */  
  196.      if($this -> WORD_X == ""){  
  197.          $this -> WORD_X = $this -> PICTURE_WIDTH - $WORD_WIDTH;  
  198.          }  
  199.      if($this -> WORD_Y == ""){  
  200.          $this -> WORD_Y = $this -> PICTURE_HEIGHT - $WORD_HEIGHT;  
  201.          }  
  202.      imagesettile($this -> TRUE_COLOR, $this -> PICTURE_CREATE);  
  203.      imagefilledrectangle($this -> TRUE_COLOR, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT, IMG_COLOR_TILED);  
  204.      $TEXT2 = imagecolorallocate($this -> TRUE_COLOR, $this -> RED, $this -> GREEN, $this -> Blue);  
  205.      imagettftext($this -> TRUE_COLOR, $this -> FONT_SIZE, $this -> ANGLE, $this -> WORD_X, $this -> WORD_Y, $TEXT2$this -> FONT_PATH, $this -> WORD);  
  206.      }  
  207.   
  208. /** 
  209.  * 水印图片 
  210.  */  
  211.  function _mark_picture(){  
  212.      
  213.     /** 
  214.      * 获取水印图片的信息 
  215.      */  
  216.      @$SIZE = getimagesize($this -> FORCE_URL);  
  217.      if(!$SIZE){  
  218.          exit($this -> ERROR['unalviable']);  
  219.          }  
  220.      $FORCE_PICTURE_WIDTH = $SIZE[0];  
  221.      $FORCE_PICTURE_HEIGHT = $SIZE[1];  
  222.      // 创建水印图片  
  223.     switch($SIZE[2]){  
  224.      case 1:  
  225.          $FORCE_PICTURE_CREATE = imagecreatefromgif($this -> FORCE_URL);  
  226.          $FORCE_PICTURE_TYPE = "gif";  
  227.          break;  
  228.      case 2:  
  229.          $FORCE_PICTURE_CREATE = imagecreatefromjpeg($this -> FORCE_URL);  
  230.          $FORCE_PICTURE_TYPE = "jpg";  
  231.          break;  
  232.      case 3:  
  233.          $FORCE_PICTURE_CREATE = imagecreatefrompng($this -> FORCE_URL);  
  234.          $FORCE_PICTURE_TYPE = "png";  
  235.          break;  
  236.          }  
  237.     /** 
  238.      * 判断水印图片的大小,并生成目标图片的大小,如果水印比图片大,则生成图片大小为水印图片的大小。否则生成的图片大小为原图片大小。 
  239.      */  
  240.      $this -> NEW_PICTURE = $this -> PICTURE_CREATE;  
  241.      if($FORCE_PICTURE_WIDTH > $this -> PICTURE_WIDTH){  
  242.      $CREATE_WIDTH = $FORCE_PICTURE_WIDTH - $this -> FORCE_START_X;  
  243.      }else{  
  244.      $CREATE_WIDTH = $this -> PICTURE_WIDTH;  
  245.      }  
  246.  if($FORCE_PICTURE_HEIGHT > $this -> PICTURE_HEIGHT){  
  247.      $CREATE_HEIGHT = $FORCE_PICTURE_HEIGHT - $this -> FORCE_START_Y;  
  248.      }else{  
  249.      $CREATE_HEIGHT = $this -> PICTURE_HEIGHT;  
  250.      }  
  251. /** 
  252.  * 创建一个画布 
  253.  */  
  254.  $NEW_PICTURE_CREATE = imagecreatetruecolor($CREATE_WIDTH$CREATE_HEIGHT);  
  255.  $WHITE = imagecolorallocate($NEW_PICTURE_CREATE, 255, 255, 255);  
  256. /** 
  257.  * 将背景图拷贝到画布中 
  258.  */  
  259.  imagecopy($NEW_PICTURE_CREATE$this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  
  260.   
  261. /** 
  262.  * 将目标图片拷贝到背景图片上 
  263.  */  
  264.  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);  
  265.  $this -> TRUE_COLOR = $NEW_PICTURE_CREATE;  
  266.  }  
  267.  # end of mark  
  268. function alpha_(){  
  269.  $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  
  270.  $rgb = "#CDCDCD";  
  271.  $tran_color = "#000000";  
  272.  for($j = 0;$j <= $this -> PICTURE_HEIGHT-1;$j++){  
  273.      for ($i = 0;$i <= $this -> PICTURE_WIDTH-1;$i++)  
  274.     {  
  275.          $rgb = imagecolorat($this -> PICTURE_CREATE, $i$j);  
  276.          $r = ($rgb >> 16) & 0xFF;  
  277.          $g = ($rgb >> 8) & 0xFF;  
  278.          $b = $rgb & 0xFF;  
  279.          $now_color = imagecolorallocate($this -> PICTURE_CREATE, $r$g$b);  
  280.          if ($now_color == $tran_color)  
  281.         {  
  282.              continue;  
  283.              }  
  284.         else  
  285.             {  
  286.              $color = imagecolorallocatealpha($this -> PICTURE_CREATE, $r$g$b$ALPHA);  
  287.              imagesetpixel($this -> PICTURE_CREATE, $ALPHA_X + $i$ALPHA_Y + $j$color);  
  288.              }  
  289.          $this -> TRUE_COLOR = $this -> PICTURE_CREATE;  
  290.          
  291.          }  
  292.      }  
  293.  }  
  294.   
  295. /** 
  296.  * 图片旋转: 
  297.  * 沿y轴旋转 
  298.  */  
  299.  function turn_y(){  
  300.  $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  
  301.  for ($x = 0; $x < $this -> PICTURE_WIDTH; $x++)  
  302. {  
  303.      imagecopy($this -> TRUE_COLOR, $this -> PICTURE_CREATE, $this -> PICTURE_WIDTH - $x - 1, 0, $x, 0, 1, $this -> PICTURE_HEIGHT);  
  304.      }  
  305.  }  
  306. /** 
  307.  * 沿X轴旋转 
  308.  */  
  309.  function turn_x(){  
  310.      $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  
  311.      for ($y = 0; $y < $this -> PICTURE_HEIGHT; $y++){  
  312.          imagecopy($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, $this -> PICTURE_HEIGHT - $y - 1, 0, $y$this -> PICTURE_WIDTH, 1);  
  313.      }  
  314.  }  
  315.   
  316. /** 
  317.  * 任意角度旋转 
  318.  */  
  319.  function turn(){  
  320.      $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  
  321.      imageCopyResized($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  
  322.      $WHITE = imagecolorallocate($this -> TRUE_COLOR, 255, 255, 255);  
  323.      $this -> TRUE_COLOR = imagerotate ($this -> TRUE_COLOR, $this -> CIRCUMROTATE, $WHITE);  
  324.  }  
  325. /** 
  326.  * 图片锐化 
  327.  */  
  328.  function sharp(){  
  329.      $this -> TRUE_COLOR = imagecreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  
  330.      $cnt = 0;  
  331.      for ($x = 0; $x < $this -> PICTURE_WIDTH; $x++){  
  332.          for ($y = 0; $y < $this -> PICTURE_HEIGHT; $y++){  
  333.              $src_clr1 = imagecolorsforindex($this -> TRUE_COLOR, imagecolorat($this -> PICTURE_CREATE, $x-1, $y-1));  
  334.              $src_clr2 = imagecolorsforindex($this -> TRUE_COLOR, imagecolorat($this -> PICTURE_CREATE, $x$y));  
  335.              $r = intval($src_clr2["red"] + $this -> SHARP * ($src_clr2["red"] - $src_clr1["red"]));  
  336.              $g = intval($src_clr2["green"] + $this -> SHARP * ($src_clr2["green"] - $src_clr1["green"]));  
  337.              $b = intval($src_clr2["blue"] + $this -> SHARP * ($src_clr2["blue"] - $src_clr1["blue"]));  
  338.              $r = min(255, max($r, 0));  
  339.              $g = min(255, max($g, 0));  
  340.              $b = min(255, max($b, 0));  
  341.              if (($DST_CLR = imagecolorexact($this -> PICTURE_CREATE, $r$g$b)) == -1)  
  342.                  $DST_CLR = imagecolorallocate($this -> PICTURE_CREATE, $r$g$b);  
  343.              $cnt++;  
  344.              if ($DST_CLR == -1) die("color  allocate  faile  at  $x,  $y  ($cnt).");  
  345.              imagesetpixel($this -> TRUE_COLOR, $x$y$DST_CLR);  
  346.         }  
  347.     }  
  348.  }  
  349. /** 
  350.  * 将图片反色处理?? 
  351.  */  
  352.  function return_color(){  
  353. /** 
  354.  * 创建一个画布 
  355.  */  
  356.  $NEW_PICTURE_CREATE = imagecreate($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  
  357.  $WHITE = imagecolorallocate($NEW_PICTURE_CREATE, 255, 255, 255);  
  358. /** 
  359.  * 将背景图拷贝到画布中 
  360.  */  
  361.  imagecopy($NEW_PICTURE_CREATE$this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT);  
  362.  $this -> TRUE_COLOR = $NEW_PICTURE_CREATE;  
  363.  }  
  364.   
  365. /** 
  366.  * 生成目标图片并显示 
  367.  */  
  368.  function show(){  
  369.  // 判断浏览器,若是IE就不发送头  
  370. if(isset($_SERVER['HTTP_USER_AGENT']))  
  371.     {  
  372.      $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);  
  373.      if(!preg_match('/^.*MSIE.*\)$/i'$ua))  
  374.         {  
  375.          header("Content-type:$this->PICTURE_MIME");  
  376.          }  
  377.      }  
  378.  $OUT = $this -> PICTURE_TYPE;  
  379.  $OUT($this -> TRUE_COLOR);  
  380.  }  
  381.   
  382. /** 
  383.  * 生成目标图片并保存 
  384.  */  
  385.  function save_picture(){  
  386.  // 以 JPEG 格式将图像输出到浏览器或文件  
  387. $OUT = $this -> PICTURE_TYPE;  
  388.  if(function_exists($OUT)){  
  389.      // 判断浏览器,若是IE就不发送头  
  390.     if(isset($_SERVER['HTTP_USER_AGENT']))  
  391.         {  
  392.          $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);  
  393.          if(!preg_match('/^.*MSIE.*\)$/i'$ua))  
  394.             {  
  395.              header("Content-type:$this->PICTURE_MIME");  
  396.              }  
  397.          }  
  398.      if(!$this -> TRUE_COLOR){  
  399.          exit($this -> ERROR['unavilable']);  
  400.          }else{  
  401.          $OUT($this -> TRUE_COLOR, $this -> DEST_URL);  
  402.          $OUT($this -> TRUE_COLOR);  
  403.          }  
  404.      }  
  405.  }  
  406. /** 
  407.  * 析构函数:释放图片 
  408.  */  
  409.  function __destruct(){  
  410. /** 
  411.  * 释放图片 
  412.  */  
  413.  imagedestroy($this -> TRUE_COLOR);  
  414.  imagedestroy($this -> PICTURE_CREATE);  
  415.  }  
  416.  # end of class  
  417. }  
  418. ?>