Advertisement
Guest User

Untitled

a guest
Dec 26th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.56 KB | None | 0 0
  1. public function img_resize($src, $dst, $width, $height, $crop=0){
  2.         $gis = getimagesize($src);
  3.         $w = $gis[0]; $h = $gis[1];
  4.  
  5.   $type = $gis[2];
  6.   switch($type){
  7.     case '6': $img = imagecreatefromwbmp($src); break;
  8.     case '1': $img = imagecreatefromgif($src); break;
  9.     case '2': $img = imagecreatefromjpeg($src); break;
  10.     case '3': $img = imagecreatefrompng($src); break;
  11.     default : return "Unsupported picture type!";
  12.   }
  13.  
  14.   // resize
  15.   if($crop){
  16.     if($w < $width or $h < $height) {
  17.         $w = $width ; $h = $height;
  18.         $x = 0;
  19.     } else {
  20.         $ratio = max($width/$w, $height/$h);
  21.         $h = $height / $ratio;
  22.         $x = ($w - $width / $ratio) / 2;
  23.         $w = $width / $ratio;
  24.     }
  25.   }
  26.   else{
  27.     if($w < $width and $h < $height) {
  28.         $height = $h; $width = $w;
  29.         $x = 0;
  30.     } else {
  31.         $ratio = min($width/$w, $height/$h);
  32.         $width = $w * $ratio;
  33.         $height = $h * $ratio;
  34.         $x = 0;
  35.     }
  36.   }
  37.  
  38.   $new = imagecreatetruecolor($width, $height);
  39.  
  40.   // preserve transparency
  41.   if($type == 1 or $type == 3){
  42.     imagecolortransparent($new, imagecolorallocatealpha($new, 255, 255, 255, 127));
  43.     imagealphablending($new, false);
  44.     imagesavealpha($new, true);
  45.   }
  46.  
  47.   imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);
  48.  
  49.   switch($type){
  50.     case '6': imagewbmp($new, $dst); break;
  51.     case '1': imagegif($new, $dst); break;
  52.     case '2': imagejpeg($new, $dst); break;
  53.     case '3': imagepng($new, $dst); break;
  54.   }
  55.   return true;
  56. }
  57.    
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement