Advertisement
Speeedfire

Untitled

Apr 10th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.59 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) {
  20.         $w = $width ; $h = $height;
  21.         $x = 0;
  22.     } else {
  23.         $ratio = max($width/$w, $height/$h);
  24.         $h = $height / $ratio;
  25.         $x = ($w - $width / $ratio) / 2;
  26.         $w = $width / $ratio;
  27.     }
  28.   }
  29.   else{
  30.     if($w < $width and $h < $height) {
  31.         $height = $h; $width = $w;
  32.         $x = 0;
  33.     } else {
  34.         $ratio = min($width/$w, $height/$h);
  35.         $width = $w * $ratio;
  36.         $height = $h * $ratio;
  37.         $x = 0;
  38.     }
  39.   }
  40.  
  41.   $new = imagecreatetruecolor($width, $height);
  42.  
  43.   // preserve transparency
  44.   if($type == 1 or $type == 3){
  45.     imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
  46.     imagealphablending($new, false);
  47.     imagesavealpha($new, true);
  48.   }
  49.  
  50.   imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);
  51.  
  52.   switch($type){
  53.     case '6': imagewbmp($new, $dst); break;
  54.     case '1': imagegif($new, $dst); break;
  55.     case '2': imagejpeg($new, $dst); break;
  56.     case '3': imagepng($new, $dst); break;
  57.   }
  58.   return true;
  59. }
  60.    
  61. }
  62. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement