コード例 #1
0
 /**
  * Generate thumbnail and redirect to resource URI
  *
  * @param Thumbnail $thumbnail
  * @return void
  */
 public function thumbnailAction(Thumbnail $thumbnail)
 {
     if ($thumbnail->getResource() === null && $thumbnail->getStaticResource() === null) {
         $this->thumbnailService->refreshThumbnail($thumbnail);
     }
     $this->redirectToUri($this->thumbnailService->getUriForThumbnail($thumbnail), 0, 302);
 }
 /**
  * @param Thumbnail $thumbnail
  * @return void
  * @throws Exception\NoThumbnailAvailableException
  */
 public function refresh(Thumbnail $thumbnail)
 {
     try {
         $width = $thumbnail->getConfigurationValue('width') ?: $thumbnail->getConfigurationValue('maximumWidth');
         $height = $thumbnail->getConfigurationValue('height') ?: $thumbnail->getConfigurationValue('maximumHeight');
         /** @var AssetInterface $asset */
         $asset = $thumbnail->getOriginalAsset();
         $icon = FileTypeIconService::getIcon($asset, $width, $height);
         $thumbnail->setStaticResource($icon['src']);
         $thumbnail->setWidth($icon['width']);
         $thumbnail->setHeight($icon['height']);
     } catch (\Exception $exception) {
         $message = sprintf('Unable to generate thumbnail for the given image (filename: %s, SHA1: %s)', $thumbnail->getOriginalAsset()->getResource()->getFilename(), $thumbnail->getOriginalAsset()->getResource()->getSha1());
         throw new Exception\NoThumbnailAvailableException($message, 1433109654, $exception);
     }
 }
 /**
  * @param Thumbnail $thumbnail
  * @return void
  * @throws Exception\NoThumbnailAvailableException
  */
 public function refresh(Thumbnail $thumbnail)
 {
     try {
         $filenameWithoutExtension = pathinfo($thumbnail->getOriginalAsset()->getResource()->getFilename(), PATHINFO_FILENAME);
         $temporaryLocalCopyFilename = $thumbnail->getOriginalAsset()->getResource()->createTemporaryLocalCopy();
         $documentFile = sprintf(in_array($thumbnail->getOriginalAsset()->getResource()->getFileExtension(), $this->getOption('paginableDocuments')) ? '%s[0]' : '%s', $temporaryLocalCopyFilename);
         $width = $thumbnail->getConfigurationValue('width') ?: $thumbnail->getConfigurationValue('maximumWidth');
         $height = $thumbnail->getConfigurationValue('height') ?: $thumbnail->getConfigurationValue('maximumHeight');
         $im = new \Imagick();
         $im->setResolution($this->getOption('resolution'), $this->getOption('resolution'));
         $im->readImage($documentFile);
         $im->setImageFormat('png');
         $im->setImageBackgroundColor('white');
         $im->setImageCompose(\Imagick::COMPOSITE_OVER);
         if (defined('\\Imagick::ALPHACHANNEL_OFF')) {
             // ImageMagick >= 7.0, Imagick >= 3.4.3RC1
             // @see https://pecl.php.net/package/imagick/3.4.3RC1
             $im->setImageAlphaChannel(\Imagick::ALPHACHANNEL_OFF);
         } else {
             $im->setImageAlphaChannel(\Imagick::ALPHACHANNEL_RESET);
         }
         $im->thumbnailImage($width, $height, true);
         if (method_exists($im, 'mergeImageLayers')) {
             // Replace flattenImages in imagick 3.3.0
             // @see https://pecl.php.net/package/imagick/3.3.0RC2
             $im->mergeImageLayers(\Imagick::LAYERMETHOD_MERGE);
         } else {
             $im->flattenImages();
         }
         $resource = $this->resourceManager->importResourceFromContent($im->getImageBlob(), $filenameWithoutExtension . '.png');
         $im->destroy();
         $thumbnail->setResource($resource);
         $thumbnail->setWidth($width);
         $thumbnail->setHeight($height);
     } catch (\Exception $exception) {
         $filename = $thumbnail->getOriginalAsset()->getResource()->getFilename();
         $sha1 = $thumbnail->getOriginalAsset()->getResource()->getSha1();
         $message = sprintf('Unable to generate thumbnail for the given document (filename: %s, SHA1: %s)', $filename, $sha1);
         throw new Exception\NoThumbnailAvailableException($message, 1433109652, $exception);
     }
 }
 /**
  * @param Thumbnail $thumbnail
  * @param Resource $resource
  * @return array
  * @throws Exception\ImageFileException
  */
 protected function resize(Thumbnail $thumbnail, Resource $resource)
 {
     $adjustments = array(new ResizeImageAdjustment(array('width' => $thumbnail->getConfigurationValue('width'), 'maximumWidth' => $thumbnail->getConfigurationValue('maximumWidth'), 'height' => $thumbnail->getConfigurationValue('height'), 'maximumHeight' => $thumbnail->getConfigurationValue('maximumHeight'), 'ratioMode' => $thumbnail->getConfigurationValue('ratioMode'), 'allowUpScaling' => $thumbnail->getConfigurationValue('allowUpScaling'))));
     return $this->imageService->processImage($resource, $adjustments);
 }
コード例 #5
0
 /**
  * @param Thumbnail $thumbnail
  * @return void
  * @throws Exception\NoThumbnailAvailableException
  */
 public function refresh(Thumbnail $thumbnail)
 {
     try {
         $width = $thumbnail->getConfigurationValue('width') ?: $thumbnail->getConfigurationValue('maximumWidth');
         $height = $thumbnail->getConfigurationValue('height') ?: $thumbnail->getConfigurationValue('maximumHeight');
         /** @var AssetInterface $asset */
         $asset = $thumbnail->getOriginalAsset();
         $thumbnail->setStaticResource($this->resourceManager->getPublicPersistentResourceUri($asset->getResource()));
         $thumbnail->setWidth($width);
         $thumbnail->setHeight($height);
     } catch (\Exception $exception) {
         $filename = $thumbnail->getOriginalAsset()->getResource()->getFilename();
         $sha1 = $thumbnail->getOriginalAsset()->getResource()->getSha1();
         $message = sprintf('Unable to generate thumbnail for the given SVG (filename: %s, SHA1: %s)', $filename, $sha1);
         throw new Exception\NoThumbnailAvailableException($message, 1433109654, $exception);
     }
 }
コード例 #6
0
 /**
  * Refreshes a thumbnail and persists the thumbnail
  *
  * @param Thumbnail $thumbnail
  * @return void
  */
 public function refreshThumbnail(Thumbnail $thumbnail)
 {
     $thumbnail->refresh();
     $this->persistenceManager->whiteListObject($thumbnail);
     if (!$this->persistenceManager->isNewObject($thumbnail)) {
         $this->thumbnailRepository->update($thumbnail);
     }
 }
 /**
  * @param Thumbnail $thumbnail
  * @return void
  * @throws Exception\NoThumbnailAvailableException
  */
 public function refresh(Thumbnail $thumbnail)
 {
     try {
         $adjustments = array(new ResizeImageAdjustment(array('width' => $thumbnail->getConfigurationValue('width'), 'maximumWidth' => $thumbnail->getConfigurationValue('maximumWidth'), 'height' => $thumbnail->getConfigurationValue('height'), 'maximumHeight' => $thumbnail->getConfigurationValue('maximumHeight'), 'ratioMode' => $thumbnail->getConfigurationValue('ratioMode'), 'allowUpScaling' => $thumbnail->getConfigurationValue('allowUpScaling'))));
         $processedImageInfo = $this->imageService->processImage($thumbnail->getOriginalAsset()->getResource(), $adjustments);
         $thumbnail->setResource($processedImageInfo['resource']);
         $thumbnail->setWidth($processedImageInfo['width']);
         $thumbnail->setHeight($processedImageInfo['height']);
     } catch (\Exception $exception) {
         $message = sprintf('Unable to generate thumbnail for the given image (filename: %s, SHA1: %s)', $thumbnail->getOriginalAsset()->getResource()->getFilename(), $thumbnail->getOriginalAsset()->getResource()->getSha1());
         throw new Exception\NoThumbnailAvailableException($message, 1433109654, $exception);
     }
 }
 /**
  * @param Thumbnail $thumbnail
  * @return boolean
  */
 protected function isExtensionSupported(Thumbnail $thumbnail)
 {
     $extension = $thumbnail->getOriginalAsset()->getResource()->getFileExtension();
     return in_array($extension, $this->getOption('supportedExtensions'));
 }
コード例 #9
0
 /**
  * @param Thumbnail $thumbnail
  * @return string
  * @throws ThumbnailServiceException
  */
 public function getUriForThumbnail(Thumbnail $thumbnail)
 {
     $resource = $thumbnail->getResource();
     if ($resource) {
         return $this->resourceManager->getPublicPersistentResourceUri($resource);
     }
     $staticResource = $thumbnail->getStaticResource();
     if ($staticResource === null) {
         throw new ThumbnailServiceException(sprintf('Could not generate URI for static thumbnail "%s".', $this->persistenceManager->getIdentifierByObject($thumbnail)), 1450178437);
     }
     try {
         list($package, $path) = $this->resourceManager->getPackageAndPathByPublicPath($staticResource);
         return $this->resourceManager->getPublicPackageResourceUri($package, $path);
     } catch (Exception $exception) {
         return $staticResource;
     }
 }