图片处理类(剪裁、缩放、水印)

  1. <?php  
  2. /**  
  3. * Image Class  
  4. * Driver: GD Library  
  5. * 暂时未处理当缩略图比原图片小时 无法添加水印的情况。  
  6. */  
  7. class Image {  
  8.   
  9.     public  $_sourceImg ; // 源图片  
  10.     public  $_tmpImg;     // 操作图片  
  11.     public  $_imgType ;   // 图片后缀  
  12.     public  $_create;     // 执行方法  
  13.     public  $_save;       // 保存方法  
  14.   
  15.     public function __construct($image) {  
  16.   
  17.         if(is_file($image)){  
  18.             @chmod($image, 0777);  
  19.             $this->_sourceImg = $image;  
  20.             $this->_imgType   = pathinfo($image, PATHINFO_EXTENSION);  
  21.             $this->_tmpImg    = dirname($image).'/'.'thumb_'.basename($image);  
  22.             @copy($this->_sourceImg, $this->_tmpImg);  
  23.             @chmod($this->_tmpImg,0777);  
  24.              switch($this->_imgType){  
  25.                case 'gif' :  
  26.                $this->_save = "imagegif";  
  27.                $this->_create   = 'imagecreatefromgif';  
  28.                break;  
  29.                case 'png' :  
  30.                $this->_save = "imagepng";  
  31.                $this->_create   = 'imagecreatefrompng';  
  32.                break;  
  33.                case 'jpg' :  
  34.                case 'jpeg' :  
  35.                $this->_save = "imagejpeg";  
  36.                $this->_create   = 'imagecreatefromjpeg';  
  37.                break;  
  38.                default :  
  39.                throw new Exception("ERROR; UNSUPPORTED IMAGE TYPE");  
  40.                break;  
  41.              }  
  42.         }else{  
  43.             throw new Exception($image.' is not a Image-Source!');  
  44.         }  
  45.     }  
  46.   
  47.     /*  
  48.     * 缩略图片  
  49.     * @param width Integer   图片剪裁后宽度  
  50.     * @param width Integer   图片剪裁后高度  
  51.     * @param auto  bool      是否等比例额剪裁  
  52.     * @param xoffset Integer 剪裁左端偏移量  
  53.     * @param yoffset Integer 剪裁顶端偏移量  
  54.     ***/  
  55.     function resize($width, $height, $auto = true, $xoffset = 0, $yoffset = 0){  
  56.             if($width > $height){  
  57.                 $size = $width;  
  58.             }else{  
  59.                 $size = $height;  
  60.             }  
  61.             $_create = $this->_create;  
  62.             $_save   = $this->_save;  
  63.             $img = $_create($this->_sourceImg);  
  64.          if($auto == TRUE){  
  65.              list($org_width, $org_height) = getimagesize($this->_sourceImg);  
  66.              if($org_width < $size && $org_height < $size){  
  67.                 $img4= $img;  
  68.              }else{  
  69.                  if($org_width > $org_height){  
  70.                         $swidth  = $size;  
  71.                         $sheight = ($org_height/$org_width) * $size;  
  72.                         $img4=imagecreatetruecolor ($swidth, $sheight);  
  73.                         imagecopyresampled($img4, $img, 0, 0, 0, 0, $swidth, $sheight, $org_width, $org_height);  
  74.                  }else{  
  75.                         $swidth  = ($org_width/$org_height) * $size;  
  76.                         $sheight = $size;  
  77.                         $img4=imagecreatetruecolor ($swidth, $sheight);  
  78.                         imagecopyresampled($img4, $img, 0, 0, 0, 0, $swidth, $sheight, $org_width, $org_height);  
  79.                  }  
  80.              }  
  81.              $value = $this->_imgType == 'png' ? 9 : 100;  
  82.              $_save($img4,$this->_tmpImg,$value);  
  83.              $img = $_create($this->_tmpImg);  
  84.          }else{  
  85.              // 自定义裁切  
  86.              $img_n=imagecreatetruecolor($width, $height);  
  87.              imagecopyresized($img_n, $img, 0, 0, $xoffset, $yoffset, $width, $height, $width, $height);  
  88.              $value = $this->_imgType == 'png' ? 9 : 100; // PNG图片质量最高为9 其他格式为 100 默认为75  
  89.              $_save($img_n,$this->_tmpImg,$value);  
  90.          }  
  91.         return $this;  
  92.     }  
  93.   
  94.     /*  
  95.     * 添加水印  
  96.     $gWaterPos = 9;//水印位置 0:随机 1:顶端居左 2:顶端居中 3:顶端居右 4:中部居左 5:中部居中 6:中部居右 7:底端居左 8:底端居中 9:底端居右  
  97.     $gWaterMarkType = 'text';//text:用字符串作水印 img:用图片作水印  
  98.     $groundImage = '' 原图  
  99.     $gWaterImg = '1.jpg';//作为水印的图片,支持GIF,JPG,PNG格式  
  100.     $gWaterText = 'text';//字符串内容 支持中文  
  101.     $gWaterFontSize = 28;//字体大小  
  102.     $gWaterTextColor = '#FF0000'; //字体颜色  
  103.     $gWaterTtfFile = 'arial.ttf'; //ttf文件,可从C:/WINDOWS/Fonts得到  
  104.     */  
  105.     function makeWater($gWaterPos = 9, $gWaterMarkType='img', $groundImage = '',$gWaterImg='water.jpg',$gWaterText = '',$gWaterFontSize = 28,$gWaterTextColor'#FF0000', $gwaterTtfFile = 'arial.ttf') {  
  106.           $groundImage = $this->_tmpImg;  
  107.           //读取水印文件  
  108.           if($gWaterMarkType == 'img'){  
  109.             if(!empty($gWaterImg) && file_exists($gWaterImg)) {  
  110.               list($water_w,$water_h,$water_t) = getimagesize($gWaterImg);  
  111.               switch($water_t) {  
  112.                 case 1:$water_im = imagecreatefromgif($gWaterImg);break;  
  113.                 case 2:$water_im = imagecreatefromjpeg($gWaterImg);break;  
  114.                 case 3:$water_im = imagecreatefrompng($gWaterImg);break;  
  115.                 default:die($formatMsg);  
  116.               }  
  117.             }else{  
  118.               throw new Exception('the waterImg is not fined!');  
  119.             }  
  120.           }  
  121.         //读取背景图片  
  122.           if(!empty($groundImage) && file_exists($groundImage)) {  
  123.             list($ground_w,$ground_h,$ground_t) = getimagesize($groundImage);  
  124.             switch($ground_t) {  
  125.               case 1:$ground_im = imagecreatefromgif($groundImage);break;  
  126.               case 2:$ground_im = imagecreatefromjpeg($groundImage);break;  
  127.               case 3:$ground_im = imagecreatefrompng($groundImage);break;  
  128.               default:die($formatMsg);  
  129.             }  
  130.           }else{  
  131.               throw new Exception('the BackgroundImg is not fined!');  
  132.           }  
  133.           //水印位置  
  134.           if($gWaterMarkType == 'img') {  
  135.             $w = $water_w;  
  136.             $h = $water_h;  
  137.           }else{  
  138.             $temp = imagettfbbox(ceil($gWaterFontSize),0,$gWaterTtfFile,$gWaterText);//取得使用 TrueType 字体的文本的范围  
  139.             $w = $temp[2] - $temp[6];  
  140.             $h = $temp[3] - $temp[7];  
  141.             unset($temp);  
  142.           }  
  143.           if($ground_w < $w || $ground_h < $h) {  
  144.               throw new Exception('the waterImg is too small!');  
  145.           }  
  146.           switch($gWaterPos) {  
  147.             case 0://随机  
  148.               $posX = rand(0,($ground_w - $w));  
  149.               $posY = rand(0,($ground_h - $h));  
  150.               break;  
  151.             case 1://1为顶端居左  
  152.               $posX = 0;  
  153.               $posY = 0;  
  154.               break;  
  155.             case 2://2为顶端居中  
  156.               $posX = ($ground_w - $w) / 2;  
  157.               $posY = 0;  
  158.               break;  
  159.             case 3://3为顶端居右  
  160.               $posX = $ground_w - $w;  
  161.               $posY = 0;  
  162.               break;  
  163.             case 4://4为中部居左  
  164.               $posX = 0;  
  165.               $posY = ($ground_h - $h) / 2;  
  166.               break;  
  167.             case 5://5为中部居中  
  168.               $posX = ($ground_w - $w) / 2;  
  169.               $posY = ($ground_h - $h) / 2;  
  170.               break;  
  171.             case 6://6为中部居右  
  172.               $posX = $ground_w - $w;  
  173.               $posY = ($ground_h - $h) / 2;  
  174.               break;  
  175.             case 7://7为底端居左  
  176.               $posX = 0;  
  177.               $posY = $ground_h - $h;  
  178.               break;  
  179.             case 8://8为底端居中  
  180.               $posX = ($ground_w - $w) / 2;  
  181.               $posY = $ground_h - $h;  
  182.               break;  
  183.             case 9://9为底端居右  
  184.               $posX = $ground_w - $w;  
  185.               $posY = $ground_h - $h;  
  186.               break;  
  187.             default://随机  
  188.               $posX = rand(0,($ground_w - $w));  
  189.               $posY = rand(0,($ground_h - $h));  
  190.               break;  
  191.           }  
  192.           //设定图像的混色模式  
  193.           imagealphablending($ground_im, true);  
  194.           if($gWaterMarkType == 'img'){  
  195.             imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件  
  196.           }else{  
  197.             if(!empty($gWaterTextColor) && (strlen($gWaterTextColor)==7) ) {  
  198.               $R = hexdec(substr($gWaterTextColor,1,2));  
  199.               $G = hexdec(substr($gWaterTextColor,3,2));  
  200.               $B = hexdec(substr($gWaterTextColor,5));  
  201.             }else{  
  202.                 throw new Exception('The waterText color format error!');  
  203.             }  
  204.             imagettftext($ground_im,$gWaterFontSize,0,$posX,$posY,imagecolorallocate($ground_im,$R,$G,$B),$gWaterTtfFile, iconv("GB2312","UTF-8",$gWaterText));  
  205.           }  
  206.           //生成水印后的图片  
  207.           @unlink($groundImage);  
  208.           switch($ground_t) {  
  209.             case 1:imagegif($ground_im,$groundImage);break;  
  210.             case 2:imagejpeg($ground_im,$groundImage);break;  
  211.             case 3:imagepng($ground_im,$groundImage);break;  
  212.             default:throw new Exception('The backgoundImage is not Support MakeWaterImage!');  
  213.           }  
  214.         //释放内存  
  215.           if(isset($water_info)){  
  216.             unset($water_info);  
  217.           }  
  218.           if(isset($water_im)){  
  219.             imagedestroy($water_im);  
  220.           }  
  221.           unset($ground_info);  
  222.           imagedestroy($ground_im);  
  223.           return $this;  
  224.     }  
  225.   
  226.     /*  
  227.     * 返回处理后的图片地址  
  228.     * @param old bool 是否替换原图  
  229.     * return String  
  230.     **/  
  231.     public function save($old = false) {  
  232.         if($old){  
  233.             @unlink($this->_sourceImg);  
  234.             @rename($this->_tmpImg, $this->_sourceImg);  
  235.             return $this->_sourceImg;  
  236.         }  
  237.         return $this->_tmpImg;  
  238.     }  
  239.   
  240.     /*  
  241.     * 显示图片  
  242.     * @paras source String 图片路径  
  243.     **/  
  244.     public function render($source = TRUE) {  
  245.         switch($this->_imgType){  
  246.                         case 'jpg':  
  247.                         case 'jpeg':  
  248.                             header('Content-Type: image/jpeg');  
  249.                         break;  
  250.                         case 'gif':  
  251.                             header('Content-Type: image/gif');  
  252.                         break;  
  253.                         case 'png':  
  254.                             header('Content-Type: image/png');  
  255.                         break;  
  256.         }  
  257.         $imgSrc = $source ? $this->_sourceImg : $this->_tmpImg;  
  258.         $_create = $this->_create;  
  259.         $_save   = $this->_save;  
  260.         $source = $_create($imgSrc);  
  261.         $_save($source);  
  262.         imagedestroy($imgResource);  
  263.     }  
  264. }  
  265. // 测试用例  
  266. $img = new Image('111.jpg');  
  267. $img->render();  
  268. $saveFileName = $img->resize(300,300,false,300,100)->makewater(9)->save();//自定义裁切 添加水印  
  269. //$saveFileName = $img->resize(200,200)->save(); // 等比例缩略  
  270. //$saveFileName = $img->resize(300,200)->makewater(9)->save(); // 等比例缩略 添加水印  
  271. //$saveFileName = $img->makewater(1)->save(); // 制作水印  
  272. echo $saveFileName;die; 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值