Advertisement
Guest User

Untitled

a guest
Mar 8th, 2011
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.48 KB | None | 0 0
  1. <?php
  2.     function _makeThumbnail($image, $dest, $ext)
  3.     {
  4.         $imageType = exif_imagetype($image);
  5.  
  6.         switch ($imageType)
  7.         {
  8.             case IMAGETYPE_JPEG:
  9.                 $img = imagecreatefromjpeg($image);
  10.                 break;
  11.             case IMAGETYPE_PNG:
  12.                 $img = imagecreatefrompng($image);
  13.                 break;
  14.             case IMAGETYPE_GIF:
  15.                 $img = imagecreatefromgif($image);
  16.                 break;
  17.             default:
  18.                 throw new Exception('Bad extension');
  19.         }
  20.        
  21.         $width  = imagesx($img);
  22.         $height = imagesy($img);
  23.  
  24.             list($widthX, $heightX) = array('130', '130');
  25.            
  26.             if ($width > $widthX || $height > $heightX)
  27.             {        
  28.                 if ($height > $width)
  29.                 {
  30.                     $ratio = $heightX / $height;  
  31.                     $newHeight = $heightX;
  32.                     $newWidth = $width * $ratio;
  33.                 }
  34.                 else
  35.                 {
  36.                     $ratio = $widthX / $width;  
  37.                     $newWidth = $widthX;  
  38.                     $newHeight = $height * $ratio;  
  39.                 }
  40.                
  41.                 $previewImg = imagecreatetruecolor($newWidth, $newHeight);
  42.                
  43.                 $palsize = ImageColorsTotal($img);
  44.                 for ($i = 0; $i < $palsize; $i++)
  45.                 {
  46.                     $colors = ImageColorsForIndex($img, $i);  
  47.                     ImageColorAllocate($previewImg, $colors['red'], $colors['green'], $colors['blue']);
  48.                 }
  49.                
  50.                 imagecopyresized($previewImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
  51.                
  52.                 $name = $dest;
  53.                 switch ($imageType)
  54.                 {
  55.                     case IMAGETYPE_JPEG:
  56.                         imagejpeg($previewImg, $name . '.' . $ext, 100);
  57.                         break;
  58.                     case IMAGETYPE_PNG:
  59.                         imagesavealpha($previewImg, true);
  60.                         imagepng($previewImg, $name . '.' . $ext, 9);
  61.                     case IMAGETYPE_GIF:
  62.                         imagegif($previewImg, $name . '.' . $ext);
  63.                         break;
  64.                     default:
  65.                         throw new Exception();
  66.                 }
  67.             }
  68.         imagedestroy($previewImg);
  69.         imagedestroy($img);
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement