vendor/pimcore/pimcore/models/Asset/Document/ImageThumbnail.php line 75

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Model\Asset\Document;
  15. use Pimcore\Event\AssetEvents;
  16. use Pimcore\Event\FrontendEvents;
  17. use Pimcore\File;
  18. use Pimcore\Helper\TemporaryFileHelperTrait;
  19. use Pimcore\Logger;
  20. use Pimcore\Model;
  21. use Pimcore\Model\Asset\Image;
  22. use Pimcore\Model\Exception\NotFoundException;
  23. use Pimcore\Tool\Storage;
  24. use Symfony\Component\EventDispatcher\GenericEvent;
  25. use Symfony\Component\Lock\LockFactory;
  26. /**
  27.  * @property Model\Asset\Document|null $asset
  28.  */
  29. final class ImageThumbnail
  30. {
  31.     use Model\Asset\Thumbnail\ImageThumbnailTrait;
  32.     use TemporaryFileHelperTrait;
  33.     /**
  34.      * @internal
  35.      *
  36.      * @var int
  37.      */
  38.     protected $page 1;
  39.     /**
  40.      * @param Model\Asset\Document|null $asset
  41.      * @param string|array|Image\Thumbnail\Config $config
  42.      * @param int $page
  43.      * @param bool $deferred
  44.      */
  45.     public function __construct($asset$config null$page 1$deferred true)
  46.     {
  47.         $this->asset $asset;
  48.         $this->config $this->createConfig($config);
  49.         $this->page $page;
  50.         $this->deferred $deferred;
  51.     }
  52.     /**
  53.      * TODO: Pimcore 11: Change method signature to getPath($args = [])
  54.      *
  55.      * @param mixed $args,...
  56.      *
  57.      * @return string
  58.      */
  59.     public function getPath(...$args)
  60.     {
  61.         // TODO: Pimcore 11: remove calling the covertArgsBcLayer() method
  62.         $args $this->convertArgsBcLayer($args);
  63.         // set defaults
  64.         $deferredAllowed $args['deferredAllowed'] ?? true;
  65.         $frontend $args['frontend'] ?? \Pimcore\Tool::isFrontend();
  66.         $pathReference $this->getPathReference($deferredAllowed);
  67.         $path $this->convertToWebPath($pathReference$frontend);
  68.         $event = new GenericEvent($this, [
  69.             'pathReference' => $pathReference,
  70.             'frontendPath' => $path,
  71.         ]);
  72.         \Pimcore::getEventDispatcher()->dispatch($eventFrontendEvents::ASSET_DOCUMENT_IMAGE_THUMBNAIL);
  73.         $path $event->getArgument('frontendPath');
  74.         return $path;
  75.     }
  76.     /**
  77.      * @param bool $deferredAllowed
  78.      */
  79.     public function generate($deferredAllowed true)
  80.     {
  81.         $deferred $deferredAllowed && $this->deferred;
  82.         $generated false;
  83.         if ($this->asset && empty($this->pathReference)) {
  84.             $config $this->getConfig();
  85.             $cacheFileStream null;
  86.             $config->setFilenameSuffix('page-' $this->page);
  87.             try {
  88.                 if (!$deferred) {
  89.                     if ($cacheFileStream $this->getCacheFileStream()) {
  90.                         $generated true;
  91.                     }
  92.                 }
  93.                 if ($config) {
  94.                     if ($deferred || $cacheFileStream) {
  95.                         $this->pathReference Image\Thumbnail\Processor::process($this->asset$config$cacheFileStream$deferred$generated);
  96.                     }
  97.                 }
  98.             } catch (\Exception $e) {
  99.                 Logger::error("Couldn't create image-thumbnail of document " $this->asset->getRealFullPath() . ': ' $e);
  100.             }
  101.         }
  102.         if (empty($this->pathReference)) {
  103.             $this->pathReference = [
  104.                 'type' => 'error',
  105.                 'src' => '/bundles/pimcoreadmin/img/filetype-not-supported.svg',
  106.             ];
  107.         }
  108.         $event = new GenericEvent($this, [
  109.             'deferred' => $deferred,
  110.             'generated' => $generated,
  111.         ]);
  112.         \Pimcore::getEventDispatcher()->dispatch($eventAssetEvents::DOCUMENT_IMAGE_THUMBNAIL);
  113.     }
  114.     /**
  115.      * @return resource|null
  116.      */
  117.     private function getCacheFileStream()
  118.     {
  119.         $storage Storage::get('asset_cache');
  120.         $cacheFilePath sprintf(
  121.             '%s/%s/image-thumb__%s__document_original_image/page_%s.png',
  122.             rtrim($this->asset->getRealPath(), '/'),
  123.             $this->asset->getId(),
  124.             $this->asset->getId(),
  125.             $this->page
  126.         );
  127.         if (!$storage->fileExists($cacheFilePath)) {
  128.             $lock \Pimcore::getContainer()->get(LockFactory::class)->createLock($cacheFilePath);
  129.             if ($lock->acquire()) {
  130.                 $tempFile File::getLocalTempFilePath('png');
  131.                 try {
  132.                     $converter \Pimcore\Document::getInstance();
  133.                     $converter->load($this->asset);
  134.                     $converter->saveImage($tempFile$this->page);
  135.                     $storage->write($cacheFilePathfile_get_contents($tempFile));
  136.                 } finally {
  137.                     $lock->release();
  138.                 }
  139.             } else {
  140.                 Logger::info('Creation of cache file stream of document ' $this->asset->getRealFullPath() . ' is locked');
  141.                 return null;
  142.             }
  143.         }
  144.         return $storage->readStream($cacheFilePath);
  145.     }
  146.     /**
  147.      * Get the public path to the thumbnail image.
  148.      * This method is here for backwards compatility.
  149.      * Up to Pimcore 1.4.8 a thumbnail was returned as a path to an image.
  150.      *
  151.      * @return string Public path to thumbnail image.
  152.      */
  153.     public function __toString()
  154.     {
  155.         return $this->getPath();
  156.     }
  157.     /**
  158.      * @param string|array|Image\Thumbnail\Config $selector
  159.      *
  160.      * @return Image\Thumbnail\Config
  161.      */
  162.     protected function createConfig($selector)
  163.     {
  164.         $config Image\Thumbnail\Config::getByAutoDetect($selector);
  165.         if (!empty($selector) && $config === null) {
  166.             throw new NotFoundException('Thumbnail definition "' . (is_string($selector) ? $selector '') . '" does not exist');
  167.         }
  168.         if ($config) {
  169.             $format strtolower($config->getFormat());
  170.             if ($format == 'source') {
  171.                 $config->setFormat('PNG');
  172.             }
  173.         }
  174.         return $config;
  175.     }
  176. }