vendor/pimcore/pimcore/models/DataObject/Data/Hotspotimage.php line 168

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\DataObject\Data;
  15. use Pimcore\Model\Asset;
  16. use Pimcore\Model\DataObject\OwnerAwareFieldInterface;
  17. use Pimcore\Model\DataObject\Traits\OwnerAwareFieldTrait;
  18. use Pimcore\Model\Element\ElementDescriptor;
  19. use Pimcore\Model\Element\Service;
  20. class Hotspotimage implements OwnerAwareFieldInterface
  21. {
  22.     use OwnerAwareFieldTrait;
  23.     /**
  24.      * @var Asset\Image|ElementDescriptor|null
  25.      */
  26.     protected $image;
  27.     /**
  28.      * @var array[]|null
  29.      */
  30.     protected $hotspots;
  31.     /**
  32.      * @var array[]|null
  33.      */
  34.     protected $marker;
  35.     /**
  36.      * @var array[]|null
  37.      */
  38.     protected $crop;
  39.     /**
  40.      * @param Asset\Image|int|null $image
  41.      * @param array $hotspots
  42.      * @param array $marker
  43.      * @param array $crop
  44.      */
  45.     public function __construct($image null$hotspots = [], $marker = [], $crop = [])
  46.     {
  47.         if ($image instanceof Asset\Image) {
  48.             $this->image $image;
  49.         } elseif (is_numeric($image)) {
  50.             $this->image Asset\Image::getById($image);
  51.         }
  52.         if (is_array($hotspots)) {
  53.             $this->hotspots = [];
  54.             foreach ($hotspots as $h) {
  55.                 $this->hotspots[] = $h;
  56.             }
  57.         }
  58.         if (is_array($marker)) {
  59.             $this->marker = [];
  60.             foreach ($marker as $m) {
  61.                 $this->marker[] = $m;
  62.             }
  63.         }
  64.         if (is_array($crop)) {
  65.             $this->crop $crop;
  66.         }
  67.         $this->markMeDirty();
  68.     }
  69.     /**
  70.      * @param array[]|null $hotspots
  71.      *
  72.      * @return $this
  73.      */
  74.     public function setHotspots($hotspots)
  75.     {
  76.         $this->hotspots $hotspots;
  77.         $this->markMeDirty();
  78.         return $this;
  79.     }
  80.     /**
  81.      * @return array[]|null
  82.      */
  83.     public function getHotspots()
  84.     {
  85.         return $this->hotspots;
  86.     }
  87.     /**
  88.      * @param array[]|null $marker
  89.      *
  90.      * @return $this
  91.      */
  92.     public function setMarker($marker)
  93.     {
  94.         $this->marker $marker;
  95.         $this->markMeDirty();
  96.         return $this;
  97.     }
  98.     /**
  99.      * @return array[]|null
  100.      */
  101.     public function getMarker()
  102.     {
  103.         return $this->marker;
  104.     }
  105.     /**
  106.      * @param array[]|null $crop
  107.      */
  108.     public function setCrop($crop)
  109.     {
  110.         $this->crop $crop;
  111.         $this->markMeDirty();
  112.     }
  113.     /**
  114.      * @return array[]|null
  115.      */
  116.     public function getCrop()
  117.     {
  118.         return $this->crop;
  119.     }
  120.     /**
  121.      * @param Asset\Image|null $image
  122.      *
  123.      * @return $this
  124.      */
  125.     public function setImage($image)
  126.     {
  127.         $this->image $image;
  128.         $this->markMeDirty();
  129.         return $this;
  130.     }
  131.     /**
  132.      * @return Asset\Image|null
  133.      */
  134.     public function getImage(): ?Asset\Image
  135.     {
  136.         return $this->image;
  137.     }
  138.     /**
  139.      * @param string|array|Asset\Image\Thumbnail\Config $thumbnailName
  140.      * @param bool $deferred
  141.      *
  142.      * @return Asset\Image\Thumbnail|string
  143.      */
  144.     public function getThumbnail($thumbnailName null$deferred true)
  145.     {
  146.         if (!$this->getImage()) {
  147.             return '';
  148.         }
  149.         $crop null;
  150.         if (is_array($this->getCrop())) {
  151.             $crop $this->getCrop();
  152.         }
  153.         $thumbConfig $this->getImage()->getThumbnailConfig($thumbnailName);
  154.         if (!$thumbConfig && $crop) {
  155.             $thumbConfig = new Asset\Image\Thumbnail\Config();
  156.         }
  157.         if ($crop) {
  158.             if ($thumbConfig->hasMedias()) {
  159.                 $medias $thumbConfig->getMedias() ?: [];
  160.                 foreach ($medias as $mediaName => $mediaConfig) {
  161.                     $thumbConfig->addItemAt(0'cropPercent', [
  162.                         'width' => $crop['cropWidth'],
  163.                         'height' => $crop['cropHeight'],
  164.                         'y' => $crop['cropTop'],
  165.                         'x' => $crop['cropLeft'],
  166.                     ], $mediaName);
  167.                 }
  168.             }
  169.             $thumbConfig->addItemAt(0'cropPercent', [
  170.                 'width' => $crop['cropWidth'],
  171.                 'height' => $crop['cropHeight'],
  172.                 'y' => $crop['cropTop'],
  173.                 'x' => $crop['cropLeft'],
  174.             ]);
  175.             $thumbConfig->generateAutoName();
  176.         }
  177.         return $this->getImage()->getThumbnail($thumbConfig$deferred);
  178.     }
  179.     /**
  180.      * @return string
  181.      */
  182.     public function __toString()
  183.     {
  184.         if ($this->image) {
  185.             return $this->image->__toString();
  186.         }
  187.         return '';
  188.     }
  189.     public function __wakeup()
  190.     {
  191.         if ($this->image instanceof ElementDescriptor) {
  192.             $image Service::getElementById($this->image->getType(), $this->image->getId());
  193.             if ($image instanceof Asset\Image) {
  194.                 $this->image $image;
  195.             } else {
  196.                 $this->image null;
  197.             }
  198.         }
  199.     }
  200. }