<?php
/**
*
* Get link with domain of asset
* Create image, gallery from file(s) or url(s)
*
*/
namespace App\Helper;
use Pimcore\Tool;
use Pimcore\Config;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\Data\ImageGallery;
use Pimcore\Model\DataObject\Data\Hotspotimage;
use Pimcore\Model\Asset;
use Pimcore\Model\Asset\Image\Thumbnail;
use App\Helper\LogHelper;
class AssetHelper
{
CONST LOG_FILE_NAME = 'helper_asset';
CONST THUMBNAIL_PREFIX = 'thumb';
public static function getThumbnails($asset)
{
$thumbnails = [];
$thumbnailList = Config::getWebsiteConfig()->get('image_thumbnail_list');
if ($thumbnailList) {
$thumbnailList = explode(',', $thumbnailList);
foreach ($thumbnailList as $thumbnailName) {
$thumbnails[self::THUMBNAIL_PREFIX . $thumbnailName] = self::getThumbnailLink($asset, $thumbnailName);
}
}
return $thumbnails;
}
public static function getGalleryThumbnails($gallery)
{
$images = [];
if ($gallery instanceof ImageGallery) {
foreach ($gallery as $item) {
if ($item) {
$hotpot = $item->getImage();
if ($hotpot) {
$asset = $hotpot;
$thumbnails = [];
$thumbnailList = Config::getWebsiteConfig()->get('image_thumbnail_list');
if ($thumbnailList) {
$thumbnailList = explode(',', $thumbnailList);
foreach ($thumbnailList as $thumbnailName) {
$thumbnails[self::THUMBNAIL_PREFIX . $thumbnailName] = self::getThumbnailLink($asset, $thumbnailName);
}
}
$images[] = $thumbnails;
}
}
}
}
return $images;
}
public static function getObjectLink($link)
{
if ($link) {
if ($link->getDirect()) {
return $link->getDirect();
}
$link = $link->getPath();
if (substr($link, 0, 1) == "/") {
return \Pimcore\Tool::getHostUrl() . $link;
}
return $link;
}
return null;
}
public static function getVideo($video)
{
if ($video) {
$videoType = $video->getType();
$data = [
'type' => $videoType,
'data' => '',
'link' => '',
'image' => ''
];
if (in_array($videoType, ['youtube', 'vimeo', 'dailymotion'])) {
$data['data'] = $video->getData();
}
if (in_array($videoType, ['youtube'])) {
$data['data'] = $video->getData();
$data['link'] = "https://www.youtube.com/watch?v=". $data['data'];
$data['image'] = "https://img.youtube.com/vi/". $data['data'] ."/0.jpg";
}
if ($videoType == 'asset') {
if ($video->getData()) {
$link = $video->getData()->getFullPath();
if (!(substr($link, 0, 4) == "http")) {
$link = \Pimcore\Tool::getHostUrl() . $link;
}
$data['data'] = $link;
$data['link'] = $link;
}
}
return $data;
}
return null;
}
public static function getGallery($gallery)
{
$images = [];
if ($gallery instanceof ImageGallery) {
foreach ($gallery as $item) {
if ($item) {
$hotpot = $item->getImage();
if ($hotpot) {
$images[] = self::getLink($hotpot);
}
}
}
}
return $images;
}
public static function getLink($asset, $skipThumbnail = false)
{
if ($asset instanceof Asset) {
$defautThumbnailName = Config::getWebsiteConfig()->get('image_thumbnail');
if ($asset instanceof Asset\Image && $defautThumbnailName && !$skipThumbnail) {
return self::getThumbnailLink($asset, $defautThumbnailName);
}
$frontendPath = $asset->getFrontendFullPath();
$url = preg_match('/^http(s)?:\\/\\/.+/', $frontendPath) ?
$frontendPath :
\Symfony\Component\HttpFoundation\Request::createFromGlobals()->getSchemeAndHttpHost() . $frontendPath;
return $url;
}
return null;
}
public static function getThumbnailLink($asset, $thumbnailName)
{
if ($asset instanceof Asset) {
$isGif = $asset->getMimetype() == 'image/gif';
if (!$isGif) {
$thumbnail = Thumbnail\Config::getByName($thumbnailName);
if ($thumbnail) {
$thumbnailAsset = $asset->getThumbnail($thumbnail);
if (!$thumbnailAsset->exists()) {
$deferred = false;
$thumbnailAsset = $asset->getThumbnail($thumbnail, $deferred);
}
$thumbnailAsset = $thumbnailAsset->getPath();
if (substr($thumbnailAsset, 0, 1) == "/") {
$prefix = \Pimcore::getContainer()->getParameter('pimcore.config')['assets']['frontend_prefixes']['thumbnail'];
$thumbnailAsset = $prefix . $thumbnailAsset;
}
return $thumbnailAsset;
}
}
return self::getLink($asset, true);
}
return null;
}
public static function createImageFromFile($file, $name, $folderPath)
{
try {
$name = self::formatName($name, $file->guessExtension());
$url = $file->getRealPath();
return self::createImage($url, $name, $folderPath);
} catch (\Throwable $e) {
LogHelper::logError(self::LOG_FILE_NAME, (string) ($e ."\n \n"));
}
return null;
}
public static function createImageFromUrl(string $url, $name, $folderPath)
{
try {
if (@exif_imagetype($url)) {
$url = str_replace(' ', '', $url);
$extension = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION);
if ($extension) {
$name = self::formatName($name, $extension);
return self::createImage($url, $name, $folderPath);
}
}
} catch (\Throwable $e) {
LogHelper::logError(self::LOG_FILE_NAME, (string) ($e ."\n \n"));
}
return null;
}
public static function createGalleryFromFiles(array $files, $name, $folderPath)
{
$gallery = [];
foreach ($files as $key => $file) {
$image = self::createImageFromFile($file, $name .'-'. $key, $folderPath);
if ($image) {
$hotspot = new Hotspotimage();
$hotspot->setImage($image);
$gallery[] = $hotspot;
}
}
return new ImageGallery($gallery);
}
public static function createGalleryFromUrls(array $urls, $name, $folderPath)
{
$gallery = [];
foreach ($urls as $key => $url) {
$image = self::createImageFromUrl($url, $name .'-'. $key, $folderPath);
if ($image) {
$hotspot = new Hotspotimage();
$hotspot->setImage($image);
$gallery[] = $hotspot;
}
}
return new ImageGallery($gallery);
}
private static function createImage($dataPath, $name, $folderPath)
{
try {
$folder = Asset::getByPath($folderPath) ?? Asset\Service::createFolderByPath($folderPath);
$image = new Asset\Image();
$image->setFileName($name);
$image->setData(@file_get_contents($dataPath));
$image->setParent($folder);
if ($image->save()) {
return $image;
}
} catch (\Throwable $e) {
LogHelper::logError(self::LOG_FILE_NAME, (string) ($e ."\n \n"));
}
return null;
}
private static function formatName($name, $extension = null)
{
$name = str_replace([' ', '/'], ['_', '+'], ltrim($name)) .'('. time() .')';
if ($extension) {
$name .= '.'. $extension;
}
return $name;
}
}