Advertisement
Guest User

Prestashop Image Cropping (Timthumb) www.bazingadesigns.com

a guest
Nov 12th, 2012
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.81 KB | None | 0 0
  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.  
  51.         if (!$src_width)
  52.             return false;
  53.  
  54.         if ($zoomCrop == true) {
  55.  
  56.             if (!$dst_width) $dst_width = 0;
  57.             if (!$dst_height) $dst_height = 0;
  58.  
  59.         } else {
  60.  
  61.             if (!$dst_width)
  62.                 $dst_width = $src_width;
  63.             if (!$dst_height)
  64.                 $dst_height = $src_height;
  65.         }  
  66.  
  67.  
  68.         $src_image = ImageManager::create($type, $src_file);
  69.  
  70.         $width_diff = $dst_width / $src_width;
  71.         $height_diff = $dst_height / $src_height;
  72.  
  73.         if ($zoomCrop==true) {
  74.          
  75.             if ($dst_width>0 && $dst_height<1) {
  76.                 $dst_height = floor ($src_height * ($dst_width / $src_width));
  77.             } else if ($dst_height>0 && $dst_width<1) {
  78.                 $dst_width = floor ($src_width * ($dst_height / $src_height));
  79.             }
  80.                  
  81.             $src_x = $src_y = 0;
  82.             $src_w = $src_width;
  83.             $src_h = $src_height;
  84.            
  85.             $cmp_x = $src_width / $dst_width;
  86.             $cmp_y = $src_height / $dst_height;
  87.            
  88.             if ($cmp_x > $cmp_y) {
  89.            
  90.                 $src_w = round (($src_width / $cmp_x * $cmp_y));
  91.                 $src_x = round (($src_width - ($src_width / $cmp_x * $cmp_y)) / 2);
  92.            
  93.             } else if ($cmp_y > $cmp_x) {
  94.            
  95.                 $src_h = round (($src_height / $cmp_y * $cmp_x));
  96.                 $src_y = round (($src_height - ($src_height / $cmp_y * $cmp_x)) / 2);
  97.            
  98.             }
  99.                        
  100.         }
  101.  
  102.         else if ($width_diff > 1 && $height_diff > 1)
  103.         {
  104.             $next_width = $src_width;
  105.             $next_height = $src_height;
  106.         }
  107.         else
  108.         {
  109.             if (Configuration::get('PS_IMAGE_GENERATION_METHOD') == 2 || (!Configuration::get('PS_IMAGE_GENERATION_METHOD') && $width_diff > $height_diff))
  110.             {
  111.                 $next_height = $dst_height;
  112.                 $next_width = round(($src_width * $next_height) / $src_height);
  113.                 $dst_width = (int)(!Configuration::get('PS_IMAGE_GENERATION_METHOD') ? $dst_width : $next_width);
  114.             }
  115.             else
  116.             {
  117.                 $next_width = $dst_width;
  118.                 $next_height = round($src_height * $dst_width / $src_width);
  119.                 $dst_height = (int)(!Configuration::get('PS_IMAGE_GENERATION_METHOD') ? $dst_height : $next_height);
  120.             }
  121.         }
  122.  
  123.         $dest_image = imagecreatetruecolor($dst_width, $dst_height);
  124.  
  125.         // If image is a PNG and the output is PNG, fill with transparency. Else fill with white background.
  126.         if ($file_type == 'png' && $type == IMAGETYPE_PNG)
  127.         {
  128.             imagealphablending($dest_image, false);
  129.             imagesavealpha($dest_image, true);
  130.             $transparent = imagecolorallocatealpha($dest_image, 255, 255, 255, 127);
  131.             imagefilledrectangle($dest_image, 0, 0, $dst_width, $dst_height, $transparent);
  132.         }
  133.         else
  134.         {
  135.             $white = imagecolorallocate($dest_image, 255, 255, 255);
  136.             imagefilledrectangle ($dest_image, 0, 0, $dst_width, $dst_height, $white);
  137.         }
  138.  
  139.         if ($zoomCrop==true)           
  140.             imagecopyresampled($dest_image, $src_image, 0, 0, $src_x, $src_y, $dst_width, $dst_height, $src_w, $src_h);
  141.         else
  142.             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);
  143.        
  144.         return (ImageManager::write($file_type, $dest_image, $dst_file));
  145.     }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement