<?php
/**
* project: Pimcore
* User: EBiermann
* Year: 2022
*/
namespace App\Document\Areabrick;
use Pimcore\Http\Request\Resolver\EditmodeResolver;
use Pimcore\Model\Asset\Image\Thumbnail\Config;
use Pimcore\Model\Document\Editable;
use Pimcore\Model\Document\Editable\Image as EditableImage;
use Pimcore\Model\Document\Editable\Area\Info;
use Pimcore\Translation\Translator;
class Image extends AbstractAreabrick {
private $translator;
private $editmode;
public function __construct( Translator $translator, EditmodeResolver $editmodeResolver) {
$this->translator = $translator;
$this->editmode = $editmodeResolver->isEditmode();
}
public function getName() {
return 'Image';
}
public function getDescription() {
return 'Image element';
}
public function action(Info $info)
{
$cssClasses = [];
$cssStyles = [];
$hasLink = false;
$thumbnail = false;
$sqip = false;
$linkEditable = $this->getDocumentEditable( $info->getDocument(), 'link', 'link');
if (!$linkEditable->isEmpty()) {
$hasLink = true;
}
if ($this->getDocumentEditable( $info->getDocument(), 'checkbox', 'border')->isChecked()) {
array_push( $cssClasses, 'image--border');
}
/** @var EditableImage $imageEditable */
$imageEditable = $this->getDocumentEditable( $info->getDocument(), 'image', 'image');
if (!$imageEditable->isEmpty()) {
$image = $imageEditable->getImage();
if ( $image instanceof \Pimcore\Model\Asset\Image ) {
$orientation = \App\Website\Tool\Image::getOrientation( $image );
$ratio = \App\Website\Tool\Image::getRatio( $image );
$width = $image->getWidth();
$height = $image->getHeight();
if ( ! $image->isVectorGraphic() ) {
$sqip = 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeD0iMCIgeT0iMCIgdmlld0JveD0iMCAwIDIgMiIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+PHBhdGggZmlsbD0iI2VjZThlOCIgZD0iTTAgMGgydjJIMHoiLz48L3N2Zz4=';
} else {
$sqip = 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==';
}
}
$thumbnailIdentifier = 'area-image-' . $orientation . '-c' . $info->getParam( 'columns' );
if ( $this->editmode ) {
/**
* create non-existant image thumbnail configuration
* otherwise we get an error on the frontend
*/
if ( Config::exists( $thumbnailIdentifier ) === false ) {
$thumbnailConfig = new Config();
$thumbnailConfig->setName( $thumbnailIdentifier );
$thumbnailConfig->save();
}
}
}
if (!$this->editmode) {
$thumbnail = $thumbnailIdentifier ?? false;
}
$info->setParam( 'thumbnail', $thumbnail);
$info->setParam( 'cssClasses', $cssClasses);
$info->setParam( 'cssStyles', $cssStyles);
$info->setParam( 'hasLink', $hasLink);
$info->setParam( 'lqip', $sqip);
}
// executed after a brick is rendered
public function postRenderAction(Info $info)
{
}
// returns a custom html wrapper element (return an empty string if you don't want a wrapper element)
public function getHtmlTagOpen(Info $info)
{
return '';
}
public function getHtmlTagClose(Info $info)
{
return '';
}
}