1. 程式人生 > >php裁剪和縮放

php裁剪和縮放

col ini get mime 長度 color 裁剪 turn ()

  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裁剪和縮放