[phpBB Debug] PHP Warning: in file [ROOT]/includes/crs/crs_misc_functions.php on line 37: mime_content_type(): Empty filename or path
[phpBB Debug] PHP Warning: in file [ROOT]/includes/crs/crs_misc_functions.php on line 37: mime_content_type(): Empty filename or path
Zen Cart 源代码 bmz_image_handler.class.php

Zen Cart 源代码 bmz_image_handler.class.php




下载文件

文件名: bmz_image_handler.class.php
文件类型: PHP文件
文件大小: 31.3 KiB
MD5: 64a30e494bdd8dcdbd183a557e2b3ca9

bmz_image_handler.class.php - 关闭高亮
  1. <?php
  2. /**
  3.  * bmz_image_handler.class.php
  4.  * IH2 class for image manipulation
  5.  *
  6.  * @author  Tim Kroeger (original author)
  7.  * @copyright Copyright 2005-2006
  8.  * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License V2.0
  9.  * @version $Id: bmz_image_handler.class.php,v 2.0 Rev 8 2010-05-31 23:46:5 DerManoMann Exp $
  10.  * modified by yellow1912 (rubikintegration.com)
  11.  * Last modified by DerManoMann 2010-05-31 23:40:21
  12.  */
  13.  
  14. require_once('bmz_gif_info.class.php');
  15.  
  16. class ih_image{
  17.  
  18.     /**
  19.    * $orig is the original image source passed to the constructor
  20.    * $src = is the reference to an actual physical image
  21.      * $local is the cached image reference
  22.      */
  23.   var $orig = null;
  24.     var $src = null;
  25.   var $local = null;
  26.     var $filename;
  27.     var $extension;
  28.     var $width;
  29.     var $height;
  30.     var $sizetype;
  31.     var $canvas;
  32.     var $zoom;
  33.     var $watermark;
  34.   var $force_canvas;
  35.  
  36. /**
  37.  * ih_image class constructor
  38.  * @author Tim Kroeger (tim@breakmyzencart.com)
  39.  * @version 1.99
  40.  * @param string $src Image source (e.g. - images/productimage.jpg)
  41.  * @param string $width The image's width
  42.  * @param string $height The image's height
  43.  */
  44.  
  45.     function ih_image($src, $width, $height){
  46.         global $ihConf;
  47.        
  48.    
  49.     $this->orig = $src;
  50.     $this->src = $src;
  51.     $this->width = $width;
  52.     $this->height = $height;
  53.     $this->zoom = array();
  54.    
  55.         $this->determine_image_sizetype();
  56.    
  57.     if ((($this->sizetype == 'large') || ($this->sizetype == 'medium')) && $this->file_not_found()) {
  58.       // large or medium image specified but not found. strip superflous suffix.
  59.       // now we can actually access the default image referenced in the database.
  60.           $this->src = $this->strip_sizetype_suffix($this->src);
  61.     }
  62.         $this->filename = $ihConf['dir']['docroot'] . $this->src;
  63.         $this->extension = substr($this->src, strrpos($this->src, '.'));
  64.  
  65.         list($newwidth, $newheight, $resize) = $this->calculate_size($this->width, $this->height);
  66.         // set canvas dimensions
  67.         if (($newwidth > 0) && ($newheight > 0)) {
  68.             $this->canvas['width'] = $newwidth;
  69.             $this->canvas['height'] = $newheight;
  70.         }
  71.  
  72.         // initialize overlays (watermark, zoom overlay)
  73.         $this->initialize_overlays($this->sizetype);
  74.     } # end class constructor
  75.  
  76.   function file_not_found() {
  77.     global $ihConf;
  78.     // try to find file by using different file extensions if initial
  79.     // source doesn't succeed
  80.     if (is_file($ihConf['dir']['docroot'] . $this->src)) {
  81.       return false;
  82.     } else {
  83.       // do a quick search for files with common extensions
  84.       $extensions = array('.png', '.PNG', '.jpg', '.JPG', '.jpeg', '.JPEG', '.gif', '.GIF');
  85.       $base = substr($this->src, 0, strrpos($this->src, '.'));
  86.       for ($i=0; $i<count($extensions); $i++) {
  87.         if (is_file($ihConf['dir']['docroot'] . $base . $extensions[$i])) {
  88.           $this->src = $base . $extensions[$i];
  89.           return false;
  90.         }
  91.       }
  92.       // not found? maybe mixed case file extension?
  93.       if ($ihConf['allow_mixed_case_ext']) {
  94.         // this can cost some time for every displayed image so default is
  95.         // to not do this search
  96.         $directory = dirname($ihConf['dir']['docroot'] . $this->src);
  97.         $dir = @dir($directory);
  98.         while ($file = $dir->read()) {
  99.           if (!is_dir($directory . $file)) {
  100.             if(preg_match("/^" . $ihConf['dir']['docroot'] . $base . "/i", $file) == '1') {
  101.               $file_ext = substr($file, strrpos($file, '.'));
  102.               if (is_file($ihConf['dir']['docroot'] . $base . $file_ext)) {
  103.                 $this->src = $base . $file_ext;
  104.                 return false;
  105.               }
  106.             }
  107.           }
  108.         }
  109.       }
  110.     }
  111.     // still here? no file found...
  112.     return true;
  113.   }
  114.  
  115.   function is_real() {
  116.     // return true if the source images are really present and medium
  117.     // or large are not just a descendant from the default image.
  118.     // small default images always return true.
  119.    
  120.     // strip file extensions, they don't matter
  121.     $orig = substr($this->orig, 0, strrpos($this->orig, '.'));
  122.     $src = substr($this->src, 0, strrpos($this->src, '.'));
  123.     return ($orig == $src);
  124.   }
  125.  
  126.     function determine_image_sizetype() {
  127.         global $ihConf;
  128.        
  129.         if (strstr($this->src, $ihConf['large']['suffix'])) {
  130.             $this->sizetype = 'large';
  131.         } elseif (strstr($this->src, $ihConf['medium']['suffix'])) {
  132.             $this->sizetype = 'medium';
  133.         } elseif ((intval($this->width) == intval($ihConf['small']['width'])) && (intval($this->height) == intval($ihConf['small']['height']))) {
  134.             $this->sizetype = 'small';
  135.         } else
  136.         $this->sizetype = 'generic';
  137.     }
  138.  
  139.     function strip_sizetype_suffix($src) {
  140.     global $ihConf;
  141.         $src = preg_replace('/' . $ihConf['large']['suffix'] . '\./', '.', $src);
  142.         $src = preg_replace('/' . $ihConf['medium']['suffix'] . '\./', '.', $src);
  143.         $src = str_replace($ihConf['medium']['prefix'] . '/', '/', $src);
  144.         $src = str_replace($ihConf['large']['prefix'] . '/', '/', $src);
  145.     return $src;
  146.     }
  147.    
  148.     function initialize_overlays($sizetype) {
  149.         global $ihConf;
  150.        
  151.         switch ($sizetype) {
  152.             case 'large':
  153.                 $this->watermark['file'] = ($ihConf['large']['watermark']) ? $ihConf['dir']['docroot'] . $ihConf['dir']['images'] . 'large/watermark' . $ihConf['large']['suffix'] . '.png' : '';
  154.                 $this->zoom['file'] = (isset($ihConf['large']['zoom'])&&$ihConf['large']['zoom']) ? $ihConf['dir']['docroot'] . $ihConf['dir']['images'] . 'large/zoom' . $ihConf['large']['suffix'] . '.png' : '';
  155.                 break;
  156.             case 'medium':
  157.                 $this->watermark['file'] = ($ihConf['medium']['watermark']) ? $ihConf['dir']['docroot'] . $ihConf['dir']['images'] . 'medium/watermark' . $ihConf['medium']['suffix'] . '.png': '';
  158.                 $this->zoom['file'] = (isset($ihConf['large']['zoom'])&&$ihConf['medium']['zoom']) ? $ihConf['dir']['docroot'] . $ihConf['dir']['images'] . 'medium/zoom' . $ihConf['medium']['suffix'] . '.png' : '';
  159.                 break;
  160.             case 'small':
  161.                 $this->watermark['file'] = ($ihConf['small']['watermark']) ? $ihConf['dir']['docroot'] . $ihConf['dir']['images'] . 'watermark.png' : '';
  162.                 $this->zoom['file'] = (isset($ihConf['large']['zoom'])&&$ihConf['small']['zoom']) ? $ihConf['dir']['docroot'] . $ihConf['dir']['images'] . 'zoom.png' : '';
  163.                 break;
  164.             default:
  165.                 $this->watermark['file'] = '';
  166.                 $this->zoom['file'] = '';
  167.                 break;
  168.         }
  169.  
  170.         if (($this->watermark['file'] != '') && is_file($this->watermark['file'])) {
  171.         // set watermark parameters
  172.             list($this->watermark['width'], $this->watermark['height']) = @getimagesize($this->watermark['file']);
  173.             list($this->watermark['startx'], $this->watermark['starty']) = $this->calculate_gravity($this->canvas['width'], $this->canvas['height'], $this->watermark['width'], $this->watermark['height'], $ihConf['watermark']['gravity']);
  174.             //echo '(' . $this->watermark['startx'] . ', ' . $this->watermark['starty'] . ') ' . $this->watermark['width'] . 'x' . $this->watermark['height'] . '<br />';
  175.         } else {
  176.             $this->watermark['file'] = '';
  177.         }
  178.        
  179.         if (($this->zoom['file'] != '') && is_file($this->zoom['file'])) {
  180.         // set zoom parameters
  181.             list($this->zoom['width'], $this->zoom['height']) = @getimagesize($this->zoom['file']);
  182.             list($this->zoom['startx'], $this->zoom['starty']) = $this->calculate_gravity($this->canvas['width'], $this->canvas['height'], $this->zoom['width'], $this->zoom['height'], $ihConf['zoom']['gravity']);
  183.             //echo '(' . $this->zoom['startx'] . ', ' . $this->zoom['starty'] . ') ' . $this->zoom['width'] . 'x' . $this->zoom['height'] . '<br />';
  184.         } else {
  185.             $this->zoom['file'] = '';
  186.         }
  187.     }
  188.    
  189.     function get_local() {
  190.         if ($this->local) return $this->local;
  191.         // check if image handler is available and if we should resize at all
  192.         if ($this->resizing_allowed()) {
  193.             $this->local = $this->get_resized_image($this->width, $this->height);
  194.         } else {
  195.       $this->local = $this->src;
  196.     }
  197.         return $this->local;
  198.     }
  199.  
  200.   function resizing_allowed() {
  201.     global $bmzConf;
  202.     global $ihConf;
  203.     // only resize if resizing is turned on
  204.     // don't resize template images so test for the configured images directory.
  205.     // if $ihConf['noresize_key'] is found within the string, don't resize either.
  206.     $allowed = false;
  207.     if ($ihConf['resize'] &&
  208.         ((strpos($this->src, $ihConf['dir']['images']) === 0) ||
  209.          ((strpos($this->src, substr($bmzConf['cachedir'], strlen($ihConf['dir']['docroot']))) === 0))) &&
  210.         (strpos($this->src, $ihConf['noresize_key']) === false)) {
  211.       $allowed = true;
  212.       for ($i=0; $i++; $i<count($ihConf)) {
  213.         $allowed &= (strpos($this->src, $ihConf['dir']['images'] . $ihConf['noresize_dirs'][$i] . '/') !== 0);
  214.       }
  215.     }
  216.     return $allowed;
  217.   }
  218.  
  219.     function get_resized_image($width, $height, $override_sizetype = '', $filetype = '') {
  220.         global $ihConf;
  221.  
  222.         $sizetype = ($override_sizetype == '') ? $this->sizetype : $override_sizetype;
  223.         switch ($sizetype) {
  224.       case 'large':
  225.         $file_extension = (($ihConf['large']['filetype'] == 'no_change') ? $this->extension : '.' . $ihConf['large']['filetype']);
  226.         $background = $ihConf['large']['bg'];
  227.         $quality = $ihConf['large']['quality'];
  228.         $width = $ihConf['large']['width'];
  229.         $height = $ihConf['large']['height'];
  230.         break;
  231.       case 'medium':
  232.         $file_extension = (($ihConf['medium']['filetype'] == 'no_change') ? $this->extension : '.' . $ihConf['medium']['filetype']);
  233.         $background = $ihConf['medium']['bg'];
  234.         $quality = $ihConf['medium']['quality'];
  235.         break;
  236.       case 'small':
  237.         $file_extension = (($ihConf['small']['filetype'] == 'no_change') ? $this->extension : '.' . $ihConf['small']['filetype']);
  238.         $background = $ihConf['small']['bg'];
  239.         $quality = $ihConf['small']['quality'];
  240.         break;
  241.       default:
  242.         $file_extension = $this->extension;
  243.         $background = $ihConf['default']['bg'];
  244.         $quality = $ihConf['default']['quality'];
  245.         break;
  246.         }
  247.         list($newwidth, $newheight, $resize) = $this->calculate_size($width, $height);
  248.         // set canvas dimensions
  249.         if (($newwidth > 0) && ($newheight > 0)) {
  250.             $this->canvas['width'] = $newwidth;
  251.             $this->canvas['height'] = $newheight;
  252.         }
  253.        
  254.         $this->initialize_overlays($sizetype);
  255.        
  256.         // override filetype?
  257.         $file_extension = ($filetype == '') ? $file_extension : $filetype;
  258.        
  259.         // Do we need to resize, watermark, zoom or convert to another filetype?
  260.         if ($resize || ($this->watermark['file'] != '') || ($this->zoom['file'] != '') || ($file_extension != $this->extension)){
  261.             $local = getCacheName($this->src . $this->watermark['file'] . $this->zoom['file'] . $quality . $background . $ihConf['watermark']['gravity'] . $ihConf['zoom']['gravity'], '.image.' . $newwidth . 'x' . $newheight . $file_extension);
  262.             //echo $local . '<br />';  
  263.             $mtime = @filemtime($local); // 0 if not exists
  264.             if ( (($mtime > @filemtime($this->filename)) && ($mtime > @filemtime($this->watermark['file'])) && ($mtime > @filemtime($this->zoom['file'])) ) ||
  265.                 $this->resize_imageIM($file_extension, $local, $background, $quality) ||
  266.                 $this->resize_imageGD($file_extension, $local, $background, $quality) ) {
  267.                 return str_replace($ihConf['dir']['docroot'], '', $local);
  268.             }
  269.             //still here? resizing failed
  270.         }
  271.         return $this->src;
  272.     }
  273.    
  274.     /**
  275.      * Calculate desired image size as set in admin->configuration->images.
  276.      */
  277.     function calculate_size($pref_width, $pref_height = '') {
  278.         list($width, $height) = @getimagesize($this->filename);
  279.         // default: nothing happens (preferred dimension = actual dimension)
  280.         $newwidth = $width;
  281.         $newheight = $height;
  282.         if (($width > 0) && ($height > 0)) {
  283.             if ((strrpos($pref_width . $pref_height, '%') !== false)) {
  284.                 // possible scaling to % of original size
  285.                 // calculate new dimension in pixels
  286.                 if (($pref_width !== '') && ($pref_height != '')) {
  287.                     // different factors for width and height
  288.                     $hscale = intval($pref_width) / 100;
  289.                     $vscale = intval($pref_height) / 100;
  290.                 } else {
  291.                     // one of the the preferred values has the scaling factor
  292.                     $hscale = intval($pref_width . $pref_height) / 100;
  293.                     $vscale = $hscale;
  294.                 }
  295.                 $newwidth = floor($width * $hscale);
  296.                 $newheight = floor($height * $vscale);
  297.             } else {
  298.                 $this->force_canvas = (strrpos($pref_width . $pref_height, '!') !== false);
  299.                 // failsafe for old zen-cart configuration one image dimension set to 0
  300.                 $pref_width = ($pref_width == '' || intval($pref_width) == 0) ? 0 : intval($pref_width);
  301.                 $pref_height = ($pref_height == '' || intval($pref_height) == 0) ? 0 : intval($pref_height);
  302.                 if ((!$this->force_canvas) && ($pref_width != 0) && ($pref_height != 0)) {
  303.                     // if no '!' is appended to dimensions we don't force the canvas size to
  304.                     // match the preferred size. the image will not have the exact specified size.
  305.                     // (we're in fact forcing the old 0-dimension zen-magic trick)
  306.                     $oldratio = $width / $height;
  307.                     $pref_ratio = $pref_width / $pref_height;
  308.                     if ($pref_ratio > $oldratio) {
  309.                         $pref_width = 0;
  310.                     } else {
  311.                         $pref_height = 0;
  312.                     }
  313.                 }
  314.                
  315.                 // now deal with the calculated preferred sizes
  316.                 if (($pref_width == 0) && ($pref_height > 0)) {
  317.                     // image dimensions are calculated to fit the preferred height
  318.                     $pref_width = floor($width * ($pref_height / $height));
  319.                 } elseif (($pref_width > 0) && ($pref_height == 0)) {
  320.                     // image dimensions are calculated to fit the preferred width
  321.                     $pref_height = floor($height * ($pref_width / $width));
  322.                 }
  323.         if ((($pref_width > 0) && ($pref_height > 0))
  324.           && (($pref_width < $width ) || ($pref_height < $height))) {
  325.                     // only calculate new dimensions if we have sane values
  326.                     $newwidth = $pref_width;
  327.                     $newheight = $pref_height;
  328.                 }
  329.             }
  330.         }
  331.         $resize = (($newwidth != $width) || ($newheight != $height));
  332.         return array($newwidth, $newheight, $resize);
  333.     }
  334.    
  335.     function resize_imageIM($file_ext, $dest_name, $bg, $quality = 85) {
  336.     global $ihConf;
  337.     global $messageStack;
  338.     //echo 'im_convert: ' . $ihConf['im_convert'] . '<br />';
  339.     // check if convert is configured
  340.     if(!$ihConf['im_convert']) return false;
  341.     //echo 'Trying to use ImageMagick.<br />';
  342.     $size = $this->canvas['width'] . 'x' . $this->canvas['height'];
  343.     //echo $size . '<br />';
  344.     $bg = trim($bg);
  345.     $bg = ($bg == '') ? $ihConf['default']['bg'] : $bg;
  346.     $transparent = (strpos($bg, 'transparent') !== false);
  347.     $transparent &= preg_match('/(\.gif)|(\.png)/i', $file_ext);
  348.     $color = $this->get_background_rgb($bg);
  349.     if ($color) {
  350.       $bg = 'rgb(' . $color['r'] . ',' .  $color['g'] . ',' . $color['b'] . ')';
  351.       $bg .= $transparent ? ' transparent' : '';
  352.     }
  353.     $gif_treatment = false;
  354.     if ($transparent && ($file_ext == ".gif")) {
  355.       // Special treatment for gif files
  356.       $bg = trim(str_replace('transparent', '', $bg));
  357.       $bg = ($bg != '') ? $bg : 'rgb(255,255,255)';
  358.       $temp_name = substr($dest_name, 0, strrpos($dest_name, '.')) . '-gif_treatment.png';
  359.       $gif_treatment = true;
  360.     } else {
  361.       $bg = (strpos($bg, 'transparent') === false) ? $bg : 'transparent';
  362.     }
  363.    
  364.     // still no background? default to transparent
  365.         $bg = ($bg != '') ? $bg : 'transparent';
  366.     $command  = $ihConf['im_convert'] . " -size $size ";
  367.     $command .= "xc:none -fill " . ($gif_treatment ? "transparent" : "\"$bg\"") . " -draw 'color 0,0 reset'";
  368.     $size .= $this->force_canvas ? '' : '!';
  369.     $command .= ' "' . $this->filename . '" -compose Over -gravity Center -geometry ' . $size . ' -composite';
  370.     $command .= ($this->watermark['file'] != '') ? ' "' . $this->watermark['file'] . '" -compose Over -gravity ' . $ihConf['watermark']['gravity'] . " -composite" : '';
  371.     $command .= ($this->zoom['file'] != '') ? ' "' . $this->zoom['file'] . '" -compose Over -gravity ' . $ihConf['zoom']['gravity'] . " -composite " : ' ';
  372.     $command .= $gif_treatment ? $temp_name : (preg_match("/\.jp(e)?g/i", $file_ext) ? "-quality $quality " : '') . "\"$dest_name\"";
  373.     @exec($command . ' 2>&1', $message, $retval);
  374.     if ($gif_treatment) {
  375.       if ($retval != 0) return false;
  376.       $command  = $ihConf['im_convert'] . " -size $size ";
  377.       $command .= "xc:none -fill \"$bg\" -draw 'color 0,0 reset'";
  378.       $command .= " \"$temp_name\" -compose Over -gravity Center -geometry $size -composite";
  379.       $command .= " \"$temp_name\" -channel Alpha -threshold " . $ihConf['trans_threshold'] . " -compose CopyOpacity -gravity Center -geometry $size -composite";
  380.       $command .= " \"$dest_name\"";
  381.       @exec($command . ' 2>&1', $message, $retval);
  382.     }
  383.     if ($retval == 0) return true;
  384.  
  385.         return false;
  386.     }
  387.  
  388.   function alphablend($background, $overlay, $threshold = -1) {
  389.     /* -------------------------------------------------------------------- */
  390.     /*      Simple cases we want to handle fast.                            */
  391.     /* -------------------------------------------------------------------- */
  392.     if ($overlay['alpha'] == 0) return $overlay;
  393.     if ($overlay['alpha'] == 127) return $background;
  394.     if (($background['alpha'] == 127) && ($threshold == -1)) return $overlay;
  395.  
  396.     /* -------------------------------------------------------------------- */
  397.     /*      What will the overlay and background alphas be?  Note that      */
  398.     /*      the background weighting is substantially reduced as the        */
  399.     /*      overlay becomes quite opaque.                                   */
  400.     /* -------------------------------------------------------------------- */
  401.     $alpha =  $overlay['alpha'] * $background['alpha'] / 127;
  402.     if (($threshold > -1) && ($alpha <= $threshold)) {
  403.       $background['alpha'] = 0;
  404.       $alpha = 0;
  405.     }
  406.  
  407.     $overlay_weight = 127 - $overlay['alpha'];
  408.     $background_weight = (127 - $background['alpha']) * $overlay['alpha'] / 127;
  409.     $total_weight = $overlay_weight + $background_weight;
  410.    
  411.     $red = (($overlay['red'] * $overlay_weight) + ($background['red'] * $background_weight)) / $total_weight;
  412.     $green = (($overlay['green'] * $overlay_weight) + ($background['green'] * $background_weight)) / $total_weight;
  413.     $blue = (($overlay['blue'] * $overlay_weight) + ($background['blue'] * $background_weight)) / $total_weight;
  414.    
  415.     return array('alpha'=>$alpha, 'red'=>$red, 'green'=>$green, 'blue'=>$blue);
  416.   }
  417.  
  418.   function imagemergealpha($background, $overlay, $startwidth, $startheight, $newwidth, $newheight, $threshold = '', $background_override = '', $debug = false) {
  419.     global $ihConf;
  420.  
  421.     //restore the transparency
  422.     if ($ihConf['gdlib']>1){
  423.       imagealphablending($background, false);
  424.     }
  425.    
  426.     $threshold = ($threshold != '') ? intval(127 * intval($threshold) / 100) : -1;
  427.    
  428.     for($x=0; $x<$newwidth; $x++) {
  429.       for($y=0; $y<$newheight; $y++) {
  430.         $c = imagecolorat($background, $x + $startwidth, $y + $startheight);
  431.         $background_color = imagecolorsforindex($background, $c);
  432.         //if ($debug) echo "($x/$y): " . $background_color['alpha'] . ':' . $background_color['red'] . ':' . $background_color['green'] . ':' . $background_color['blue'] . ' ++ ';
  433.         $c = imagecolorat($overlay, $x, $y);
  434.         $overlay_color = imagecolorsforindex($overlay, $c);
  435.         //if ($debug) echo $overlay_color['alpha'] . ':' . $overlay_color['red'] . ':' . $overlay_color['green'] . ':' . $overlay_color['blue'] . ' ==&gt; ';
  436.         $color = $this->alphablend($background_color, $overlay_color, $threshold);
  437.         //if ($debug) echo $color['alpha'] . ':' . $color['red'] . ':' . $color['green'] . ':' . $color['blue'] . '<br />';
  438.         //if ($threshold > -1) $color['alpha'] = ($color['alpha'] > $threshold) ? 127 : 0;
  439.  
  440.  
  441.         if (($threshold > -1) && ($color['alpha'] > $threshold)) {
  442.           $color = $background_override;
  443.         } else {
  444.           $color = imagecolorallocatealpha($background, $color['red'], $color['green'], $color['blue'], $color['alpha']);
  445.         }
  446.         imagesetpixel($background, $x + $startwidth, $y + $startheight, $color);
  447.       }
  448.     }
  449.     return $background;
  450.   }
  451.  
  452.  
  453.   function resize_imageGD($file_ext, $dest_name, $bg, $quality = 85) {
  454.     global $ihConf;
  455.     global $messageStack;
  456.  
  457.     if($ihConf['gdlib'] < 1) return false; //no GDlib available or wanted
  458.     $srcimage = $this->load_imageGD($this->filename);
  459.     if (!$srcimage) return false; // couldn't load image
  460.     $src_ext = substr($this->filename, strrpos($this->filename, '.'));
  461.     $srcwidth = imagesx($srcimage);
  462.     $srcheight = imagesy($srcimage);
  463.     if ($this->force_canvas) {
  464.       if (($srcwidth / $this->canvas['width']) > ($srcheight / $this->canvas['height'])) {
  465.         $newwidth = $this->canvas['width'];
  466.         $newheight = floor(($newwidth / $srcwidth) * $srcheight);
  467.        } else {
  468.         $newheight = $this->canvas['height'];
  469.         $newwidth = floor(($newheight / $srcheight) * $srcwidth);
  470.        }
  471.     } else {
  472.       $newwidth = $this->canvas['width'];
  473.       $newheight = $this->canvas['height'];
  474.     }
  475.     $startwidth = (($this->canvas['width'] - $newwidth)/2);
  476.     $startheight = (($this->canvas['height'] - $newheight)/2);
  477.  
  478.     if(($ihConf['gdlib']>1) && function_exists("imagecreatetruecolor")){
  479.       $tmpimg = @imagecreatetruecolor ($newwidth, $newheight);
  480.     }
  481.     if(!$tmpimg) $tmpimg = @imagecreate($newwidth, $newheight);
  482.     if(!$tmpimg) return false;
  483.    
  484.     //keep alpha channel if possible
  485.     if ($ihConf['gdlib']>1 && function_exists('imagesavealpha')){
  486.       imagealphablending($tmpimg, false);
  487.     }
  488.     //try resampling first
  489.     if(function_exists("imagecopyresampled")){
  490.       if(!@imagecopyresampled($tmpimg, $srcimage, 0, 0, 0, 0, $newwidth, $newheight, $srcwidth, $srcheight)) {
  491.         imagecopyresized($tmpimg, $srcimage, 0, 0, 0, 0, $newheight, $newwidth, $srcwidth, $srcheight);
  492.       }
  493.     } else {
  494.       imagecopyresized($tmpimg, $srcimage, 0, 0, 0, 0, $newwidth, $newheight, $srcwidth, $srcheight);
  495.     }
  496.    
  497.     imagedestroy($srcimage);
  498.    
  499.     // initialize FIRST background image (transparent canvas)
  500.     if(($ihConf['gdlib']>1) && function_exists("imagecreatetruecolor")){
  501.       $newimg = @imagecreatetruecolor ($this->canvas['width'], $this->canvas['height']);
  502.     }
  503.     if(!$newimg) $newimg = @imagecreate($this->canvas['width'], $this->canvas['height']);
  504.     if(!$newimg) return false;
  505.    
  506.     if ($ihConf['gdlib']>1 && function_exists('imagesavealpha')){
  507.       imagealphablending($newimg, false);
  508.     }
  509.     $background_color = imagecolorallocatealpha($newimg, 255, 255, 255, 127);
  510.     imagefilledrectangle($newimg, 0, 0, $this->canvas['width'] - 1, $this->canvas['height'] - 1, $background_color);
  511.  
  512.     //$newimg = $this->imagemergealpha($newimg, $tmpimg, $startwidth, $startheight, $newwidth, $newheight);
  513.     imagecopy($newimg, $tmpimg, $startwidth, $startheight, 0, 0, $newwidth, $newheight);
  514.     imagedestroy($tmpimg);
  515.     $tmpimg = $newimg;
  516.  
  517.  
  518.     if ($ihConf['gdlib']>1 && function_exists('imagesavealpha')){
  519.       imagealphablending($tmpimg, true);
  520.     }
  521.     // we need to watermark our images
  522.     if ($this->watermark['file'] != '') {
  523.       $this->watermark['image'] = $this->load_imageGD($this->watermark['file']);
  524.       imagecopy($tmpimg, $this->watermark['image'], $this->watermark['startx'], $this->watermark['starty'], 0, 0, $this->watermark['width'], $this->watermark['height']);
  525.       //$tmpimg = $this->imagemergealpha($tmpimg, $this->watermark['image'], $this->watermark['startx'], $this->watermark['starty'], $this->watermark['width'], $this->watermark['height']);
  526.       imagedestroy($this->watermark['image']);
  527.     }
  528.  
  529.     // we need to zoom our images
  530.     if ($this->zoom['file'] != '') {
  531.       $this->zoom['image'] = $this->load_imageGD($this->zoom['file']);
  532.       //imagecopy($tmpimg, $this->zoom['image'], $this->zoom['startx'], $this->zoom['starty'], 0, 0, $this->zoom['width'], $this->zoom['height']);
  533.       $tmpimg = $this->imagemergealpha($tmpimg, $this->zoom['image'], $this->zoom['startx'], $this->zoom['starty'], $this->zoom['width'], $this->zoom['height']);
  534.       imagedestroy($this->zoom['image']);
  535.     }
  536.  
  537.     // initialize REAL background image (filled canvas)
  538.     if(($ihConf['gdlib']>1) && function_exists("imagecreatetruecolor")){
  539.       $newimg = @imagecreatetruecolor ($this->canvas['width'], $this->canvas['height']);
  540.     }
  541.     if(!$newimg) $newimg = @imagecreate($this->canvas['width'], $this->canvas['height']);
  542.     if(!$newimg) return false;
  543.    
  544.     if ($ihConf['gdlib']>1 && function_exists('imagesavealpha')){
  545.       imagealphablending($newimg, false);
  546.     }
  547.  
  548.     // determine background
  549.     // default to white as "background" -> better rendering on bright pages
  550.     // when downsampling to gif with just boolean transparency
  551.     $color = $this->get_background_rgb($bg);
  552.     if (!$color) {
  553.       $color = $this->get_background_rgb($ihConf['default']['bg']);
  554.       $transparent = (strpos($ihConf['default']['bg'], 'transparent') !== false);
  555.     } else {
  556.       $transparent = (strpos($bg, 'transparent') !== false);
  557.     }
  558.     $transparent &= preg_match('/(\.gif)|(\.png)/i', $file_ext);
  559.    
  560.     $alpha = $transparent ? 127 : 0;
  561.     if ($color) {
  562.       $background_color = imagecolorallocatealpha($newimg, intval($color['r']), intval($color['g']), intval($color['b']), $alpha);
  563.     } else {
  564.       $background_color = imagecolorallocatealpha($newimg, 255, 255, 255, $alpha);
  565.     }
  566.     imagefilledrectangle($newimg, 0, 0, $this->canvas['width'] - 1, $this->canvas['height'] - 1, $background_color);
  567.  
  568.     if ($ihConf['gdlib']>1 && function_exists('imagesavealpha')){
  569.       imagealphablending($newimg, true);
  570.     }
  571.  
  572.     if (preg_match('/\.gif/i', $file_ext)) {
  573.       if ($transparent) {
  574.         $newimg = $this->imagemergealpha($newimg, $tmpimg, 0, 0, $this->canvas['width'], $this->canvas['height'], $ihConf['trans_threshold'], $background_color);
  575.         imagecolortransparent($newimg, $background_color);
  576.       } else {
  577.         imagecopy($newimg, $tmpimg, 0, 0, 0, 0, $this->canvas['width'], $this->canvas['height']);
  578.       }
  579.     } else {
  580.       if ($transparent) {
  581.         $newimg = $this->imagemergealpha($newimg, $tmpimg, 0, 0, $this->canvas['width'], $this->canvas['height']);
  582.       } else {
  583.         imagecopy($newimg, $tmpimg, 0, 0, 0, 0, $this->canvas['width'], $this->canvas['height']);
  584.       }
  585.     }
  586.     imagedestroy($tmpimg);
  587.  
  588.     if ($ihConf['gdlib']>1 && function_exists('imagesavealpha')){
  589.       imagesavealpha($newimg, true);
  590.     }
  591.  
  592.     if (preg_match('/\.gif/i', $file_ext)) {
  593.       if ($ihConf['gdlib']>1 && function_exists('imagetruecolortopalette')) {
  594.         imagetruecolortopalette($newimg, true, 256);
  595.       }
  596.     }
  597.  
  598.     return $this->save_imageGD($file_ext, $newimg, $dest_name, $quality);
  599.   }
  600.  
  601.     function calculate_gravity($canvaswidth, $canvasheight, $overlaywidth, $overlayheight, $gravity) {
  602.           // Calculate overlay position from gravity setting. Center as default.
  603.           $startheight = (($canvasheight - $overlayheight)/2);
  604.           $startwidth = (($canvaswidth - $overlaywidth)/2);
  605.           if (strpos($gravity, 'North') !== false) {
  606.             $startheight = 0;
  607.           } elseif (strpos($gravity, 'South') !== false) {
  608.             $startheight = $canvasheight - $overlayheight;
  609.           }
  610.           if (strpos($gravity, 'West') !== false) {
  611.             $startwidth = 0;
  612.           } elseif (strpos($gravity, 'East') !== false) {
  613.             $startwidth = $canvaswidth - $overlaywidth;
  614.           }
  615.           return array($startwidth, $startheight);
  616.     }
  617.    
  618.     function load_imageGD($src_name) {
  619.         // create an image of the given filetype
  620.         $file_ext = substr($src_name, strrpos($src_name, '.'));
  621.         switch (strtolower($file_ext)) {
  622.             case '.gif':
  623.                 if(!function_exists("imagecreatefromgif")) return false;
  624.                     $image = @imagecreatefromgif($src_name);
  625.                 break;
  626.             case '.png':
  627.                 if(!function_exists("imagecreatefrompng")) return false;
  628.                 $image = @imagecreatefrompng($src_name);
  629.                 break;
  630.             case '.jpg':
  631.             case '.jpeg':
  632.                 if(!function_exists("imagecreatefromjpeg")) return false;
  633.                 $image = @imagecreatefromjpeg($src_name);
  634.                 break;
  635.         }
  636.         return $image;
  637.     }
  638.    
  639.     function save_imageGD($file_ext, $image, $dest_name, $quality = 75) {
  640.         global $ihConf;
  641.        
  642.         switch (strtolower($file_ext)) {
  643.             case '.gif':
  644.                 if(!function_exists("imagegif")) return false;
  645.                 $ok = imagegif($image, $dest_name);
  646.                 break;
  647.             case '.png':
  648.                 if(!function_exists("imagepng")) return false;
  649.                 $quality = (int)$quality/100;
  650.                 $ok = imagepng($image, $dest_name, $quality);
  651.                 break;
  652.             case '.jpg':
  653.             case '.jpeg':
  654.                 if(!function_exists("imagejpeg")) return false;
  655.                 $ok = imagejpeg($image, $dest_name, $quality);
  656.                 break;
  657.             default: $ok = false;
  658.         }
  659.         imagedestroy($image);
  660.    
  661.         return $ok;
  662.     }
  663.    
  664.     function get_background_rgb($bg) {
  665.         $bg = trim(str_replace('transparent', '', $bg));
  666.         list($red, $green, $blue)= preg_split('/[, :]/', $bg);
  667.         if (preg_match('/[0-9]+/', $red.$green.$blue)) {
  668.             $red = min(intval($red), 255);
  669.             $green = min(intval($green), 255);
  670.             $blue = min(intval($blue), 255);
  671.             $color = array('r'=>$red, 'g'=>$green, 'b'=>$blue);
  672.             return $color;
  673.         } else {
  674.             return false;
  675.         }
  676.     }
  677.        
  678.     function get_additional_parameters($alt, $width, $height, $parameters) {
  679.         global $ihConf;
  680.     if ($this->sizetype == 'small') {
  681.       if ($ihConf[$this->sizetype]['zoom']) {
  682.         if ($this->zoom['file'] == '' || !$ihConf[$this->sizetype]['hotzone']) {
  683.           // if no zoom image, the whole image triggers the popup
  684.           $this->zoom['startx'] = 0;
  685.           $this->zoom['starty'] = 0;
  686.           $this->zoom['width'] = $width;
  687.           $this->zoom['height'] = $height;
  688.         }
  689.         //escape possible quotes if they're not already escapped
  690.         $alt = addslashes(htmlentities($alt, ENT_COMPAT, CHARSET));  
  691.        // strip potential suffixes just to be sure
  692.         $src = $this->strip_sizetype_suffix($this->src);
  693.         // define zoom sizetype
  694.         if (ZOOM_IMAGE_SIZE == 'Medium') {
  695.         $zoom_sizetype = ($this->sizetype=='small')?'medium':'large';  
  696.         } else {
  697.         $zoom_sizetype = ($this->sizetype=='small')?'large':'medium';
  698.         }
  699.         // additional zoom functionality
  700.         $products_image_directory = substr($src, strlen($ihConf['dir']['images']), strrpos($src, '/') - strlen($ihConf['dir']['images']) + 1);
  701.         $products_image_filename = substr($src, strrpos($src, '/') + 1, strlen ($src) - (strrpos ($src, '/') + 1) - strlen ($this->extension));  
  702.         $products_image_zoom = $ihConf['dir']['images'] . $zoom_sizetype . '/' . $products_image_directory . $products_image_filename . $ihConf[$zoom_sizetype]['suffix'] . $this->extension;
  703.         $ih_zoom_image = new ih_image($products_image_zoom, $ihConf[$zoom_sizetype]['width'], $ihConf[$zoom_sizetype]['height']);
  704.         $products_image_zoom = $ih_zoom_image->get_local();
  705.         list($zoomwidth, $zoomheight) = @getimagesize($ihConf['dir']['docroot'] . $products_image_zoom);
  706.         // we should parse old parameters here and possibly merge some inc case they're duplicate
  707.         $parameters .= ($parameters != '') ? ' ' : '';
  708.         return $parameters . 'style="position:relative" onmouseover="showtrail(' . "'$products_image_zoom','$alt',$width,$height,$zoomwidth,$zoomheight,this," . $this->zoom['startx'].','.$this->zoom['starty'].','.$this->zoom['width'].','.$this->zoom['height'].');" onmouseout="hidetrail();" ';
  709.       }
  710.        return $parameters;
  711.     }
  712.     return $parameters;
  713.     }  
  714. }
  715.