Beispiel #1
0
 function getThumbnail($filename, $size = null, $options = array(), $relativePath = true, $cachePath = null)
 {
     $config =& hikashop_config();
     $scalemode = 'inside';
     $jconf = JFactory::getConfig();
     $jdebug = $jconf->get('debug');
     $ret = new stdClass();
     $ret->success = false;
     $ret->path = $filename;
     $ret->height = 0;
     $ret->width = 0;
     $ret->req_height = 0;
     $ret->req_width = 0;
     $fullFilename = $filename;
     if ($relativePath === true) {
         $fullFilename = $this->uploadFolder . $filename;
     }
     if (is_string($relativePath)) {
         $fullFilename = $relativePath . $filename;
     }
     $clean_filename = $fullFilename;
     try {
         $clean_filename = JPath::clean(realpath($fullFilename));
         if (JPATH_ROOT != '' && strpos($clean_filename, JPath::clean(JPATH_ROOT)) !== 0) {
             if (!defined('MULTISITES_MASTER_ROOT_PATH') || MULTISITES_MASTER_ROOT_PATH == '' || strpos($clean_filename, JPath::clean(MULTISITES_MASTER_ROOT_PATH)) !== 0) {
                 return $ret;
             }
         }
     } catch (Exception $e) {
     }
     if ($cachePath !== false && empty($cachePath)) {
         $cachePath = $this->uploadFolder;
     } else {
         if ($cachePath !== false) {
             $cachePath = rtrim(JFolder::cleanPath($cachePath), DS) . DS;
         }
     }
     if (!JFile::exists($fullFilename)) {
         if ($jdebug && !empty($filename)) {
             $p = JProfiler::getInstance('Application');
             $dbgtrace = debug_backtrace();
             $dbgfile = str_replace('\\', '/', $dbgtrace[0]['file']);
             $dbgline = $dbgtrace[0]['line'];
             unset($dbgtrace);
             $p->mark('HikaShop image [' . $fullFilename . '] does not exists (from: ' . substr($dbgfile, strrpos($dbgfile, '/') + 1) . ':' . $dbgline . ')');
         }
         if (!isset($options['default'])) {
             return $ret;
         }
         $ret->path = $filename = $config->get('default_image');
         if ($ret->path == 'barcode.png') {
             $fullFilename = HIKASHOP_MEDIA . 'images' . DS . $ret->path;
             $ret->url = HIKASHOP_IMAGES . '/' . $ret->path;
             $ret->origin_url = HIKASHOP_IMAGES . '/' . $ret->path;
             $ret->filename = $ret->path;
         } else {
             $fullFilename = $this->uploadFolder . $ret->path;
         }
         if (!JFile::exists($fullFilename)) {
             return $ret;
         }
         $clean_filename = JPath::clean(realpath($fullFilename));
         unset($ret->url);
         unset($ret->filename);
     }
     if (empty($size) || !is_array($size) || !isset($size['x']) && !isset($size[0]) && !isset($size['width'])) {
         $size = array('x' => (int) $config->get('thumbnail_x', 100), 'y' => (int) $config->get('thumbnail_y', 100));
     }
     if (isset($size['width'])) {
         $size = array('x' => (int) $size['width'], 'y' => (int) $size['height']);
     }
     if (!isset($size['x'])) {
         $size = array('x' => (int) $size[0], 'y' => (int) $size[1]);
     }
     $optString = '';
     if (!empty($options['forcesize'])) {
         $optString .= 'f';
     }
     if (!empty($options['grayscale'])) {
         $optString .= 'g';
     }
     if (!empty($options['blur'])) {
         $optString .= 'b';
     }
     if (!empty($options['scale'])) {
         switch ($options['scale']) {
             case 'outside':
                 $scalemode = 'outside';
                 $optString .= 'sO';
             case 'inside':
                 break;
         }
     }
     if (!empty($options['background']) && is_string($options['background']) && strtolower($options['background']) != '#ffffff') {
         $optString .= 'c' . trim(strtoupper($options['background']), '#');
     }
     if (!empty($options['radius']) && (int) $options['radius'] > 2) {
         $optString .= 'r' . (int) $options['radius'];
     }
     $destFolder = 'thumbnails' . DS . $size['y'] . 'x' . $size['x'] . $optString;
     $ret->req_height = $size['y'];
     $ret->req_width = $size['x'];
     $extension = strtolower(substr($filename, strrpos($filename, '.') + 1));
     $origin = new stdClass();
     if ($extension == 'svg') {
         $scaling = false;
         $origin->width = $ret->req_width;
         $origin->height = $ret->req_height;
         $options['forcesize'] = false;
     } else {
         list($origin->width, $origin->height) = getimagesize($clean_filename);
         $ret->orig_height = $origin->height;
         $ret->orig_width = $origin->width;
         $scaling = $this->scaleImage($origin->width, $origin->height, $size['x'], $size['y'], $scalemode);
         if ($scaling !== false) {
             $this->thumbnail_x = $scaling[0];
             $this->thumbnail_y = $scaling[1];
         } else {
             $this->thumbnail_x = $origin->width;
             $this->thumbnail_y = $origin->height;
         }
         if (empty($size['x'])) {
             $size['x'] = $scaling[0];
         }
         if (empty($size['y'])) {
             $size['y'] = $scaling[1];
         }
         if ($cachePath !== false && JFile::exists($cachePath . $destFolder . DS . $filename)) {
             $ret->success = true;
             $ret->path = $destFolder . DS . $filename;
             $ret->filename = $filename;
             $ret->url = $this->uploadFolder_url . str_replace(array('\\/', '\\', '//'), '/', $ret->path);
             if (empty($ret->origin_url)) {
                 $ret->origin_url = $this->uploadFolder_url . str_replace(array('\\/', '\\', '//'), '/', $filename);
             }
             list($ret->width, $ret->height) = getimagesize($cachePath . $destFolder . DS . $filename);
             return $ret;
         }
     }
     if ($scaling === false && empty($options['forcesize'])) {
         $ret->success = true;
         $ret->width = $origin->width;
         $ret->height = $origin->height;
         $ret->filename = $filename;
         $ret->url = $this->uploadFolder_url . str_replace(array('\\/', '\\', '//'), '/', $ret->path);
         if (empty($ret->origin_url)) {
             $ret->origin_url = $this->uploadFolder_url . str_replace(array('\\/', '\\', '//'), '/', $filename);
         }
         return $ret;
     }
     unset($ret->url);
     if ($scaling === false) {
         $scaling = array($origin->width, $origin->height);
     }
     $quality = array('jpg' => 95, 'png' => 9);
     if (!empty($options['quality'])) {
         if (is_array($options['quality'])) {
             if (!empty($options['quality']['jpg'])) {
                 $quality['jpg'] = (int) $options['quality']['jpg'];
             }
             if (!empty($options['quality']['png'])) {
                 $quality['png'] = (int) $options['quality']['png'];
             }
         } elseif ((int) $options['quality'] > 0) {
             $quality['jpg'] = (int) $options['quality'];
         }
     }
     if ($config->get('image_check_memory', 1)) {
         static $memory_limit = null;
         if ($memory_limit === null) {
             $memory_limit = ini_get('memory_limit');
             if (preg_match('/^(\\d+)\\s*(.)$/', $memory_limit, $matches)) {
                 $m = array('G' => 1073741824, 'M' => 1048576, 'K' => 1024);
                 $unit = strtoupper($matches[2]);
                 if (isset($m[$unit])) {
                     $memory_limit = (int) $matches[1] * $m[$unit];
                 } else {
                     $memory_limit = 0;
                 }
             }
             $memory_limit = (int) $memory_limit;
         }
         if ($memory_limit > 0) {
             $rest = $memory_limit - memory_get_usage();
             $e_x = empty($options['forcesize']) ? $scaling[0] : $size['x'];
             $e_y = empty($options['forcesize']) ? $scaling[1] : $size['y'];
             $estimation = ($origin->width * $origin->height + $e_x * $e_y) * 8;
             if ($estimation > $rest) {
                 $ret->success = false;
                 $app = JFactory::getApplication();
                 $app->enqueueMessage(JText::sprintf('WARNING_IMAGE_TOO_BIG_FOR_MEMORY', $filename));
                 return $ret;
             }
         }
     }
     $img = $this->_getImage($fullFilename, $extension);
     if (!$img) {
         return false;
     }
     $transparentIndex = imagecolortransparent($img);
     if (in_array($extension, array('gif', 'png'))) {
         imagealphablending($img, false);
         imagesavealpha($img, true);
     }
     if (empty($options['forcesize'])) {
         $thumb = imagecreatetruecolor($scaling[0], $scaling[1]);
     } else {
         $thumb = imagecreatetruecolor($size['x'], $size['y']);
     }
     $bgcolor = $this->_getBackgroundColor($thumb, @$options['background']);
     if (in_array($extension, array('gif', 'png'))) {
         $palletSize = imagecolorstotal($img);
         if ($transparentIndex >= 0 && $transparentIndex < $palletSize) {
             $trnprt_color = imagecolorsforindex($img, $transparentIndex);
             $color = imagecolorallocate($thumb, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
             imagecolortransparent($thumb, $color);
             imagefill($thumb, 0, 0, $color);
         } elseif ($extension == 'png') {
             imagealphablending($thumb, false);
             $color = imagecolorallocatealpha($thumb, 0, 0, 0, 127);
             imagefill($thumb, 0, 0, $color);
             imagesavealpha($thumb, true);
         }
     } else {
         imagefill($thumb, 0, 0, $bgcolor);
     }
     if (function_exists('imageantialias')) {
         imageantialias($thumb, true);
     }
     $x = 0;
     $y = 0;
     $sx = $scaling[0];
     $sy = $scaling[1];
     if (!empty($options['forcesize'])) {
         $x = ($size['x'] - $scaling[0]) / 2;
         $y = ($size['y'] - $scaling[1]) / 2;
     } else {
         if ($origin->width < $sx) {
             $sx = $origin->width;
         }
         if ($origin->height < $sy) {
             $sy = $origin->height;
         }
     }
     if (function_exists('imagecopyresampled')) {
         imagecopyresampled($thumb, $img, $x, $y, 0, 0, $sx, $sy, $origin->width, $origin->height);
     } else {
         imagecopyresized($thumb, $img, $x, $y, 0, 0, $sx, $sy, $origin->width, $origin->height);
     }
     if (!empty($options['radius']) && (int) $options['radius'] > 2) {
         $radius = (int) $options['radius'];
         $corner_image = imagecreatetruecolor($radius, $radius);
         imagealphablending($corner_image, false);
         imagesavealpha($corner_image, true);
         $bgcolor = $this->_getBackgroundColor($corner_image, @$option['background']);
         $color = imagecolorallocatealpha($corner_image, 0, 0, 0, 127);
         imagecolortransparent($corner_image, $color);
         imagefill($corner_image, 0, 0, $bgcolor);
         imagefilledellipse($corner_image, $radius, $radius, $radius * 2, $radius * 2, $color);
         imagecopymerge($thumb, $corner_image, 0, 0, 0, 0, $radius, $radius, 100);
         $corner_image = imagerotate($corner_image, 90, 0);
         imagecopymerge($thumb, $corner_image, 0, $scaling[1] - $radius, 0, 0, $radius, $radius, 100);
         $corner_image = imagerotate($corner_image, 90, 0);
         imagecopymerge($thumb, $corner_image, $scaling[0] - $radius, $scaling[1] - $radius, 0, 0, $radius, $radius, 100);
         $corner_image = imagerotate($corner_image, 90, 0);
         imagecopymerge($thumb, $corner_image, $scaling[0] - $radius, 0, 0, 0, $radius, $radius, 100);
     }
     if (function_exists('imagefilter')) {
         if (!empty($options['grayscale'])) {
             imagefilter($thumb, IMG_FILTER_GRAYSCALE);
         }
         if (!empty($options['blur'])) {
             imagefilter($thumb, IMG_FILTER_GAUSSIAN_BLUR);
         }
     }
     ob_start();
     switch ($extension) {
         case 'gif':
             $status = imagegif($thumb);
             break;
         case 'jpg':
         case 'jpeg':
             $status = imagejpeg($thumb, null, $quality['jpg']);
             break;
         case 'png':
             $status = imagepng($thumb, null, $quality['png']);
             break;
     }
     imagedestroy($img);
     @imagedestroy($thumb);
     $imageContent = ob_get_clean();
     if ($cachePath === false) {
         $ret->success = $status;
         $ret->data = $imageContent;
         return $ret;
     }
     $ret->success = $status && JFile::write($cachePath . $destFolder . DS . $filename, $imageContent);
     if ($ret->success) {
         list($ret->width, $ret->height) = getimagesize($cachePath . $destFolder . DS . $filename);
         $ret->path = $destFolder . DS . $filename;
         $ret->filename = $filename;
         $ret->url = $this->uploadFolder_url . str_replace(array('\\/', '\\', '//'), '/', $ret->path);
         if (empty($ret->origin_url)) {
             $ret->origin_url = $this->uploadFolder_url . str_replace(array('\\/', '\\', '//'), '/', $filename);
         }
     } else {
         static $image_generation_warning = null;
         if ($image_generation_warning === null) {
             $app = JFactory::getApplication();
             $app->enqueueMessage(JText::sprintf('WRITABLE_FOLDER', $cachePath . $destFolder), 'error');
             $image_generation_warning = true;
         }
     }
     return $ret;
 }