示例#1
0
 /**
  * Create thumbnail.
  * 
  * @param string $path absolute path to original image
  * @param int $cwidth max. thumbnail width
  * @param int $cheight max. thumbnail height
  * @return real path to thumnail
  */
 static function thumb($path, $cwidth = null, $cheight = null)
 {
     if (!$path) {
         $path = 'components' . DS . 'com_bookpro' . DS . 'assets' . DS . 'images' . DS . 'no_image.jpg';
     }
     $config = JComponentHelper::getParams('com_bookpro');
     /* @var $config BookingConfig */
     if (!is_file($path)) {
         return '';
     }
     if (!$cwidth && !$cheight) {
         return '';
     }
     static $thumbs;
     $aroot = JPATH_ROOT . '/cache/com_bookpro';
     $ini = $aroot . DS . 'thumbs.ini';
     if (is_null($thumbs)) {
         $thumbs = array();
         if (file_exists($ini)) {
             $thumbs = parse_ini_file($ini);
             if ($thumbs === false) {
                 $thumbs = array();
             }
         }
     }
     jimport('joomla.filesystem.file');
     //$name = JFile::getName($path);
     $name = basename($path);
     $ext = JFile::getExt($path);
     $ext = '.' . $ext;
     $names[] = str_replace($ext, '', $name);
     if ($cwidth) {
         $names[] = 'w' . $cwidth;
     }
     if ($cheight) {
         $names[] = 'h' . $cheight;
     }
     $names[] = $ext;
     $name = implode('', $names);
     if (isset($thumbs[$name])) {
         $rpath = $thumbs[$name];
         $apath = AImage::abspath($rpath);
         if (file_exists($apath)) {
             return $thumbs[$name];
         } else {
             unset($thumbs[$name]);
         }
     }
     $data = getimagesize($path);
     if ($data === false) {
         return '';
     }
     list($width, $height, $type) = $data;
     switch ($type) {
         case 1:
         case 2:
         case 3:
             break;
         default:
             return '';
     }
     if ($width <= $cwidth && $height <= $cheight) {
         $rpath = AImage::realpath($path);
         return $rpath;
     }
     if ($cwidth > $width) {
         $cwidth = $width;
     }
     if ($cheight > $height) {
         $cheight = $height;
     }
     $rroot = JURI::root() . 'cache/com_bookpro/';
     if (!file_exists($aroot)) {
         if (!@mkdir($aroot, 0775, true)) {
             return '';
         }
     }
     $crc = (string) crc32($name);
     $d = array();
     for ($i = 0; $i < CACHE_IMAGES_DEPTH; $i++) {
         $d[] = substr($crc, $i, 1);
     }
     $arthumb = $aroot . DS . implode(DS, $d) . DS;
     $athumb = $arthumb . $name;
     $rthumb = $rroot . implode('/', $d) . '/' . $name;
     if (file_exists($athumb)) {
         return $rthumb;
     }
     if (!file_exists($arthumb)) {
         if (!@mkdir($arthumb, 0775, true)) {
             return '';
         }
     }
     $destX = 0;
     $destY = 0;
     $srcX = 0;
     $srcY = 0;
     $nwidth = $cwidth;
     $nheight = $cheight;
     $sheight = $height;
     $swidth = $width;
     if ($cwidth && $cheight) {
         $percent = $cwidth / $cheight;
         $sheight = round($width / $percent);
         if ($sheight > $height) {
             $sheight = $height;
             $swidth = round($height * $percent);
         }
         $srcX = round(($width - $swidth) / 2);
         $srcY = round(($height - $sheight) / 2);
     } elseif ($cwidth) {
         $nheight = round($height * $nwidth / $width);
     } elseif ($cheight) {
         $nwidth = round($width * $nheight / $height);
     }
     $thumb = imagecreatetruecolor($nwidth, $nheight);
     switch ($type) {
         case 1:
             $source = imagecreatefromgif($path);
             imagealphablending($thumb, false);
             imagesavealpha($thumb, true);
             break;
         case 2:
             $source = imagecreatefromjpeg($path);
             break;
         case 3:
             $source = imagecreatefrompng($path);
             imagealphablending($thumb, false);
             imagesavealpha($thumb, true);
             break;
         default:
             return '';
     }
     imagecopyresampled($thumb, $source, $destX, $destY, $srcX, $srcY, $nwidth, $nheight, $swidth, $sheight);
     switch ($type) {
         case 1:
             imagegif($thumb, $athumb);
             break;
         case 2:
             imagejpeg($thumb, $athumb, $config->get('jpg_quality'));
             break;
         case 3:
             imagepng($thumb, $athumb, $config->get('png_quality'), $config->get('"png_filter"'));
             break;
         default:
             return '';
     }
     imagedestroy($source);
     imagedestroy($thumb);
     $handle = @fopen($ini, 'a');
     if ($handle !== false) {
         $cache = $name . '="' . $rthumb . '"' . PHP_EOL;
         @fwrite($handle, $cache);
     }
     return $rthumb;
 }