php 圖片操做類,支持生成縮略圖,添加水印,上傳縮略圖

<?phpphp

class  Image {
     //類開始
     public  $originimage  "" //源圖片文件地址
     public  $imageext  "" //源圖片格式
     public  $thumbimage  "" //縮略圖文件存放地址
     public  $thumb_maxwidth  = 1440;  //縮略圖最大寬度
     public  $thumb_maxheight  = 900;  //縮略圖最大高度
     public  $watermark_text  "" //水印文字內容
     public  $watermark_minwidth  = 300;  //源圖片最小寬度:大於此值時加水印
     public  $watermark_minheight  = 200;  //源圖片最小高度:大於此值時加水印
     public  $watermark_fontfile  "" //字體文件
     public  $watermark_fontsize  = 14;  //字體大小
     public  $watermark_logo  "config/water.png" //水印LOGO地址
     public  $watermark_transparent  = 20;  //水印LOGO不透明度
     private  $origin_width  = 0;  //源圖片寬度
     private  $origin_height  = 0;  //源圖片高度
     private  $imageQuilty  = 90;  //圖片質量
     private  $tmp_originimage  "" //臨時圖片(源圖片)
     private  $tmp_thumbimage  "" //臨時圖片(縮略圖)
     private  $tmp_waterimage  "" //臨時圖片(水印LOGO)
     private  $_waterPosition  = 2;  //1正中間 2右下角
     //生成縮略圖
     public  function  gen_thumbimage() {
         if  ( $this ->originimage ==  ""  ||  $this ->thumbimage ==  "" ) {
             return  0;
         }
         $this ->get_oriwidthheight ();
         if  ( $this ->origin_width <  $this ->thumb_maxwidth &&  $this ->origin_height <  $this ->thumb_maxheight) {
             $this ->thumb_maxwidth =  $this ->origin_width;
             $this ->thumb_maxheight =  $this ->origin_height;
         else  {
             if  ( $this ->origin_width <  $this ->origin_height) {
                 $this ->thumb_maxwidth = ( $this ->thumb_maxheight /  $this ->origin_height) *  $this ->origin_width;
             else  {
                 $this ->thumb_maxheight = ( $this ->thumb_maxwidth /  $this ->origin_width) *  $this ->origin_height;
             }
         }
         $this ->get_imagetype ();
         $this ->gen_tmpimage_origin ();
         $this ->gen_tmpimage_thumb ();
         if  ( $this ->tmp_originimage ==  ""  ||  $this ->tmp_thumbimage ==  "" ) {
             return  - 1;
         }
         imagecopyresampled (  $this ->tmp_thumbimage,  $this ->tmp_originimage, 0, 0, 0, 0,  $this ->thumb_maxwidth, $this ->thumb_maxheight,  $this ->origin_width,  $this ->origin_height );
         switch  ( $this ->imageext) {
             case  "gif"  :
                 imagegif (  $this ->tmp_thumbimage,  $this ->thumbimage );
                 return  1;
                 break ;
             case  "jpg"  :
                 imagejpeg (  $this ->tmp_thumbimage,  $this ->thumbimage,  $this ->imageQuilty );
                 return  2;
                 break ;
             case  "png"  :
                 imagepng (  $this ->tmp_thumbimage,  $this ->thumbimage );
                 return  3;
                 break ;
             default  :
                 return  - 2;
                 break ;
         }
     }
     //添加文字水印
     public  function  add_watermark1() {
         if  ( $this ->originimage ==  ""  ||  $this ->watermark_text ==  ""  ||  $this ->watermark_fontfile ==  "" ) {
             return  0;
         }
         $this ->get_oriwidthheight ();
         if  ( $this ->origin_width <  $this ->watermark_minwidth ||  $this ->origin_height <  $this ->watermark_minheight) {
             return  0;
         }
         $this ->get_imagetype ();
         $this ->gen_tmpimage_origin ();
         if  ( $this ->tmp_originimage ==  "" ) {
             return  - 1;
         }
         $textcolor  = imagecolorallocate (  $this ->tmp_originimage, 255, 0, 0 );
         $angle  = 0;
         $px  $this ->origin_width / 2 - 200;
         $py  $this ->origin_height / 2 - 10;
         imagettftext (  $this ->tmp_originimage,  $this ->watermark_fontsize,  $angle $px $py $textcolor $this ->watermark_fontfile,  $this ->watermark_text );
         switch  ( $this ->imageext) {
             case  "gif"  :
                 imagegif (  $this ->tmp_originimage,  $this ->originimage );
                 return  1;
                 break ;
             case  "jpg"  :
                 imagejpeg (  $this ->tmp_originimage,  $this ->originimage,  $this ->imageQuilty );
                 return  2;
                 break ;
             case  "png"  :
                 imagepng (  $this ->tmp_originimage,  $this ->originimage );
                 return  3;
                 break ;
             default  :
                 return  - 2;
                 break ;
         }
     }
     //添加LOGO水印
     public  function  add_watermark2() {
         if  ( $this ->originimage ==  ""  ||  $this ->watermark_logo ==  "" ) {
             return  0;
         }
         $this ->get_oriwidthheight ();
         if  ( $this ->origin_width <  $this ->watermark_minwidth ||  $this ->origin_height <  $this ->watermark_minheight) {
             return  0;
         }
         $this ->get_imagetype ();
         $this ->gen_tmpimage_origin ();
         $this ->gen_tmpimage_waterlogo ();
         if  ( $this ->tmp_originimage ==  ""  ||  $this ->tmp_waterimage ==  "" ) {
             return  - 1;
         }
         list (  $logo_width $logo_height  ) =  getimagesize  $this ->watermark_logo );
         //正中間
         $waterZb  $this ->waterPosition (  $logo_width $logo_height  );
         $px  $waterZb  [ 'x' ];
         $py  $waterZb  [ 'y' ];
         imagecopymerge (  $this ->tmp_originimage,  $this ->tmp_waterimage,  $px $py , 0, 0,  $logo_width , $logo_height $this ->watermark_transparent );
         switch  ( $this ->imageext) {
             case  "gif"  :
                 imagegif (  $this ->tmp_originimage,  $this ->originimage );
                 return  1;
                 break ;
             case  "jpg"  :
                 imagejpeg (  $this ->tmp_originimage,  $this ->originimage,  $this ->imageQuilty );
                 return  2;
                 break ;
             case  "png"  :
                 imagepng (  $this ->tmp_originimage,  $this ->originimage );
                 return  3;
                 break ;
             default  :
                 return  - 2;
                 break ;
         }
     }
      
     /**
      * 上傳縮略圖
      * 注意上傳文件大小限制
      *@param String $files $_FILES['upload'] 類型
      *@param String  $path  存儲的目錄 默認在/static/attached/
      *@param boolean $isWater 是否添加水印
      * @return string $filePath 網頁url圖片路徑
      */
     public  function  upload( $files $path $isWater ) {
         if  ( is_uploaded_file  $files  [ 'tmp_name' ] )) {
             $upfile  $files ;
             $name  $upfile  [name];
             $type  $upfile  [type];
             $size  $upfile  [size];
             $tmp_name  $upfile  [tmp_name];
             $error  $upfile  [error];
              
             if  ( $size  > 1048576) {
                 return  array  ( 'status'  => false,  'message'  =>  "$name圖片太大超過1MB"  );
             }
              
             $rs  $this -> getImageSize  $tmp_name  );
             if  (!  $rs  [ 'status' ]) {
                 $rs  [ 'message' ] =  $name  $rs  [ 'message' ];
                 return  $rs ;
             }
              
             // 建立文件夾
             $save_path  getcwd  () .  "/static/attached/"  $path  "/" ;
             $save_url  "./static/attached/"  $path  "/" ;
             $ym  date  "Ym"  );
             $save_path  .=  $ym  "/" ;
             $save_url  .=  $ym  "/" ;
             if  (!  file_exists  $save_path  )) {
                 mkdir  $save_path  );
             }
              
             if  ( $error  ==  '0' ) {
                 $fileType  substr  $name strpos  $name "."  ) + 1 );
                 $prefix  $this ->getRandPrefix ();
                 $newName  date  "YmdHi"  ) .  $prefix  "."  $fileType ;
                 $filepath  $save_path  $newName ;
                 move_uploaded_file (  $tmp_name $filepath  );
              
             }
              
             if  ( $isWater ) {
                 $this ->water (  $filepath  );
             }
              
             return  array  ( 'status'  => true,  'message'  =>  $save_url  $newName  );
         }
     }
      
     /**
      * 圖片增長水印處理
      * @param unknown_type $image
      */
     public  function  water( $image ) {
         $this ->watermark_logo = ROOT .  $this ->watermark_logo;
         $this ->originimage =  $image ;
         //LOGO水印
         $this ->add_watermark2 ();
     }
      
     /**
      *
      * 獲取隨機前綴
      */
     private  function  getRandPrefix() {
         $string  "abcdefghijklmnopqrstuvwxyz0123456789" ;
         $prefix  '' ;
         for ( $i  = 0;  $i  < 4;  $i  ++) {
             $rand  = rand ( 0, 33 );
             $prefix  .=  $string  { $rand };
         }
         return  $prefix ;
     }
      
     //檢測圖片大小
     private  function  getImageSize ( $image ) {
         list (  $width $height $type $attr  ) =  getimagesize  $image  );
          
         if  ( $type  != 2 &&  $type  != 3) {
             return  array  ( 'status'  => false,  'message'  =>  "圖片格式不正確,請上傳JPG或者PNG圖片"  $type  );
         }
         //檢測圖片大小
         if  ( $width  > 1440) {
             return  array  ( 'status'  => false,  'message'  =>  "圖片寬度請小於1440px,當前爲"  $width  "px"  );
         }
          
         if  ( $height  > 900) {
             return  array  ( 'status'  => false,  'message'  =>  "圖片高度請小於900px,當前爲"  $height  "px"  );
          
         }
         return  array  ( 'status'  => true );
     }
      
     /**
      * 生成縮略圖
      *
      * @param String $imagefile 原始文件
      * @param String $thumbWidth  縮略圖寬度
      * @param String $thumbHeight 縮略圖高度
      * @return String 縮略圖url        
      */
     public  function  reduceImage( $imagefile $thumbWidth $thumbHeight $path  "thumb" ) {
          
         // 生成縮略圖
         $dir  date  "Ym" , time () );
         $imagefile  = ROOT .  $imagefile ;
         $imagefile_s  = ROOT .  "static/attached/"  $path  "/"  $dir  "/s_"  basename  $imagefile  );
         $imagetrans  new  Image ();
         $imagetrans ->originimage =  $imagefile ;
         $imagetrans ->thumbimage =  $imagefile_s ;
         $imagetrans ->thumb_maxwidth =  $thumbWidth ;
         $imagetrans ->thumb_maxheight =  $thumbHeight ;
         $isokid  $imagetrans ->gen_thumbimage ();
          
         return  "./static/attached/"  $path  "/"  $dir  "/s_"  basename  $imagefile  );
      
     }
     /**
      * 水印位置
      * @param int $logo_width
      * @param int $logo_height
      * @return 水印座標
      */
     private  function  waterPosition( $logo_width $logo_height ) {
         switch  ( $this ->_waterPosition) {
             case  1 :
                 $px  $this ->origin_width / 2 -  $logo_width  / 2;
                 $py  $this ->origin_height / 2 -  $logo_height  / 2;
                 break ;
             case  2 :
                 $px  $this ->origin_width -  $logo_width  - 10;
                 $py  $this ->origin_height -  $logo_height  - 10;
                 break ;
             default  :
                 $px  $this ->origin_width / 2 -  $logo_width  / 2;
                 $py  $this ->origin_height / 2 -  $logo_height  / 2;
                 break ;
         }
          
         return  array  ( 'x'  =>  $px 'y'  =>  $py  );
     }
     //獲取圖片尺寸
     private  function  get_oriwidthheight() {
         list (  $this ->origin_width,  $this ->origin_height ) =  getimagesize  $this ->originimage );
         return  1;
     }
     /*
      * 檢測圖片格式
      * 原方法須要開啓exif 擴展
      */
     private  function  get_imagetype() {
         $ext  $this ->getImgext (  $this ->originimage );
         switch  ( $ext ) {
             case  1 :
                 $this ->imageext =  "gif" ;
                 break ;
             case  2 :
                 $this ->imageext =  "jpg" ;
                 break ;
             case  3 :
                 $this ->imageext =  "png" ;
                 break ;
             default  :
                 $this ->imageext =  "unknown" ;
                 break ;
         }
     }
     //建立臨時圖片(源圖片)
     private  function  gen_tmpimage_origin() {
         $ext  $this ->getImgext (  $this ->originimage );
         switch  ( $ext ) {
             case  1 :
                 $this ->tmp_originimage = imagecreatefromgif (  $this ->originimage );
                 $bgcolor  = imagecolorallocate (  $this ->tmp_originimage, 0, 0, 0 );
                 $bgcolortrans  = imagecolortransparent (  $this ->tmp_originimage,  $bgcolor  );
                 break ;
             case  2 :
                 $this ->tmp_originimage = imagecreatefromjpeg (  $this ->originimage );
                 break ;
             case  3 :
                 $this ->tmp_originimage = imagecreatefrompng (  $this ->originimage );
                 imagesavealpha (  $this ->tmp_originimage, true );
                 break ;
             default  :
                 $this ->tmp_originimage =  "" ;
                 break ;
         }
     }
     //建立臨時圖片(縮略圖)
     private  function  gen_tmpimage_thumb() {
         $ext  $this ->getImgext (  $this ->originimage );
         switch  ( $ext ) {
             case  1 :
                 $this ->tmp_thumbimage = imagecreatetruecolor (  $this ->thumb_maxwidth,  $this ->thumb_maxheight );
                 $bgcolor  = imagecolorallocate (  $this ->tmp_thumbimage, 255, 255, 255 );
                 imagefill (  $this ->tmp_thumbimage, 0, 0,  $bgcolor  );
                 break ;
             case  2 :
                 $this ->tmp_thumbimage = imagecreatetruecolor (  $this ->thumb_maxwidth,  $this ->thumb_maxheight );
                 break ;
             case  3 :
                 $this ->tmp_thumbimage = imagecreatetruecolor (  $this ->thumb_maxwidth,  $this ->thumb_maxheight );
                 $bgcolor  = imagecolorallocate (  $this ->tmp_thumbimage, 255, 255, 255 );
                 imagefill (  $this ->tmp_thumbimage, 0, 0,  $bgcolor  );
                 imagealphablending (  $this ->tmp_thumbimage, false );
                 imagesavealpha (  $this ->tmp_thumbimage, true );
                 break ;
             default  :
                 $this ->tmp_thumbimage =  "" ;
                 break ;
         }
     }
     //建立臨時圖片(LOGO水印)
     private  function  gen_tmpimage_waterlogo() {
         $ext  $this ->getImgext (  $this ->watermark_logo );
         switch  ( $ext ) {
             case  1 :
                 $this ->tmp_waterimage = imagecreatefromgif (  $this ->watermark_logo );
                 $bgcolor  = imagecolorallocate (  $this ->tmp_waterimage, 0, 0, 0 );
                 $bgcolortrans  = imagecolortransparent (  $this ->tmp_waterimage,  $bgcolor  );
                 break ;
             case  2 :
                 $this ->tmp_waterimage = imagecreatefromjpeg (  $this ->watermark_logo );
                 break ;
             case  3 :
                 $this ->tmp_waterimage = imagecreatefrompng (  $this ->watermark_logo );
                 imagesavealpha (  $this ->tmp_waterimage, true );
                 break ;
             default  :
                 $this ->tmp_waterimage =  "" ;
                 break ;
         }
     }
     /*
      * 獲取後綴名
      */
     public  function  getImgext( $filename ) {
         return  exif_imagetype (  $filename  );
     }
     //釋放資源
     public  function  __destruct() {
         if  ( is_object  $this ->tmp_originimage ) == true) {
             imagedestroy (  $this ->tmp_originimage );
         }
         if  ( is_object  $this ->tmp_thumbimage ) == true) {
             imagedestroy (  $this ->tmp_thumbimage );
         }
         if  ( is_object  $this ->tmp_waterimage ) == true) {
             imagedestroy (  $this ->tmp_waterimage );
         }
     }
}
相關文章
相關標籤/搜索