| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- class ImageResize {
-
- public function setSize($origin,$target,$cut_width,$cut_height,$start_x,$start_y,$new_width,$new_height,$ext){
- ini_set("memory_limit","1000M");
- $new=imagecreatetruecolor($new_width,$new_height);
- if($ext=='jpg'){
- $source=imagecreatefromjpeg($origin);
- }else{
- $source=imagecreatefrompng($origin);
- imagesavealpha($source,true);
- imagealphablending($new,false);
- imagesavealpha($new,true);
- }
- imagecopyresampled($new,$source,0,0,$start_x,$start_y,$new_width,$new_height,$cut_width,$cut_height);
- if($ext=='jpg'){
- imagejpeg($new,$target,90);
- }else{
- imagepng($new,$target);
- }
- if(file_exists($target)){
- chmod($target,0777);
- return $target;
- }
- return false;
- }
-
- public function getSize($origin,$type=null) {
- $sizes=getimagesize($origin);
- if(is_null($type)){
- return array($sizes[0],$sizes[1]);
- }
- return $sizes[$type];
- }
-
- public function resize($data, $thumb = false){
- $origin = $data['origin'];//原图片路径
- $targets = $data['targets'];//目标图片集参数
- $ext = substr($origin, -3);//图片后缀
-
- foreach ($targets as $key=>$value){
- $new_width = $value['width'];
- $new_height = $value['height'];
- $target = $value['target'];
-
- if($thumb || $origin == $target) {
- $start_x = 0;
- $start_y = 0;
- $cut_width = $this->getSize($origin,0);
- $cut_height = $this->getSize($origin,1);
- }else {
- $start_x = $data['x'];//剪切起点x坐标
- $start_y = $data['y'];//剪切起点y坐标
- $cut_width = $data['w'];//剪切宽度
- $cut_height = $data['h'];//剪切高度
- }
-
- $return = $this->setSize($origin,$target,$cut_width,$cut_height,$start_x,$start_y,$new_width,$new_height,$ext);
- if(!$return){
- break;
- }
- }
- return $return;
- }
- }
- ?>
|