src/Helper/AssetHelper.php line 155

Open in your IDE?
  1. <?php
  2. /**
  3.  * 
  4.  * Get link with domain of asset
  5.  * Create image, gallery from file(s) or url(s)
  6.  *
  7.  */
  8. namespace App\Helper;
  9. use Pimcore\Tool;
  10. use Pimcore\Config;
  11. use Pimcore\Model\DataObject\ClassDefinition;
  12. use Pimcore\Model\DataObject\ClassDefinition\Data;
  13. use Pimcore\Model\DataObject\Data\ImageGallery;
  14. use Pimcore\Model\DataObject\Data\Hotspotimage;
  15. use Pimcore\Model\Asset;
  16. use Pimcore\Model\Asset\Image\Thumbnail;
  17. use App\Helper\LogHelper;
  18. class AssetHelper
  19. {
  20.     CONST LOG_FILE_NAME 'helper_asset';
  21.     CONST THUMBNAIL_PREFIX 'thumb';
  22.     public static function getThumbnails($asset)
  23.     {
  24.         $thumbnails = [];
  25.         $thumbnailList Config::getWebsiteConfig()->get('image_thumbnail_list');
  26.         if ($thumbnailList) {
  27.             $thumbnailList explode(','$thumbnailList);
  28.             foreach ($thumbnailList as $thumbnailName) {
  29.                 $thumbnails[self::THUMBNAIL_PREFIX $thumbnailName] = self::getThumbnailLink($asset$thumbnailName);
  30.             }
  31.         }
  32.         return $thumbnails;
  33.     }
  34.     public static function getGalleryThumbnails($gallery)
  35.     {
  36.         $images = [];
  37.         if ($gallery instanceof ImageGallery) {
  38.             foreach ($gallery as $item) {
  39.                 if ($item) {
  40.                     $hotpot $item->getImage();
  41.                     if ($hotpot) {
  42.                         $asset $hotpot;
  43.                         $thumbnails = [];
  44.                         $thumbnailList Config::getWebsiteConfig()->get('image_thumbnail_list');
  45.                         if ($thumbnailList) {
  46.                             $thumbnailList explode(','$thumbnailList);
  47.                             foreach ($thumbnailList as $thumbnailName) {
  48.                                 $thumbnails[self::THUMBNAIL_PREFIX $thumbnailName] = self::getThumbnailLink($asset$thumbnailName);
  49.                             }
  50.                         }
  51.                         $images[] = $thumbnails;
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.         return $images;
  57.     }
  58.     public static function getObjectLink($link)
  59.     {
  60.         if ($link) {
  61.             if ($link->getDirect()) {
  62.                 return $link->getDirect();
  63.             }
  64.             $link $link->getPath();
  65.             if (substr($link01) == "/") {
  66.                 return \Pimcore\Tool::getHostUrl() . $link;
  67.             }
  68.             return $link;
  69.         }
  70.         return null;
  71.     }
  72.     public static function getVideo($video)
  73.     {
  74.         if ($video) {
  75.             $videoType $video->getType();
  76.             $data = [
  77.                 'type' => $videoType,
  78.                 'data' => '',
  79.                 'link' => '',
  80.                 'image' => ''
  81.             ];
  82.             if (in_array($videoType, ['youtube''vimeo''dailymotion'])) {
  83.                $data['data'] = $video->getData();
  84.             }
  85.             if (in_array($videoType, ['youtube'])) {
  86.                $data['data'] = $video->getData();
  87.                $data['link'] = "https://www.youtube.com/watch?v="$data['data'];
  88.                $data['image'] = "https://img.youtube.com/vi/"$data['data'] ."/0.jpg";
  89.             }
  90.             if ($videoType == 'asset') {
  91.                 if ($video->getData()) {
  92.                     $link $video->getData()->getFullPath();
  93.                     if (!(substr($link04) == "http")) {
  94.                         $link \Pimcore\Tool::getHostUrl() . $link;
  95.                     }
  96.                     $data['data'] = $link;
  97.                     $data['link'] = $link;
  98.                 }
  99.             }
  100.             return $data;
  101.         }
  102.         return null;
  103.     }
  104.     public static function getGallery($gallery)
  105.     {
  106.         $images = [];
  107.         if ($gallery instanceof ImageGallery) {
  108.             foreach ($gallery as $item) {
  109.                 if ($item) {
  110.                     $hotpot $item->getImage();
  111.                     if ($hotpot) {
  112.                         $images[] = self::getLink($hotpot);
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.         return $images;
  118.     }
  119.     public static function getLink($asset$skipThumbnail false)
  120.     {
  121.         if ($asset instanceof Asset) {
  122.             $defautThumbnailName Config::getWebsiteConfig()->get('image_thumbnail');
  123.             if ($asset instanceof Asset\Image && $defautThumbnailName && !$skipThumbnail) {
  124.                 return self::getThumbnailLink($asset$defautThumbnailName);
  125.             }
  126.             $frontendPath $asset->getFrontendFullPath();
  127.             $url preg_match('/^http(s)?:\\/\\/.+/'$frontendPath) ?
  128.                 $frontendPath :
  129.                 \Symfony\Component\HttpFoundation\Request::createFromGlobals()->getSchemeAndHttpHost() . $frontendPath;
  130.             return $url;
  131.         }
  132.         return null;
  133.     }
  134.     public static function getThumbnailLink($asset$thumbnailName)
  135.     {
  136.         if ($asset instanceof Asset) {
  137.             $isGif $asset->getMimetype() == 'image/gif';
  138.             if (!$isGif) {
  139.                 $thumbnail Thumbnail\Config::getByName($thumbnailName);
  140.                 if ($thumbnail) {
  141.                     $thumbnailAsset $asset->getThumbnail($thumbnail);
  142.                     if (!$thumbnailAsset->exists()) {
  143.                         $deferred false;
  144.                         $thumbnailAsset $asset->getThumbnail($thumbnail$deferred);
  145.                     }
  146.                     $thumbnailAsset $thumbnailAsset->getPath();
  147.                     if (substr($thumbnailAsset01) == "/") {
  148.                         $prefix \Pimcore::getContainer()->getParameter('pimcore.config')['assets']['frontend_prefixes']['thumbnail'];
  149.                         $thumbnailAsset $prefix $thumbnailAsset;
  150.                     }
  151.                     return $thumbnailAsset;
  152.                 }
  153.             }
  154.             return self::getLink($assettrue);
  155.         }
  156.         return null;
  157.     }
  158.     public static function createImageFromFile($file$name$folderPath)
  159.     {
  160.         try {
  161.             $name self::formatName($name$file->guessExtension());
  162.             $url $file->getRealPath();
  163.             return self::createImage($url$name$folderPath);
  164.         } catch (\Throwable $e) {
  165.             LogHelper::logError(self::LOG_FILE_NAME, (string) ($e ."\n \n"));
  166.         }
  167.         return null;
  168.     }
  169.     public static function createImageFromUrl(string $url$name$folderPath)
  170.     {
  171.         try {
  172.             if (@exif_imagetype($url)) {
  173.                 $url str_replace(' '''$url);
  174.                 $extension pathinfo(parse_url($urlPHP_URL_PATH), PATHINFO_EXTENSION);
  175.                 if ($extension) {
  176.                     $name self::formatName($name$extension);
  177.                     return self::createImage($url$name$folderPath);
  178.                 }
  179.             }
  180.         } catch (\Throwable $e) {
  181.             LogHelper::logError(self::LOG_FILE_NAME, (string) ($e ."\n \n"));
  182.         }
  183.         return null;
  184.     }
  185.     public static function createGalleryFromFiles(array $files$name$folderPath)
  186.     {
  187.         $gallery = [];
  188.         foreach ($files as $key => $file) {
  189.             $image self::createImageFromFile($file$name .'-'$key$folderPath);
  190.             if ($image) {
  191.                 $hotspot = new Hotspotimage();
  192.                 $hotspot->setImage($image);
  193.                 $gallery[] = $hotspot;
  194.             }
  195.         }
  196.         return new ImageGallery($gallery);
  197.     }
  198.     public static function createGalleryFromUrls(array $urls$name$folderPath)
  199.     {
  200.         $gallery = [];
  201.         foreach ($urls as $key => $url) {
  202.             $image self::createImageFromUrl($url$name .'-'$key$folderPath);
  203.             if ($image) {
  204.                 $hotspot = new Hotspotimage();
  205.                 $hotspot->setImage($image);
  206.                 $gallery[] = $hotspot;
  207.             }
  208.         }
  209.         return new ImageGallery($gallery);
  210.     }
  211.     private static function createImage($dataPath$name$folderPath)
  212.     {
  213.         try {
  214.             $folder Asset::getByPath($folderPath) ?? Asset\Service::createFolderByPath($folderPath);
  215.             $image = new Asset\Image();
  216.             $image->setFileName($name);
  217.             $image->setData(@file_get_contents($dataPath));
  218.             $image->setParent($folder);
  219.             if ($image->save()) {
  220.                 return $image;
  221.             }
  222.         } catch (\Throwable $e) {
  223.             LogHelper::logError(self::LOG_FILE_NAME, (string) ($e ."\n \n"));
  224.         }
  225.         return null;
  226.     }
  227.     private static function formatName($name$extension null)
  228.     {
  229.         $name str_replace([' ''/'], ['_''+'], ltrim($name)) .'('time() .')';
  230.         if ($extension) {
  231.             $name .= '.'$extension;
  232.         }
  233.         return $name;
  234.     }
  235. }