SHARE
TWEET

Função para criação de thumbnails

yesamarcos Nov 22nd, 2015 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. // RESIZE AN IMAGE PROPORTIONALLY AND CROP TO THE CENTER
  4. function resize_and_crop($original_image_url, $thumb_image_url, $thumb_w, $thumb_h, $quality = 100)
  5. {
  6.     // ACQUIRE THE ORIGINAL IMAGE: http://php.net/manual/en/function.imagecreatefromjpeg.php
  7.     $original = imagecreatefromjpeg($original_image_url);
  8.     if (!$original) return FALSE;
  9.  
  10.     // GET ORIGINAL IMAGE DIMENSIONS
  11.     list($original_w, $original_h) = getimagesize($original_image_url);
  12.  
  13.     // RESIZE IMAGE AND PRESERVE PROPORTIONS
  14.     $thumb_w_resize = $thumb_w;
  15.     $thumb_h_resize = $thumb_h;
  16.     if ($original_w > $original_h)
  17.     {
  18.         $thumb_h_ratio  = $thumb_h / $original_h;
  19.         $thumb_w_resize = (int)round($original_w * $thumb_h_ratio);
  20.     }
  21.     else
  22.     {
  23.         $thumb_w_ratio  = $thumb_w / $original_w;
  24.         $thumb_h_resize = (int)round($original_h * $thumb_w_ratio);
  25.     }
  26.     if ($thumb_w_resize < $thumb_w)
  27.     {
  28.         $thumb_h_ratio  = $thumb_w / $thumb_w_resize;
  29.         $thumb_h_resize = (int)round($thumb_h * $thumb_h_ratio);
  30.         $thumb_w_resize = $thumb_w;
  31.     }
  32.  
  33.     // CREATE THE PROPORTIONAL IMAGE RESOURCE
  34.     $thumb = imagecreatetruecolor($thumb_w_resize, $thumb_h_resize);
  35.     if (!imagecopyresampled($thumb, $original, 0,0,0,0, $thumb_w_resize, $thumb_h_resize, $original_w, $original_h)) return FALSE;
  36.  
  37.     // ACTIVATE THIS TO STORE THE INTERMEDIATE IMAGE
  38.     // imagejpeg($thumb, 'RAY_temp_' . $thumb_w_resize . 'x' . $thumb_h_resize . '.jpg', 100);
  39.  
  40.     // CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS
  41.     $final = imagecreatetruecolor($thumb_w, $thumb_h);
  42.  
  43.     $thumb_w_offset = 0;
  44.     $thumb_h_offset = 0;
  45.     if ($thumb_w < $thumb_w_resize)
  46.     {
  47.         $thumb_w_offset = (int)round(($thumb_w_resize - $thumb_w) / 2);
  48.     }
  49.     else
  50.     {
  51.         $thumb_h_offset = (int)round(($thumb_h_resize - $thumb_h) / 2);
  52.     }
  53.  
  54.     if (!imagecopy($final, $thumb, 0,0, $thumb_w_offset, $thumb_h_offset, $thumb_w_resize, $thumb_h_resize)) return FALSE;
  55.  
  56.     // STORE THE FINAL IMAGE - WILL OVERWRITE $thumb_image_url
  57.     if (!imagejpeg($final, $thumb_image_url, $quality)) return FALSE;
  58.     return TRUE;
  59. }
  60.  
  61. echo '<a target="_blank" href="banner.jpg">Banner Original</a><br/>';
  62.  
  63. resize_and_crop('banner.jpg', 'banner_100x100.jpg', 100, 100);
  64. echo '<a target="_blank" href="banner_100x100.jpg">100x100</a><br/>';
  65.  
  66. resize_and_crop('banner.jpg', 'banner_200x100.jpg', 200, 100);
  67. echo '<a target="_blank" href="banner_200x100.jpg">200x100</a><br/>';
  68.  
  69. resize_and_crop('banner.jpg', 'banner_200x300.jpg', 200, 300);
  70. echo '<a target="_blank" href="banner_200x300.jpg">200x300</a><br/>';
  71.  
  72. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top