Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit a6b222f7 authored by Nicolas's avatar Nicolas
Browse files

- Ajout des paramétrages System.xml

- Remplacement du ResizeImage par l'Helper ImageOptimizer
- Suppression des fonctions pour les images responsive
parent 315a5a6f
No related branches found
No related tags found
No related merge requests found
......@@ -7,9 +7,12 @@ use \Magento\Framework\App\Filesystem\DirectoryList;
use \Magento\Framework\Image\AdapterFactory as ImageAdapterFactory;
use \Magento\Framework\View\Element\Template;
use \Magento\Framework\View\Element\Template\Context;
use \Magento\Store\Model\ScopeInterface;
use \Magento\Store\Model\StoreManagerInterface;
use \Magento\Widget\Block\BlockInterface;
use \NicolasBejean\Base\Helper\ImageOptimizer;
use \NicolasBejean\ImageSliderManager\Model\ImageSlider as ImageSliderModel;
use \NicolasBejean\ImageSliderManager\Model\ImageSliderRepository;
use \NicolasBejean\ImageSliderManager\Model\ImageSliderFactory;
......@@ -18,8 +21,6 @@ use \NicolasBejean\ImageSliderManager\Model\Template\FilterProvider;
use \NicolasBejean\MediaManager\Model\ImageRepository;
use \NicolasBejean\MediaManager\Model\Image as ImageModel;
use \Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
use \Exception;
use \Magento\Framework\Exception\LocalizedException;
use \Magento\Framework\Exception\NoSuchEntityException;
......@@ -55,7 +56,7 @@ class ImageSlider extends Template implements BlockInterface
/**
* @var ImageAdapterFactory
*/
protected $ImageAdapterFactory;
protected $imageAdapterFactory;
/**
* Store manager
......@@ -86,30 +87,12 @@ class ImageSlider extends Template implements BlockInterface
* if keepAspectRatio(false).
* - backgroundColor[null]: Default white
*/
protected $defaultSettings = [
'constrainOnly' => true,
'keepAspectRatio' => true,
'keepTransparency' => true,
'keepFrame' => false,
'backgroundColor' => null
];
/**
* @var array
*/
protected $sizeSettings = [];
protected $defaultSettings = [];
/**
* @var array
*/
protected $defaultSizeSettings = [
320, 480, 640, 800, 960, 1120, 1280, 1440, 1600, 1920, 2560, 3040, 3840
];
/**
* @var JsonSerializer
* @var ImageOptimizer
*/
private $jsonSerializer;
protected $imageOptimizer;
/**
* @var ImageRepository
......@@ -126,34 +109,44 @@ class ImageSlider extends Template implements BlockInterface
* @param FilterProvider $filterProvider
* @param ImageSliderRepository $imageSliderRepository
* @param ImageSliderFactory $imageSliderFactory
* @param ImageAdapterFactory $ImageAdapterFactory
* @param JsonSerializer $jsonSerializer
* @param ImageAdapterFactory $imageAdapterFactory
* @param ImageRepository $imageRepository
* @param array $data
*/
public function __construct(
Context $context,
FilterProvider $filterProvider,
ImageSliderRepository $imageSliderRepository,
ImageSliderFactory $imageSliderFactory,
ImageSliderRepository $imageSliderRepository,
ImageRepository $imageRepository,
ImageAdapterFactory $ImageAdapterFactory,
JsonSerializer $jsonSerializer,
ImageOptimizer $imageOptimizer,
ImageAdapterFactory $imageAdapterFactory,
array $data = []
) {
$this->filterProvider = $filterProvider;
$this->storeManager = $context->getStoreManager();
$this->filesystem = $context->getFilesystem();
$this->imageSliderRepository = $imageSliderRepository;
$this->filterProvider = $filterProvider;
$this->imageSliderFactory = $imageSliderFactory;
$this->imageSliderRepository = $imageSliderRepository;
$this->imageRepository = $imageRepository;
$this->ImageAdapterFactory = $ImageAdapterFactory;
$this->storeManager = $context->getStoreManager();
$this->filesystem = $context->getFilesystem();
$this->jsonSerializer = $jsonSerializer;
$this->imageOptimizer = $imageOptimizer;
$this->imageAdapterFactory = $imageAdapterFactory;
parent::__construct($context, $data);
$this->defaultSettings = [
'constrainOnly' => $this->_scopeConfig->getValue('mediamanagerimage/resize/constrain_only', ScopeInterface::SCOPE_STORE),
'keepAspectRatio' => $this->_scopeConfig->getValue('mediamanagerimage/resize/keep_aspect_ratio', ScopeInterface::SCOPE_STORE),
'keepTransparency' => $this->_scopeConfig->getValue('mediamanagerimage/resize/keep_transparency', ScopeInterface::SCOPE_STORE),
'keepFrame' => $this->_scopeConfig->getValue('mediamanagerimage/resize/keep_frame', ScopeInterface::SCOPE_STORE),
'backgroundColor' => $this->_scopeConfig->getValue('mediamanagerimage/resize/background_color', ScopeInterface::SCOPE_STORE),
'identifier' => $this->_scopeConfig->getValue('mediamanagerimage/save/identifier', ScopeInterface::SCOPE_STORE),
'basename' => $this->_scopeConfig->getValue('mediamanagerimage/save/basename', ScopeInterface::SCOPE_STORE),
'width' => $this->_scopeConfig->getValue('mediamanagerimage/save/width', ScopeInterface::SCOPE_STORE),
'height' => $this->_scopeConfig->getValue('mediamanagerimage/save/height', ScopeInterface::SCOPE_STORE),
'compression' => $this->_scopeConfig->getValue('mediamanagerimage/save/compression', ScopeInterface::SCOPE_STORE)
];
}
/**
......@@ -184,9 +177,8 @@ class ImageSlider extends Template implements BlockInterface
$result = array();
/** @var array $image */
foreach ($array as $image) {
array_push($result, $this->getImage($image));
array_push($result, $this->getImage((int)$image));
}
return $result;
......@@ -226,7 +218,11 @@ class ImageSlider extends Template implements BlockInterface
public function getActiveTitle()
{
if ($this->getWidgetTitle() !== null) {
return (boolean)$this->jsonSerializer->unserialize(strtolower($this->getData('active_title')));
if ($this->getData('active_title') === 'true') {
return true;
}
return false;
}
return false;
......@@ -262,7 +258,11 @@ class ImageSlider extends Template implements BlockInterface
public function getActiveContent()
{
if ($this->getWidgetContent() !== null) {
return (boolean)$this->jsonSerializer->unserialize(strtolower($this->getData('active_content')));
if ($this->getData('active_content') === 'true') {
return true;
}
return false;
}
return false;
......@@ -273,6 +273,10 @@ class ImageSlider extends Template implements BlockInterface
*/
public function getWidgetContent()
{
if (strlen($this->getData('content')) === 0) {
return false;
}
return $this->getData('content');
}
......@@ -281,6 +285,10 @@ class ImageSlider extends Template implements BlockInterface
*/
public function getWidgetContentCSS()
{
if (strlen($this->getData('content_css_classes')) === 0) {
return false;
}
return $this->getData('content_css_classes');
}
......@@ -289,7 +297,11 @@ class ImageSlider extends Template implements BlockInterface
*/
public function getActiveResize()
{
return (boolean)$this->jsonSerializer->unserialize(strtolower($this->getData('active_resize')));
if ($this->getData('active_resize') === 'true') {
return true;
}
return false;
}
/**
......@@ -298,7 +310,7 @@ class ImageSlider extends Template implements BlockInterface
public function getWidth()
{
if (is_null($this->getData('width'))) {
return 450;
return $this->defaultSettings['width'];
}
return $this->getData('width');
......@@ -310,37 +322,21 @@ class ImageSlider extends Template implements BlockInterface
public function getHeight()
{
if (is_null($this->getData('height'))) {
return 450;
return $this->defaultSettings['height'];
}
return $this->getData('height');
}
/**
* Récupère le taux de compression de l'image
*/
public function getCompression()
{
if (is_null($this->getData('quality'))) {
return 60;
}
return $this->getData('quality');
}
/**
* Récupère le booléen pour activer les images responsives
*/
public function getActiveResponsiveImage()
{
return (boolean)$this->jsonSerializer->unserialize(strtolower($this->getData('active_responsive_image')));
}
/**
* Récupère les classes CSS
*/
public function getCssClasses()
{
if (strlen($this->getData('css_classes')) === 0) {
return false;
}
return $this->getData('css_classes');
}
......@@ -349,6 +345,10 @@ class ImageSlider extends Template implements BlockInterface
*/
public function getExtraCss()
{
if (strlen($this->getData('extra_css')) === 0) {
return false;
}
return $this->getData('extra_css');
}
......@@ -357,6 +357,10 @@ class ImageSlider extends Template implements BlockInterface
*/
public function getDataBind()
{
if (strlen($this->getData('databind')) === 0) {
return false;
}
return $this->getData('databind');
}
......@@ -365,7 +369,11 @@ class ImageSlider extends Template implements BlockInterface
*/
public function getActiveWrapper()
{
return (boolean)$this->jsonSerializer->unserialize(strtolower($this->getData('active_wrapper')));
if ($this->getData('active_wrapper') === 'true') {
return true;
}
return false;
}
/**
......@@ -373,6 +381,10 @@ class ImageSlider extends Template implements BlockInterface
*/
public function getWrapperCssClasses()
{
if (strlen($this->getData('wrapper_css_classes')) === 0) {
return false;
}
return $this->getData('wrapper_css_classes');
}
......@@ -381,7 +393,11 @@ class ImageSlider extends Template implements BlockInterface
*/
public function getActiveLink()
{
return (boolean)$this->jsonSerializer->unserialize(strtolower($this->getData('active_link')));
if ($this->getData('active_link') === 'true') {
return true;
}
return false;
}
/**
......@@ -419,20 +435,29 @@ class ImageSlider extends Template implements BlockInterface
/**
* Permet d'optimiser les images
*
* @param $image
* @param null $width
* @param null $height
* @param array $resizeSettings
* @param array $settings
* @return string
* @throws NoSuchEntityException
* @throws Exception
*/
public function getResizeImage($image, $width = null, $height = null, array $resizeSettings = [])
public function getResizeImage($image, $width = null, $height = null, array $settings = [])
{
$this->initResizeSettings($resizeSettings);
$baseURL = $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
$dirPath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
/* Si c'est pas un fichier JPG, on retourne l'original */
if (substr($image, -3) != 'jpg') {
return $baseURL . $image;
}
/* Si il n'y a pas de resize activé, on retourne l'original */
if ($this->getData('image_active_resize') === 'false') {
return $baseURL . $image;
}
$settings['basename'] = $image;
if ($image) {
if (is_string($image)) {
......@@ -445,113 +470,56 @@ class ImageSlider extends Template implements BlockInterface
}
}
/**
* Si différent d'un fichier JPG, pas de traitement
*/
if (substr($image, -3) != 'jpg' && $this->getActiveResize() == false) {
return $baseURL . $image;
$dirPath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
$absolutePath = $dirPath->getAbsolutePath('') . $image;
if (!is_null($width)) {
$settings['width'] = (int)$width;
}
$absolutePath = $dirPath->getAbsolutePath('') . $image;
if (!is_null($height)) {
$settings['height'] = (int)$height;
}
$imageResized = $dirPath->getAbsolutePath('resized/' . $width . '/') . $image;
/**
* Créé $imageAdapter
*/
$imageAdapter = $this->ImageAdapterFactory->create();
$imageAdapter->open($absolutePath);
/**
* If the "constrainOnly" parameter is set to true,
* in this case the images which are smaller than specified value will be not enlarged by Magento.
* Only border of such images will increase.
* This is useful if you have small product images and you don't like when Magento pixelate them.
* This option will not effect images which are bigger than specified value.
*/
$imageAdapter->constrainOnly($this->resizeSettings['constrainOnly']);
/**
* If the "keepAspectRatio" parameter is set to true,
* in this case the proportions of the image will not be modified.
*/
$imageAdapter->keepAspectRatio($this->resizeSettings['keepAspectRatio']);
/**
* The "keepTransparency" parameter keep the transparent background of the images.
* If the "keepTransparency" parameter is set to false,
* in this case such images will have white background (by default).
* You can set any color for the background using the backgroundColor parameter.
*/
$imageAdapter->keepTransparency($this->resizeSettings['keepTransparency']);
/**
* The "keepFrame" parameter guarantees that the image will be not cropped.
* When "keepAspectRatio" is false the "keepFrame" will not work.
*/
$imageAdapter->keepFrame($this->resizeSettings['keepFrame']);
/**
* The "backgroundColor" allows to set any color as image background.
* You can enter a color as a RGB code, example: backgroundColor(array(255,255,255)).
* If the "keepTransparency" parameter is set to true,
* in this case the background will be not applied to the images with transparency.
*/
$imageAdapter->backgroundColor($this->resizeSettings['backgroundColor']);
$imageAdapter->quality($this->getCompression());
$imageAdapter->resize($width, $height);
/**
* Enregistre l'image dans le dossier de destination
*/
$destination = $imageResized ;
$imageAdapter->save($destination);
$resizedURL = $baseURL . 'resized/' . $width . '/' . $image;
return $resizedURL;
if (!is_null($this->getData('image_quality'))) {
$settings['compression'] = (int)$this->getData('image_quality');
}
/* Initialise les options de redimensionnement */
$this->initResizeSettings($settings);
try {
return $this->imageOptimizer->getResizeImage($absolutePath, $dirPath->getAbsolutePath(''), $this->resizeSettings);
} catch (Exception $e) {
return $baseURL . $image;
}
}
/**
* Prepare and set size settings for srcset
* Récupère la largeur de l'image du widget
*
* @param array $sizeSettings
* @param $imagePath
* @return int|mixed|null
*/
protected function initSizeSettings(array $sizeSettings)
public function getResizedImageWidth($imagePath)
{
$this->sizeSettings = $this->defaultSizeSettings;
$dirPath = $this->filesystem->getDirectoryRead(DirectoryList::PUB);
$absoluteImagePath = $dirPath->getAbsolutePath($imagePath);
foreach ($sizeSettings as $key => $value) {
if (array_key_exists($key, $this->sizeSettings)) {
$this->sizeSettings[$key] = $value;
}
}
return $this->imageOptimizer->getImageWidth($absoluteImagePath);
}
/**
* Prépare les images pour srcset
* Récupère la hauteur de l'image du widget
*
* @param string $imagePath
* @param number $width
* @param array $sizeSettings
* @return string
* @throws NoSuchEntityException
* @param $imagePath
* @return int|mixed|null
*/
public function getResponsiveImage($imagePath, $width, array $sizeSettings = [])
public function getResizedImageHeight($imagePath)
{
$this->initSizeSettings($sizeSettings);
if (substr($imagePath, -3) != 'jpg') {
return '';
} else {
$srcset = $this->getResizeImage($imagePath, $width, $width) . ' ' . $width . 'w';
$dirPath = $this->filesystem->getDirectoryRead(DirectoryList::PUB);
$absoluteImagePath = $dirPath->getAbsolutePath($imagePath);
foreach ($this->sizeSettings as $size) {
if ($size <= ($this->getWidth() * 2)) {
$srcset .= ', ' . $this->getResizeImage($imagePath, $size, $size) . ' ' . $size . 'w';
} else {
break;
}
}
return $srcset;
}
return $this->imageOptimizer->getImageHeight($absoluteImagePath);
}
}
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="imageslidermanager" translate="label" sortOrder="15" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>Image Slider Manager</label>
<tab>nicolasbejean</tab>
<resource>NicolasBejean_ImageSliderManager::global</resource>
<group id="resize" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Image Resize Settings</label>
<comment>Default Resize Settings</comment>
<field id="constrain_only" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Constrain Only</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Guarantee, that image picture will not be bigger, than it was. It is false by default.</comment>
</field>
<field id="keep_aspect_ratio" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Keep Aspect Ratio</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Guarantee, that image picture width/height will not be distorted. It is true by default.</comment>
</field>
<field id="keep_transparency" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Keep Transparency</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Guarantee, that image will not lose transparency if any. It is true by default.</comment>
</field>
<field id="keep_frame" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Keep Frame</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Guarantee, that image will have dimensions, set in $width/$height. Not applicable, if keepAspectRatio(false).</comment>
</field>
<field id="background_color" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Background Color</label>
<comment>Default white</comment>
</field>
</group>
<group id="save" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Image Saved Settings</label>
<comment>Default Save Settings</comment>
<field id="identifier" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Identifier</label>
</field>
<field id="basename" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Basename</label>
</field>
<field id="width" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Width</label>
</field>
<field id="height" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Height</label>
</field>
<field id="compression" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Compression</label>
</field>
</group>
</section>
</system>
</config>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<imageslidermanager>
<resize>
<constrain_only>0</constrain_only>
<keep_aspect_ratio>1</keep_aspect_ratio>
<keep_transparency>1</keep_transparency>
<keep_frame>0</keep_frame>
<background_color>#ffffff</background_color>
</resize>
<save>
<identifier>ism</identifier>
<basename></basename>
<width>1920</width>
<height>1080</height>
<compression>60</compression>
</save>
</imageslidermanager>
</default>
</config>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment