Advertisement
Guest User

Prestashop Image Cropping (Timthumb) www.bazingadesigns.com

a guest
Nov 13th, 2012
1,979
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. // Usage:
  4. // Replace the contents of:
  5. // /overrides/classes/ImageManager.php
  6. // With this document. Then just add a new image size in the Backoffice :
  7. // Preferences -> Images -> Add New
  8. // AND name it whatever_btt (attach '_btt' at the end of the name string)
  9. // THEN just use this image size where you want :)
  10.  
  11. // CAUTION! Check if you're using the same version of Prestashop as we do: 1.5.2
  12. // (should work in all 1.5.xs though but we never checked :)
  13.  
  14. // Good luck - Bazinga Designs
  15. // http://www.bazingadesigns.com/en
  16.  
  17. class ImageManager extends ImageManagerCore
  18. {
  19.  
  20.     /**
  21.      * Resize, cut and optimize image
  22.      *
  23.      * Zoom & Croop when the destination file name
  24.      * contains the '_timthumb' phrase
  25.      * - Modified by www.bazingadesigns.com/en
  26.      * (TimThumb ZoomCrop port)
  27.      *
  28.      * @param string $src_file Image object from $_FILE
  29.      * @param string $dst_file Destination filename
  30.      * @param integer $dst_width Desired width (optional)
  31.      * @param integer $dst_height Desired height (optional)
  32.      * @param string $file_type
  33.      * @return boolean Operation result
  34.      */
  35.     public static function resize($src_file, $dst_file, $dst_width = null, $dst_height = null, $file_type = 'jpg', $force_type = false)
  36.     {
  37.         if (!file_exists($src_file))
  38.             return false;
  39.         list($src_width, $src_height, $type) = getimagesize($src_file);
  40.  
  41.         // If PS_IMAGE_QUALITY is activated, the generated image will be a PNG with .jpg as a file extension.
  42.         // This allow for higher quality and for transparency. JPG source files will also benefit from a higher quality
  43.         // because JPG reencoding by GD, even with max quality setting, degrades the image.
  44.         if (Configuration::get('PS_IMAGE_QUALITY') == 'png_all'
  45.             || (Configuration::get('PS_IMAGE_QUALITY') == 'png' && $type == IMAGETYPE_PNG) && !$force_type)
  46.             $file_type = 'png';
  47.        
  48.         if (strpos($dst_file, '_btt')!==FALSE)
  49.             $zoomCrop = true;
  50.         else
  51.             $zoomCrop = false;
  52.  
  53.         if (!$src_width)
  54.             return false;
  55.  
  56.         if ($zoomCrop == true) {
  57.  
  58.             if (!$dst_width) $dst_width = 0;
  59.             if (!$dst_height) $dst_height = 0;
  60.  
  61.         } else {
  62.  
  63.             if (!$dst_width)
  64.                 $dst_width = $src_width;
  65.             if (!$dst_height)
  66.                 $dst_height = $src_height;
  67.         }  
  68.  
  69.  
  70.         $src_image = ImageManager::create($type, $src_file);
  71.  
  72.         $width_diff = $dst_width / $src_width;
  73.         $height_diff = $dst_height / $src_height;
  74.  
  75.         if ($zoomCrop==true) {
  76.          
  77.             if ($dst_width>0 && $dst_height<1) {
  78.                 $dst_height = floor ($src_height * ($dst_width / $src_width));
  79.             } else if ($dst_height>0 && $dst_width<1) {
  80.                 $dst_width = floor ($src_width * ($dst_height / $src_height));
  81.             }
  82.                  
  83.             $src_x = $src_y = 0;
  84.             $src_w = $src_width;
  85.             $src_h = $src_height;
  86.            
  87.             $cmp_x = $src_width / $dst_width;
  88.             $cmp_y = $src_height / $dst_height;
  89.            
  90.             if ($cmp_x > $cmp_y) {
  91.            
  92.                 $src_w = round (($src_width / $cmp_x * $cmp_y));
  93.                 $src_x = round (($src_width - ($src_width / $cmp_x * $cmp_y)) / 2);
  94.            
  95.             } else if ($cmp_y > $cmp_x) {
  96.            
  97.                 $src_h = round (($src_height / $cmp_y * $cmp_x));
  98.                 $src_y = round (($src_height - ($src_height / $cmp_y * $cmp_x)) / 2);
  99.            
  100.             }
  101.                        
  102.         }
  103.  
  104.         else if ($width_diff > 1 && $height_diff > 1)
  105.         {
  106.             $next_width = $src_width;
  107.             $next_height = $src_height;
  108.         }
  109.         else
  110.         {
  111.             if (Configuration::get('PS_IMAGE_GENERATION_METHOD') == 2 || (!Configuration::get('PS_IMAGE_GENERATION_METHOD') && $width_diff > $height_diff))
  112.             {
  113.                 $next_height = $dst_height;
  114.                 $next_width = round(($src_width * $next_height) / $src_height);
  115.                 $dst_width = (int)(!Configuration::get('PS_IMAGE_GENERATION_METHOD') ? $dst_width : $next_width);
  116.             }
  117.             else
  118.             {
  119.                 $next_width = $dst_width;
  120.                 $next_height = round($src_height * $dst_width / $src_width);
  121.                 $dst_height = (int)(!Configuration::get('PS_IMAGE_GENERATION_METHOD') ? $dst_height : $next_height);
  122.             }
  123.         }
  124.  
  125.         $dest_image = imagecreatetruecolor($dst_width, $dst_height);
  126.  
  127.         // If image is a PNG and the output is PNG, fill with transparency. Else fill with white background.
  128.         if ($file_type == 'png' && $type == IMAGETYPE_PNG)
  129.         {
  130.             imagealphablending($dest_image, false);
  131.             imagesavealpha($dest_image, true);
  132.             $transparent = imagecolorallocatealpha($dest_image, 255, 255, 255, 127);
  133.             imagefilledrectangle($dest_image, 0, 0, $dst_width, $dst_height, $transparent);
  134.         }
  135.         else
  136.         {
  137.             $white = imagecolorallocate($dest_image, 255, 255, 255);
  138.             imagefilledrectangle ($dest_image, 0, 0, $dst_width, $dst_height, $white);
  139.         }
  140.  
  141.         if ($zoomCrop==true)           
  142.             imagecopyresampled($dest_image, $src_image, 0, 0, $src_x, $src_y, $dst_width, $dst_height, $src_w, $src_h);
  143.         else
  144.             imagecopyresampled($dest_image, $src_image, (int)(($dst_width - $next_width) / 2), (int)(($dst_height - $next_height) / 2), 0, 0, $next_width, $next_height, $src_width, $src_height);
  145.        
  146.         return (ImageManager::write($file_type, $dest_image, $dst_file));
  147.     }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement