Advertisement
Guest User

PHP image resize script (that could be better)

a guest
Dec 29th, 2011
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.69 KB | None | 0 0
  1. <?php
  2.  
  3.   /* ----------[ func IMAGE_RESIZE_DIMENSIONS ]----------*/
  4.   function image_resize_dimensions($source_width,$source_height,$thumb_width,$thumb_height)
  5.   {
  6.  
  7.     $source_ratio = $source_width / $source_height;
  8.     $thumb_ratio = $thumb_width / $thumb_height;
  9.     // Ratio is Taller
  10.     if ($thumb_ratio > $source_ratio)
  11.     {
  12.       $result_height = $thumb_height;
  13.       $result_width = $thumb_height * $source_ratio;
  14.     }
  15.     // Ratio is Wider
  16.     elseif ($thumb_ratio < $source_ratio)
  17.     {
  18.       $result_width = $thumb_width;
  19.       $result_height = $thumb_width / $source_ratio;
  20.     }
  21.     // Ratio the Same
  22.     elseif($thumb_ratio == $source_ratio)
  23.     {
  24.       $result_height = $thumb_height;
  25.       $result_width = $thumb_width;
  26.     }
  27.  
  28.     return array('x'=>$result_width,'y'=>$result_height);
  29.   }
  30.  
  31.   /* ----------[ func IMAGE_RESIZE ]----------*/
  32.   function image_resize_jpg($image = FALSE,
  33.                             $filename = FALSE,
  34.                             $thumb_width,
  35.                             $thumb_height,
  36.                             $maximize = FALSE,
  37.                             $quality = 75,
  38.                             $image_object = FALSE)
  39.   {
  40.     if (is_resource($image_object) === true)
  41.     {
  42.       $source_width = imagesx($image_object);
  43.       $source_height = imagesy($image_object);
  44.       $thumb_source = $image_object;
  45.     }
  46.     else
  47.     {
  48.       list($source_width, $source_height, $source_type) = @getimagesize($image);
  49.       switch($source_type)
  50.       {
  51.         case 1:
  52.           $thumb_source = imagecreatefromgif($image);
  53.           break;
  54.         case 2:
  55.           $thumb_source = imagecreatefromjpeg($image);
  56.           break;
  57.         case 3:
  58.           $thumb_source = imagecreatefrompng($image);
  59.           break;
  60.         default:
  61.           return false;
  62.           break;
  63.        }
  64.     }
  65.  
  66.     if ($maximize === TRUE)
  67.     {
  68.  
  69.       $target_width = $thumb_width;
  70.       $target_height = $thumb_height;
  71.  
  72.       if ($thumb_width && ($source_width < $source_height))
  73.       {
  74.          $thumb_width = ($thumb_height / $source_height) * $source_width;
  75.       }
  76.       else
  77.       {
  78.          $thumb_height = ($thumb_width / $source_width) * $source_height;
  79.       }
  80.  
  81.       if ($thumb_height < $target_height)
  82.       {
  83.         $multiply_height = $target_height / $thumb_height;
  84.         $thumb_height = $thumb_height * $multiply_height;
  85.         $thumb_width = $thumb_width * $multiply_height;
  86.       }
  87.       if ($thumb_width < $target_width)
  88.       {
  89.         $multiply_width = $target_width / $thumb_width;
  90.         $thumb_height = $thumb_height * $multiply_width;
  91.         $thumb_width = $thumb_width * $multiply_width;
  92.       }
  93.  
  94.       $thumb_height = ceil($thumb_height);
  95.       $thumb_width = ceil($thumb_width);
  96.  
  97.     }
  98.     else if ($source_width > $thumb_width or $source_height > $thumb_height)
  99.     {
  100.  
  101.       $dimensions = image_resize_dimensions($source_width,$source_height,$thumb_width,$thumb_height);
  102.       $thumb_width = $dimensions['x'];
  103.       $thumb_height = $dimensions['y'];
  104.       $target_width = $dimensions['x'];
  105.       $target_height = $dimensions['y'];
  106.  
  107.     }
  108.     else // Resize Nothing
  109.     {
  110.       $thumb_width = $source_width;
  111.       $thumb_height = $source_height;
  112.       $target_width = $source_width;
  113.       $target_height = $source_height;
  114.     }
  115.  
  116.     $thumb_image = imagecreatetruecolor($thumb_width,$thumb_height);
  117.  
  118.       //imagecopyresampled($thumb_image, $thumb_source, 0, 0, 0, 0, $thumb_width, $thumb_height, $source_width, $source_height);
  119.     fastimagecopyresampled($thumb_image, $thumb_source, 0, 0, 0, 0, $thumb_width, $thumb_height, $source_width, $source_height);
  120.  
  121.     $target_image = imagecreatetruecolor($target_width,$target_height);
  122.     imagecopy ($target_image, $thumb_image, 0, 0, ($thumb_width - $target_width)/2, ($thumb_height - $target_height)/2, $target_width , $target_height);
  123.  
  124.     if($filename === FALSE)
  125.     {
  126.       unset($thumb_image);
  127.       return array('file'=>$filename,
  128.                    'width'=>$target_width,
  129.                    'height'=>$target_height,
  130.                    'quality'=>$quality,
  131.                    'type'=>$source_type,
  132.                    'object'=>$target_image
  133.                    );
  134.     }
  135.     else
  136.     {
  137.       imagejpeg($target_image,$filename,$quality);
  138.       unset($target_image,$thumb_image);
  139.       return array('file'=>$filename,
  140.                    'width'=>$target_width,
  141.                    'height'=>$target_height,
  142.                    'quality'=>$quality,
  143.                    'type'=>$source_type
  144.                    );
  145.     }
  146.   }
  147.  
  148.  
  149.     function fastimagecopyresampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 3)
  150.     {
  151.         // Plug-and-Play fastimagecopyresampled function replaces much slower imagecopyresampled.
  152.         // Just include this function and change all "imagecopyresampled" references to "fastimagecopyresampled".
  153.         // Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail size using the default quality setting.
  154.         // Author: Tim Eckel - Date: 12/17/04 - Project: FreeRingers.net - Freely distributable.
  155.         //
  156.         // Optional "quality" parameter (defaults is 3).  Fractional values are allowed, for example 1.5.
  157.         // 1 = Up to 600 times faster.  Poor results, just uses imagecopyresized but removes black edges.
  158.         // 2 = Up to 95 times faster.  Images may appear too sharp, some people may prefer it.
  159.         // 3 = Up to 60 times faster.  Will give high quality smooth results very close to imagecopyresampled.
  160.         // 4 = Up to 25 times faster.  Almost identical to imagecopyresampled for most images.
  161.         // 5 = No speedup.  Just uses imagecopyresampled, highest quality but no advantage over imagecopyresampled.
  162.    
  163.         if (empty($src_image) || empty($dst_image)) { return false; }
  164.         if ($quality <= 1) {
  165.          $temp = imagecreatetruecolor ($dst_w + 1, $dst_h + 1);
  166.          imagecopyresized ($temp, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w + 1, $dst_h + 1, $src_w, $src_h);
  167.          imagecopyresized ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $dst_w, $dst_h);
  168.          imagedestroy ($temp);
  169.         } elseif ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
  170.          $tmp_w = $dst_w * $quality;
  171.          $tmp_h = $dst_h * $quality;
  172.          $temp = imagecreatetruecolor ($tmp_w + 1, $tmp_h + 1);
  173.          imagecopyresized ($temp, $src_image, $dst_x * $quality, $dst_y * $quality, $src_x, $src_y, $tmp_w + 1, $tmp_h + 1, $src_w, $src_h);
  174.          imagecopyresampled ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h);
  175.          imagedestroy ($temp);
  176.         } else {
  177.          imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
  178.         }
  179.         return true;
  180.     }
  181.  
  182. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement