예제 #1
0
 public static function createThumb($path, $width = 100, $height = 100, $crop = 2, $cachefolder = 'hgimages', $external = 0)
 {
     $myImage = new JImage();
     if (!$external) {
         $myImage->loadFile(JPATH_SITE . DS . $path);
     } else {
         $myImage->loadFile($path);
     }
     if ($myImage->isLoaded()) {
         // $filename = end(explode('/', $path));
         $filename = JFile::getName($path);
         $filefolder = substr(md5(self::getFolderPath($path)), 1, 10);
         $newfilename = $width . 'x' . $height . 'x' . $crop . '_' . $filefolder . '_' . JFile::makeSafe($filename);
         $hgimages = JPATH_CACHE . '/' . $cachefolder . '/';
         if (!JFolder::exists($hgimages)) {
             JFolder::create($hgimages);
         }
         $fileExists = JFile::exists($hgimages . $newfilename);
         if (!$fileExists) {
             switch ($crop) {
                 // Case for self::CROP
                 case 4:
                     $resizedImage = $myImage->crop($width, $height, null, null, true);
                     break;
                     // Case for self::CROP_RESIZE
                 // Case for self::CROP_RESIZE
                 case 5:
                     $resizedImage = $myImage->cropResize($width, $height, true);
                     break;
                 default:
                     $resizedImage = $myImage->resize($width, $height, true, $crop);
                     break;
             }
             $properties = $myImage->getImageFileProperties($path);
             $mime = $properties->mime;
             if ($mime == 'image/jpeg') {
                 $type = IMAGETYPE_JPEG;
             } elseif ($mime = 'image/png') {
                 $type = IMAGETYPE_PNG;
             } elseif ($mime = 'image/gif') {
                 $type = IMAGETYPE_GIF;
             }
             $resizedImage->toFile($hgimages . $newfilename, $type);
         }
         return $newfilename;
     } else {
         return "My file is not loaded";
     }
 }
예제 #2
0
 /**
  * Get the thumbnail path.
  *
  * @param  string  $image    Path to source image
  * @param  string  $size     The thumbnail size. Ex: '500x400'
  * @param  string  $folder   The thumbnail folder
  * @param  string  $filename The thumbnail file name
  * @param  integer quality   The thumbnail quality
  *
  * @return string            The thumbnail path
  *
  * @throws ArgumentException
  * @throws InvalidArgumentException
  */
 private static function _getThumbnail($image, $size = '550x460', $folder = null, $filename = null, $quality = 90)
 {
     jimport('joomla.filesystem.file');
     require_once __DIR__ . '/libraries/phpthumb/ThumbLib.inc.php';
     if (empty($image)) {
         throw new ArgumentException(JText::_("MOD_SLIDESHOW_ERROR_ARGUMENT_IMAGE_EMPTY"));
     }
     if (empty($folder)) {
         $folder = 'cache/mod_slideshow';
     }
     $folder = JPATH_SITE . '/' . $folder;
     // Check or try to create folder
     if (JFolder::exists($folder) || JFolder::create($folder)) {
         // Desired thumbnail size
         $size = explode('x', strtolower($size));
         if (count($size) != 2) {
             throw new InvalidArgumentException(JText::sprintf('MOD_SLIDESHOW_ERROR_ARGUMENT_SIZE_INVALID', $size));
         }
         // Generate thumb name
         $filename = empty($filename) ? pathinfo($image, PATHINFO_FILENAME) : $filename;
         $extension = pathinfo($image, PATHINFO_EXTENSION);
         $filename = $filename . '.' . $extension;
         $file = $folder . '/' . $filename;
         $image = new JImage($image);
         try {
             $thumbnail = $image->cropResize($size[0], $size[1], false);
             $thumbnail->toFile($folder . '/' . $filename);
         } catch (Exception $e) {
             JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
         }
         /*$thumbnail = PhpThumbFactory::create($image);
         		$thumbnail->setoptions(array('jpegQuality' => $quality,'resizeUp' => true));
         		$thumbnail->adaptiveResize($size[0], $size[1]);
         		$thumbnail->save($file);*/
     }
     return str_replace(JPATH_SITE . '/', JUri::root(), $file);
 }