Esempio n. 1
0
 /**
  * Method to generate a single thumbnail.
  *
  * First it loads an image from the media path,
  * then resizes it and saves it to the default thumbnail directory
  *
  * @param Media $media
  * @param array $thumbnailSizes - array of all sizes which needs to be generated
  * @param bool $keepProportions - Whether or not keeping the proportions of the original image, the size can be affected when true
  * @throws \Exception
  * @return bool
  */
 public function createMediaThumbnail(Media $media, $thumbnailSizes = array(), $keepProportions = false)
 {
     if ($media->getType() !== $media::TYPE_IMAGE) {
         throw new \Exception("File is not an image.");
     }
     if (empty($thumbnailSizes)) {
         $thumbnailSizes = $this->getThumbnailSizesFromMedia($media);
         $thumbnailSizes = array_merge($thumbnailSizes, $media->getDefaultThumbnails());
     }
     $albumSettings = $this->getAlbumSettingsFromMedia($media);
     if ($albumSettings) {
         $highDpi = $albumSettings->isThumbnailHighDpi();
         $standardQuality = $albumSettings->getThumbnailQuality();
         $highDpiQuality = $albumSettings->getThumbnailHighDpiQuality();
     } else {
         $highDpi = false;
         $standardQuality = 90;
         $highDpiQuality = 90;
     }
     $thumbnailSizes = $this->uniformThumbnailSizes($thumbnailSizes);
     $imagePath = $media->getPath();
     $parameters = array('path' => $imagePath, 'sizes' => $thumbnailSizes, 'keepProportions' => $keepProportions);
     if ($this->eventManager) {
         $parameters = $this->eventManager->filter('Shopware_Components_Thumbnail_Manager_CreateThumbnail', $parameters, array('subject' => $this, 'media' => $media));
     }
     foreach ($parameters['sizes'] as $size) {
         $suffix = $size['width'] . 'x' . $size['height'];
         $destinations = $this->getDestination($media, $suffix);
         foreach ($destinations as $destination) {
             $this->generator->createThumbnail($parameters['path'], $destination, $size['width'], $size['height'], $parameters['keepProportions'], $standardQuality);
         }
     }
     if (!$highDpi) {
         return;
     }
     foreach ($parameters['sizes'] as $size) {
         $suffix = $size['width'] . 'x' . $size['height'] . '@2x';
         $destinations = $this->getDestination($media, $suffix);
         foreach ($destinations as $destination) {
             $this->generator->createThumbnail($parameters['path'], $destination, $size['width'] * 2, $size['height'] * 2, $parameters['keepProportions'], $highDpiQuality);
         }
     }
 }
Esempio n. 2
0
 /**
  * Method to generate a single thumbnail.
  * First it loads an image from the media path,
  * then resizes it and saves it to the default thumbnail directory
  *
  * @param Media $media
  * @param array $thumbnailSizes - array of all sizes which needs to be generated
  * @param bool $keepProportions - Whether or not keeping the proportions of the original image, the size can be affected when true
  * @throws \Exception
  * @return bool
  */
 public function createMediaThumbnail(Media $media, $thumbnailSizes = array(), $keepProportions = false)
 {
     if ($media->getType() !== $media::TYPE_IMAGE) {
         throw new \Exception("File is not an image.");
     }
     if (!is_writable($this->getThumbnailDir($media))) {
         throw new \Exception("Thumbnail directory is not writable");
     }
     if (empty($thumbnailSizes)) {
         $album = $media->getAlbum();
         if (!$album) {
             throw new \Exception("No album configured for the passed media object and no size passed!");
         }
         $settings = $album->getSettings();
         if (!$settings) {
             throw new \Exception("No settings configured in the album of the given media object!");
         }
         $settingSizes = $settings->getThumbnailSize();
         //when no sizes are defined in the album
         if (empty($settingSizes) || empty($settingSizes[0])) {
             $settingSizes = array();
         }
         $thumbnailSizes = array_merge($thumbnailSizes, $settingSizes);
     }
     $thumbnailSizes = array_merge($thumbnailSizes, $media->getDefaultThumbnails());
     $thumbnailSizes = $this->uniformThumbnailSizes($thumbnailSizes);
     $imagePath = $this->rootDir . DIRECTORY_SEPARATOR . $media->getPath();
     $parameters = array('path' => $imagePath, 'sizes' => $thumbnailSizes, 'keepProportions' => $keepProportions);
     if ($this->eventManager) {
         $parameters = $this->eventManager->filter('Shopware_Components_Thumbnail_Manager_CreateThumbnail', $parameters, array('subject' => $this, 'media' => $media));
     }
     foreach ($parameters['sizes'] as $size) {
         $suffix = $size['width'] . 'x' . $size['height'];
         $destinations = $this->getDestination($media, $suffix);
         foreach ($destinations as $destination) {
             $this->generator->createThumbnail($parameters['path'], $destination, $size['width'], $size['height'], $parameters['keepProportions']);
         }
     }
 }