码迷,mamicode.com
首页 > Web开发 > 详细

php裁剪和缩放

时间:2017-05-27 15:27:47      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:int   file   lod   width   路径   return   ack   图片   path   

  1 <?php
  2 
  3 /**
  4  *  @remark:将历史图片切成750x500尺寸
  5  *
  6  */
  7 class Cutimage{
  8     private $file_name;
  9     private $file_path;
 10     private $pwidth;
 11     private $pheight;
 12     public function __construct($file_name,$file_path,$width,$height){
 13         $this->file_name=$file_name;
 14         $this->file_path=$file_path;
 15         $this->pwidth=$width;
 16         $this->pheight=$height;
 17     }
 18     public function index(){
 19         $name=$this->file_name;
 20         $file_path=$this->file_path;
 21         $local_img=$file_path.$name;
 22         $new_img= self::zoom($local_img,$this->pwidth,$this->pheight);///切成750x500的
 23         if($new_img!=null){
 24             $sf_path=$file_path.$new_img;
 25             self::zoomimg($sf_path,$sf_path,$this->pwidth,$this->pheight);    //缩放图片
 26         }
 27     }
 28     /**
 29      * 裁剪函数:原图/目标图 这种比例 以小的比例为准
 30      */
 31     public function zoom($back,$target_width,$target_height){
 32         try{
 33             $info = getimagesize($back);
 34         }catch(Exception $e){
 35             return null;
 36         }
 37         if(!$info) return null;
 38         $str =$info[‘mime‘];
 39         $arr =explode(‘/‘,$str);
 40         $ext = $arr[1];
 41         $create = ‘imagecreatefrom‘.$ext;
 42         $save = ‘image‘.$ext;
 43         ini_set("memory_limit", "600M");
 44         $img = $create($back);
 45         $source_width=$info[0];
 46         $source_height=$info[1];
 47         $width_ratio=$info[0]/$target_width;
 48         $height_ratio=$info[1]/$target_height;
 49         if($width_ratio<$height_ratio){
 50             if($info[0]<=$target_width){
 51                 $cropped_width=$info[0];
 52                 $source_x=0;
 53                 $cropped_height=$target_height*$width_ratio;
 54                 if($cropped_height<$info[1]){$source_y=($info[1]-$cropped_height)/2;}
 55                 else{$source_y=0;}
 56             }else{
 57                 // $cropped_width=$target_width; 如果按原图宽>目标宽  不裁剪宽
 58                 $cropped_width=$info[0];
 59                 // $source_x = ($source_width - $cropped_width) / 2;
 60                 $source_x=0;
 61                 $cropped_height=$target_height*$width_ratio;
 62                 if($cropped_height<$info[1]){$source_y=($info[1]-$cropped_height)/2;}
 63                 else{$source_y=0;}
 64             }
 65 
 66         }elseif($width_ratio>$height_ratio){
 67             if($info[1]<$target_height){
 68                 $cropped_height=$info[1];
 69                 $source_y=0;
 70                 $cropped_width=$height_ratio*$target_width;
 71                 if($cropped_width<$info[0]){$source_x = ($source_width - $cropped_width) / 2;}
 72                 else{$source_x=0;}
 73             }else{
 74                 $cropped_height=$info[1];
 75                 // $source_y = ($source_height - $cropped_height) / 2;
 76                 $source_y =0;
 77                 $cropped_width=$height_ratio*$target_width;
 78                 if($cropped_width<$info[0]){$source_x = ($source_width - $cropped_width) / 2;}
 79                 else{$source_x=0;}
 80             }
 81         }
 82         else{    // 源图适中
 83             $cropped_width  = $source_width;
 84             $cropped_height = $source_height;
 85             $source_x = 0;
 86             $source_y = 0;
 87         }
 88         $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
 89         imagecopy($cropped_image, $img, 0, 0, $source_x, $source_y, $cropped_width,$cropped_height);
 90         $name = basename($back);
 91         $pre=$target_width.‘x‘.$target_height.‘_‘;
 92         $filename=$pre.$name;
 93         $save($cropped_image,‘./‘.$filename);
 94         imagedestroy($img);
 95         imagedestroy($cropped_image);
 96         return $filename;
 97     }
 98     /*
 99       @param $inf 待缩放图片路径
100       @param $outf  缩放后图片存放路径
101       @param $ow 目标缩放宽度
102       @param $oh 目标缩放高度
103       @param $force 若原图长宽都比目标图片小,是否强制放大,0为输出原图,1为强制放大
104     */
105     public function zoomimg($inf, $outf, $ow, $oh, $q=80, $force=0){
106         if(file_exists($inf)){
107             $ow = intval($ow);
108             $oh = intval($oh);
109             $q = intval($q);
110             //取得源图片的宽度和高度
111             $size_src=getimagesize($inf);
112             $iw=$size_src[‘0‘];
113             $ih=$size_src[‘1‘];
114             $str =$size_src[‘mime‘];
115             $arr =explode(‘/‘,$str);
116             $ext = $arr[1];
117             $create = ‘imagecreatefrom‘.$ext;
118             $save = ‘image‘.$ext;
119             if($ext == ‘png‘){
120                 $q = 9;
121             }else{
122                 $q = 80;
123             }
124             //图片的等比缩放
125             $src=$create($inf);
126             //根据最大值为300,算出另一个边的长度,得到缩放后的图片宽度和高度
127             if(($ow > $iw && $oh > $ih) || ($ow == 0 && $oh == 0) || ($ow == 0 && $oh >$ih) || ($ow > $iw && $oh == 0)){
128                 $ow = $iw;
129                 $oh = $ih;
130             }elseif($ow == 0){
131                 $ow = $iw*($oh/$ih);
132             }elseif($oh == 0){
133                 $oh = $ih*($ow/$iw);
134             }elseif($iw/$ow > $ih/$oh){
135                 $oh = $ih*($ow/$iw);
136             }else{
137                 $ow = $iw*($oh/$ih);
138             }
139             //声明一个$w宽,$h高的真彩图片资源
140             $image=imagecreatetruecolor($ow, $oh);
141             //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
142             imagecopyresampled($image, $src, 0, 0, 0, 0, $ow, $oh, $iw, $ih);
143             $save($image,$outf,$q);
144             //销毁资源
145             imagedestroy($image);
146             return true;
147         }else{
148             return false;
149         }
150     }
151 }

 

php裁剪和缩放

标签:int   file   lod   width   路径   return   ack   图片   path   

原文地址:http://www.cnblogs.com/wumoran/p/6912821.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!