vendor/pimcore/pimcore/models/Asset/Video/ImageThumbnail.php line 189

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\Video;
  15. use Pimcore\Event\AssetEvents;
  16. use Pimcore\Event\FrontendEvents;
  17. use Pimcore\File;
  18. use Pimcore\Logger;
  19. use Pimcore\Model;
  20. use Pimcore\Model\Asset\Image;
  21. use Pimcore\Tool\Storage;
  22. use Symfony\Component\EventDispatcher\GenericEvent;
  23. use Symfony\Component\Lock\LockFactory;
  24. final class ImageThumbnail
  25. {
  26.     use Model\Asset\Thumbnail\ImageThumbnailTrait;
  27.     /**
  28.      * @internal
  29.      *
  30.      * @var int
  31.      */
  32.     protected $timeOffset;
  33.     /**
  34.      * @internal
  35.      *
  36.      * @var Image|null
  37.      */
  38.     protected $imageAsset;
  39.     /**
  40.      * @param Model\Asset\Video $asset
  41.      * @param string|array|Image\Thumbnail\Config|null $config
  42.      * @param int|null $timeOffset
  43.      * @param Image|null $imageAsset
  44.      * @param bool $deferred
  45.      */
  46.     public function __construct($asset$config null$timeOffset null$imageAsset null$deferred true)
  47.     {
  48.         $this->asset $asset;
  49.         $this->timeOffset $timeOffset;
  50.         $this->imageAsset $imageAsset;
  51.         $this->config $this->createConfig($config);
  52.         $this->deferred $deferred;
  53.     }
  54.     /**
  55.      * @param bool $deferredAllowed
  56.      *
  57.      * @return string
  58.      */
  59.     public function getPath($deferredAllowed true)
  60.     {
  61.         $pathReference $this->getPathReference($deferredAllowed);
  62.         $path $this->convertToWebPath($pathReference);
  63.         $event = new GenericEvent($this, [
  64.             'pathReference' => $pathReference,
  65.             'frontendPath' => $path,
  66.         ]);
  67.         \Pimcore::getEventDispatcher()->dispatch($eventFrontendEvents::ASSET_VIDEO_IMAGE_THUMBNAIL);
  68.         $path $event->getArgument('frontendPath');
  69.         return $path;
  70.     }
  71.     /**
  72.      * @internal
  73.      *
  74.      * @param bool $deferredAllowed
  75.      *
  76.      * @throws \Exception
  77.      */
  78.     public function generate($deferredAllowed true)
  79.     {
  80.         $deferred $deferredAllowed && $this->deferred;
  81.         $generated false;
  82.         if ($this->asset && empty($this->pathReference)) {
  83.             $cs $this->asset->getCustomSetting('image_thumbnail_time');
  84.             $im $this->asset->getCustomSetting('image_thumbnail_asset');
  85.             if ($im || $this->imageAsset) {
  86.                 if ($this->imageAsset) {
  87.                     $im $this->imageAsset;
  88.                 } else {
  89.                     $im Model\Asset::getById($im);
  90.                 }
  91.                 if ($im instanceof Image) {
  92.                     $imageThumbnail $im->getThumbnail($this->getConfig());
  93.                     $this->pathReference $imageThumbnail->getPathReference();
  94.                 }
  95.             }
  96.             if (empty($this->pathReference)) {
  97.                 $timeOffset $this->timeOffset;
  98.                 if (!$this->timeOffset && $cs) {
  99.                     $timeOffset $cs;
  100.                 }
  101.                 // fallback
  102.                 if (!$timeOffset && $this->asset instanceof Model\Asset\Video) {
  103.                     $timeOffset ceil($this->asset->getDuration() / 3);
  104.                 }
  105.                 $storage Storage::get('asset_cache');
  106.                 $cacheFilePath sprintf('%s/image-thumb__%s__video_original_image/time_%s.png',
  107.                     rtrim($this->asset->getRealPath(), '/'),
  108.                     $this->asset->getId(),
  109.                     $timeOffset
  110.                 );
  111.                 if (!$storage->fileExists($cacheFilePath)) {
  112.                     $lock \Pimcore::getContainer()->get(LockFactory::class)->createLock($cacheFilePath);
  113.                     $lock->acquire(true);
  114.                     // after we got the lock, check again if the image exists in the meantime - if not - generate it
  115.                     if (!$storage->fileExists($cacheFilePath)) {
  116.                         $tempFile File::getLocalTempFilePath('png');
  117.                         $converter \Pimcore\Video::getInstance();
  118.                         $converter->load($this->asset->getLocalFile());
  119.                         $converter->saveImage($tempFile$timeOffset);
  120.                         $generated true;
  121.                         $storage->write($cacheFilePathfile_get_contents($tempFile));
  122.                         unlink($tempFile);
  123.                     }
  124.                     $lock->release();
  125.                 }
  126.                 $cacheFileStream $storage->readStream($cacheFilePath);
  127.                 if ($this->getConfig()) {
  128.                     $this->getConfig()->setFilenameSuffix('time-' $timeOffset);
  129.                     try {
  130.                         $this->pathReference Image\Thumbnail\Processor::process(
  131.                             $this->asset,
  132.                             $this->getConfig(),
  133.                             $cacheFileStream,
  134.                             $deferred,
  135.                             $generated
  136.                         );
  137.                     } catch (\Exception $e) {
  138.                         Logger::error("Couldn't create image-thumbnail of video " $this->asset->getRealFullPath());
  139.                         Logger::error($e);
  140.                     }
  141.                 }
  142.             }
  143.             if (empty($this->pathReference)) {
  144.                 $this->pathReference = [
  145.                     'type' => 'error',
  146.                     'src' => '/bundles/pimcoreadmin/img/filetype-not-supported.svg',
  147.                 ];
  148.             }
  149.             $event = new GenericEvent($this, [
  150.                 'deferred' => $deferred,
  151.                 'generated' => $generated,
  152.             ]);
  153.             \Pimcore::getEventDispatcher()->dispatch($eventAssetEvents::VIDEO_IMAGE_THUMBNAIL);
  154.         }
  155.     }
  156.     /**
  157.      * Get the public path to the thumbnail image.
  158.      * This method is here for backwards compatility.
  159.      * Up to Pimcore 1.4.8 a thumbnail was returned as a path to an image.
  160.      *
  161.      * @return string Public path to thumbnail image.
  162.      */
  163.     public function __toString()
  164.     {
  165.         return $this->getPath();
  166.     }
  167.     /**
  168.      * @param string|array|Image\Thumbnail\Config $selector
  169.      *
  170.      * @return Image\Thumbnail\Config|null
  171.      *
  172.      * @throws Model\Exception\NotFoundException
  173.      */
  174.     private function createConfig($selector)
  175.     {
  176.         $thumbnailConfig Image\Thumbnail\Config::getByAutoDetect($selector);
  177.         if (!empty($selector) && $thumbnailConfig === null) {
  178.             throw new Model\Exception\NotFoundException('Thumbnail definition "' . (is_string($selector) ? $selector '') . '" does not exist');
  179.         }
  180.         return $thumbnailConfig;
  181.     }
  182.     /**
  183.      * @param string $name
  184.      * @param int $highRes
  185.      *
  186.      * @return Image\Thumbnail|null
  187.      *
  188.      * @throws \Exception
  189.      */
  190.     public function getMedia($name$highRes 1)
  191.     {
  192.         $thumbConfig $this->getConfig();
  193.         if ($thumbConfig instanceof Image\Thumbnail\Config) {
  194.             $mediaConfigs $thumbConfig->getMedias();
  195.             if (isset($mediaConfigs[$name])) {
  196.                 $thumbConfigRes = clone $thumbConfig;
  197.                 $thumbConfigRes->selectMedia($name);
  198.                 $thumbConfigRes->setHighResolution($highRes);
  199.                 $thumbConfigRes->setMedias([]);
  200.                 $imgId $this->asset->getCustomSetting('image_thumbnail_asset');
  201.                 $img Model\Asset::getById($imgId);
  202.                 if ($img instanceof Image) {
  203.                     $thumb $img->getThumbnail($thumbConfigRes);
  204.                 }
  205.                 return $thumb ?? null;
  206.             } else {
  207.                 throw new \Exception("Media query '" $name "' doesn't exist in thumbnail configuration: " $thumbConfig->getName());
  208.             }
  209.         }
  210.         return null;
  211.     }
  212. }