php 缩放再裁剪,php裁剪和缩放

1 <?php2

3 /**4 * @remark:将历史图片切成750x500尺寸5 *6 */

7 classCutimage{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 functionindex(){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目标宽 不裁剪宽

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

66 }elseif($width_ratio>$height_ratio){67 if($info[1]

76 $source_y =0;77 $cropped_width=$height_ratio*$target_width;78 if($cropped_width

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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值