Advertisement
Speeedfire

Untitled

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