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 5c978ce7 authored by Nicolas's avatar Nicolas
Browse files

### Added

- Mise en place de la classe PdfRepository pour supprimer le load (deprecated)
- Mise en place de la classe VideoRepository pour supprimer le load (deprecated)
- Ajout d'une classe spécifique dans chaque template de widget
### Changed
- Corrections de l'orthographe de la classe ImageAdapterFactory pour les classes Pdf et Video
- Mise à jour des Helpers et suppression des load (deprecated)
- Remplacement du ResizeImage par l'Helper ImageOptimizer
### Deleted
- Suppression de la classe JsonSerializer et modification du traitement des valeurs en booléen pour les classes Pdf et Video
- Suppression d'une variable dnas le return d'une fonction pour les classes Pdf et Video
parent 2ccb83e7
No related branches found
No related tags found
No related merge requests found
<?php <?php
namespace NicolasBejean\MediaManager\Block\Widget; namespace NicolasBejean\MediaManager\Block\Widget;
use \Magento\Framework\Exception\NoSuchEntityException;
use \Magento\Framework\Filesystem; use \Magento\Framework\Filesystem;
use \Magento\Framework\UrlInterface;
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;
use \Magento\Framework\View\Element\Template\Context; use \Magento\Framework\View\Element\Template\Context;
use \Magento\Store\Model\StoreManagerInterface; use \Magento\Store\Model\StoreManagerInterface;
use \Magento\Widget\Block\BlockInterface; use \Magento\Widget\Block\BlockInterface;
use \Magento\Framework\UrlInterface;
use \Magento\Framework\App\Filesystem\DirectoryList; use \NicolasBejean\Base\Helper\ImageOptimizer;
use \Magento\Framework\Image\AdapterFactory as ImageAdapterFactory;
use \NicolasBejean\MediaManager\Model\Image as ImageModel; use \NicolasBejean\MediaManager\Model\Image as ImageModel;
use \NicolasBejean\MediaManager\Model\ImageRepository as ImageRepository; use \NicolasBejean\MediaManager\Model\ImageRepository as ImageRepository;
use \NicolasBejean\MediaManager\Model\ImageFactory; use \NicolasBejean\MediaManager\Model\ImageFactory;
...@@ -17,6 +19,7 @@ use \NicolasBejean\MediaManager\Model\Template\FilterProvider; ...@@ -17,6 +19,7 @@ use \NicolasBejean\MediaManager\Model\Template\FilterProvider;
use \Exception; use \Exception;
use \Magento\Framework\Exception\LocalizedException; use \Magento\Framework\Exception\LocalizedException;
use \Magento\Framework\Exception\NoSuchEntityException;
/** /**
* Class Image for Widget * Class Image for Widget
...@@ -39,6 +42,18 @@ class Image extends Template implements BlockInterface ...@@ -39,6 +42,18 @@ class Image extends Template implements BlockInterface
*/ */
protected $filterProvider; protected $filterProvider;
/**
* Store manager
*
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* @var Filesystem
*/
private $filesystem;
/** /**
* Image factory * Image factory
* *
...@@ -56,20 +71,6 @@ class Image extends Template implements BlockInterface ...@@ -56,20 +71,6 @@ class Image extends Template implements BlockInterface
*/ */
protected $imageRepository; protected $imageRepository;
/**
* Store manager
*
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* Filesystem instance
*
* @var Filesystem
*/
protected $filesystem;
/** /**
* @var array * @var array
*/ */
...@@ -90,9 +91,19 @@ class Image extends Template implements BlockInterface ...@@ -90,9 +91,19 @@ class Image extends Template implements BlockInterface
'keepAspectRatio' => true, 'keepAspectRatio' => true,
'keepTransparency' => true, 'keepTransparency' => true,
'keepFrame' => false, 'keepFrame' => false,
'backgroundColor' => null 'backgroundColor' => null,
'identifier' => 'mmi',
'basename' => '',
'width' => 1920,
'height' => 1080,
'compression' => 60
]; ];
/**
* @var ImageOptimizer
*/
protected $imageOptimizer;
/** /**
* @var array * @var array
*/ */
...@@ -111,6 +122,7 @@ class Image extends Template implements BlockInterface ...@@ -111,6 +122,7 @@ class Image extends Template implements BlockInterface
* @param ImageFactory $imageFactory * @param ImageFactory $imageFactory
* @param ImageAdapterFactory $imageAdapterFactory * @param ImageAdapterFactory $imageAdapterFactory
* @param ImageRepository $imageRepository * @param ImageRepository $imageRepository
* @param ImageOptimizer $imageOptimizer
* @param array $data * @param array $data
*/ */
public function __construct( public function __construct(
...@@ -119,14 +131,17 @@ class Image extends Template implements BlockInterface ...@@ -119,14 +131,17 @@ class Image extends Template implements BlockInterface
ImageFactory $imageFactory, ImageFactory $imageFactory,
ImageAdapterFactory $imageAdapterFactory, ImageAdapterFactory $imageAdapterFactory,
ImageRepository $imageRepository, ImageRepository $imageRepository,
ImageOptimizer $imageOptimizer,
array $data = [] array $data = []
) { ) {
$this->storeManager = $context->getStoreManager();
$this->filesystem = $context->getFilesystem();
$this->filterProvider = $filterProvider; $this->filterProvider = $filterProvider;
$this->imageFactory = $imageFactory; $this->imageFactory = $imageFactory;
$this->imageAdapterFactory = $imageAdapterFactory; $this->imageAdapterFactory = $imageAdapterFactory;
$this->imageRepository = $imageRepository; $this->imageRepository = $imageRepository;
$this->storeManager = $context->getStoreManager(); $this->imageOptimizer = $imageOptimizer;
$this->filesystem = $context->getFilesystem();
parent::__construct($context, $data); parent::__construct($context, $data);
} }
...@@ -228,25 +243,15 @@ class Image extends Template implements BlockInterface ...@@ -228,25 +243,15 @@ class Image extends Template implements BlockInterface
return $this->getData('image_content_css_classes'); return $this->getData('image_content_css_classes');
} }
/**
* Récupère le booléen pour activer le resize
*/
public function getActiveResize()
{
if ($this->getData('image_active_resize') === 'true') {
return true;
}
return false;
}
/** /**
* Récupère la largeur de l'image du widget * Récupère la largeur de l'image du widget
*
* @return int|mixed|null
*/ */
public function getImageWidth() public function getImageWidth()
{ {
if (is_null($this->getData('image_width'))) { if (is_null($this->getData('image_width'))) {
return 450; return $this->defaultSettings['width'];
} }
return $this->getData('image_width'); return $this->getData('image_width');
...@@ -254,28 +259,18 @@ class Image extends Template implements BlockInterface ...@@ -254,28 +259,18 @@ class Image extends Template implements BlockInterface
/** /**
* Récupère la hauteur de l'image du widget * Récupère la hauteur de l'image du widget
*
* @return int|mixed|null
*/ */
public function getImageHeight() public function getImageHeight()
{ {
if (is_null($this->getData('image_height'))) { if (is_null($this->getData('image_height'))) {
return 450; return $this->defaultSettings['height'];
} }
return $this->getData('image_height'); return $this->getData('image_height');
} }
/**
* Récupère le taux de compression de l'image
*/
public function getImageCompression()
{
if (is_null($this->getData('image_quality'))) {
return 60;
}
return $this->getData('image_quality');
}
/** /**
* Récupère le booléen pour activer les images responsives * Récupère le booléen pour activer les images responsives
*/ */
...@@ -394,17 +389,25 @@ class Image extends Template implements BlockInterface ...@@ -394,17 +389,25 @@ class Image extends Template implements BlockInterface
* @param $image * @param $image
* @param null $width * @param null $width
* @param null $height * @param null $height
* @param array $resizeSettings * @param array $settings
* @return string * @return string
* @throws NoSuchEntityException * @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); $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 ($image) {
if (is_string($image)) { if (is_string($image)) {
...@@ -417,66 +420,29 @@ class Image extends Template implements BlockInterface ...@@ -417,66 +420,29 @@ class Image extends Template implements BlockInterface
} }
} }
/** $dirPath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
* Si différent d'un fichier JPG, pas de traitement $absolutePath = $dirPath->getAbsolutePath('') . $image;
*/
if (substr($image, -3) != 'jpg' && $this->getActiveResize() == false) { if (!is_null($width)) {
return $baseURL . $image; $settings['width'] = (int)$width;
} }
$absolutePath = $dirPath->getAbsolutePath('') . $image; if (!is_null($height)) {
$settings['height'] = (int)$height;
}
if (!is_null($this->getData('image_quality'))) {
$settings['compression'] = (int)$this->getData('image_quality');
}
/* Initialise les options de redimensionnement */
$this->initResizeSettings($settings);
$imageResized = $dirPath->getAbsolutePath('resized/' . $width . '/') . $image; try {
return $this->imageOptimizer->getResizeImage($absolutePath, $dirPath->getAbsolutePath(''), $this->resizeSettings);
/** } catch (Exception $e) {
* Créé $imageAdapter return $baseURL . $image;
*/ }
$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->getImageCompression());
$imageAdapter->resize($width, $height);
/**
* Enregistre l'image dans le dossier de destination
*/
$destination = $imageResized ;
$imageAdapter->save($destination);
return $baseURL . 'resized/' . $width . '/' . $image;
} }
/** /**
...@@ -524,4 +490,32 @@ class Image extends Template implements BlockInterface ...@@ -524,4 +490,32 @@ class Image extends Template implements BlockInterface
return $srcset; return $srcset;
} }
} }
/**
* Récupère la largeur de l'image du widget
*
* @param $imagePath
* @return int|mixed|null
*/
public function getResizedImageWidth($imagePath)
{
$dirPath = $this->filesystem->getDirectoryRead(DirectoryList::PUB);
$absoluteImagePath = $dirPath->getAbsolutePath($imagePath);
return $this->imageOptimizer->getImageWidth($absoluteImagePath);
}
/**
* Récupère la hauteur de l'image du widget
*
* @param $imagePath
* @return int|mixed|null
*/
public function getResizedImageHeight($imagePath)
{
$dirPath = $this->filesystem->getDirectoryRead(DirectoryList::PUB);
$absoluteImagePath = $dirPath->getAbsolutePath($imagePath);
return $this->imageOptimizer->getImageHeight($absoluteImagePath);
}
} }
<?php <?php
namespace NicolasBejean\MediaManager\Block\Widget; namespace NicolasBejean\MediaManager\Block\Widget;
use \Magento\Framework\Exception\NoSuchEntityException;
use \Magento\Framework\Filesystem; use \Magento\Framework\Filesystem;
use \Magento\Framework\UrlInterface;
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;
use \Magento\Framework\View\Element\Template\Context; use \Magento\Framework\View\Element\Template\Context;
use \Magento\Store\Model\StoreManagerInterface; use \Magento\Store\Model\StoreManagerInterface;
use \Magento\Widget\Block\BlockInterface; use \Magento\Widget\Block\BlockInterface;
use \Magento\Framework\UrlInterface;
use \Magento\Framework\App\Filesystem\DirectoryList; use \NicolasBejean\Base\Helper\ImageOptimizer;
use \Magento\Framework\Image\AdapterFactory as ImageAdapterFactory;
use \NicolasBejean\MediaManager\Model\Pdf as PdfModel; use \NicolasBejean\MediaManager\Model\Pdf as PdfModel;
use \NicolasBejean\MediaManager\Model\PdfRepository as PdfRepository; use \NicolasBejean\MediaManager\Model\PdfRepository as PdfRepository;
use \NicolasBejean\MediaManager\Model\PdfFactory; use \NicolasBejean\MediaManager\Model\PdfFactory;
...@@ -17,6 +19,7 @@ use \NicolasBejean\MediaManager\Model\Template\FilterProvider; ...@@ -17,6 +19,7 @@ use \NicolasBejean\MediaManager\Model\Template\FilterProvider;
use \Exception; use \Exception;
use \Magento\Framework\Exception\LocalizedException; use \Magento\Framework\Exception\LocalizedException;
use \Magento\Framework\Exception\NoSuchEntityException;
/** /**
* Class Pdf for Widget * Class Pdf for Widget
...@@ -39,6 +42,20 @@ class Pdf extends Template implements BlockInterface ...@@ -39,6 +42,20 @@ class Pdf extends Template implements BlockInterface
*/ */
protected $filterProvider; protected $filterProvider;
/**
* Store manager
*
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* Filesystem instance
*
* @var Filesystem
*/
protected $filesystem;
/** /**
* Pdf factory * Pdf factory
* *
...@@ -56,20 +73,6 @@ class Pdf extends Template implements BlockInterface ...@@ -56,20 +73,6 @@ class Pdf extends Template implements BlockInterface
*/ */
protected $pdfRepository; protected $pdfRepository;
/**
* Store manager
*
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* Filesystem instance
*
* @var Filesystem
*/
protected $filesystem;
/** /**
* @var array * @var array
*/ */
...@@ -90,15 +93,26 @@ class Pdf extends Template implements BlockInterface ...@@ -90,15 +93,26 @@ class Pdf extends Template implements BlockInterface
'keepAspectRatio' => true, 'keepAspectRatio' => true,
'keepTransparency' => true, 'keepTransparency' => true,
'keepFrame' => false, 'keepFrame' => false,
'backgroundColor' => null 'backgroundColor' => null,
'identifier' => 'mmi',
'basename' => '',
'width' => 1920,
'height' => 1080,
'compression' => 60
]; ];
/**
* @var ImageOptimizer
*/
protected $imageOptimizer;
/** /**
* @param Context $context * @param Context $context
* @param FilterProvider $filterProvider * @param FilterProvider $filterProvider
* @param PdfFactory $pdfFactory * @param PdfFactory $pdfFactory
* @param ImageAdapterFactory $imageAdapterFactory * @param ImageAdapterFactory $imageAdapterFactory
* @param PdfRepository $pdfRepository * @param PdfRepository $pdfRepository
* @param ImageOptimizer $imageOptimizer
* @param array $data * @param array $data
*/ */
public function __construct( public function __construct(
...@@ -107,14 +121,17 @@ class Pdf extends Template implements BlockInterface ...@@ -107,14 +121,17 @@ class Pdf extends Template implements BlockInterface
PdfFactory $pdfFactory, PdfFactory $pdfFactory,
ImageAdapterFactory $imageAdapterFactory, ImageAdapterFactory $imageAdapterFactory,
PdfRepository $pdfRepository, PdfRepository $pdfRepository,
ImageOptimizer $imageOptimizer,
array $data = [] array $data = []
) { ) {
$this->storeManager = $context->getStoreManager();
$this->filesystem = $context->getFilesystem();
$this->filterProvider = $filterProvider; $this->filterProvider = $filterProvider;
$this->pdfFactory = $pdfFactory; $this->pdfFactory = $pdfFactory;
$this->imageAdapterFactory = $imageAdapterFactory; $this->imageAdapterFactory = $imageAdapterFactory;
$this->pdfRepository = $pdfRepository; $this->pdfRepository = $pdfRepository;
$this->storeManager = $context->getStoreManager(); $this->imageOptimizer = $imageOptimizer;
$this->filesystem = $context->getFilesystem();
parent::__construct($context, $data); parent::__construct($context, $data);
} }
...@@ -290,18 +307,6 @@ class Pdf extends Template implements BlockInterface ...@@ -290,18 +307,6 @@ class Pdf extends Template implements BlockInterface
return $this->getData('alt'); return $this->getData('alt');
} }
/**
* Récupère image_active_resize
*/
public function getActiveResize()
{
if ($this->getData('image_active_resize') === 'true') {
return true;
}
return false;
}
/** /**
* Récupère image_width * Récupère image_width
*/ */
...@@ -326,18 +331,6 @@ class Pdf extends Template implements BlockInterface ...@@ -326,18 +331,6 @@ class Pdf extends Template implements BlockInterface
return $this->getData('image_height'); return $this->getData('image_height');
} }
/**
* Récupère image_quality
*/
public function getImageCompression()
{
if (is_null($this->getData('image_quality'))) {
return 60;
}
return $this->getData('image_quality');
}
/** /**
* Récupère active_image_background * Récupère active_image_background
*/ */
...@@ -416,17 +409,26 @@ class Pdf extends Template implements BlockInterface ...@@ -416,17 +409,26 @@ class Pdf extends Template implements BlockInterface
* @param $image * @param $image
* @param null $width * @param null $width
* @param null $height * @param null $height
* @param array $resizeSettings * @param array $settings
* @return string * @return string
* @throws NoSuchEntityException * @throws NoSuchEntityException
* @throws Exception * @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); $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 ($image) {
if (is_string($image)) { if (is_string($image)) {
...@@ -439,65 +441,56 @@ class Pdf extends Template implements BlockInterface ...@@ -439,65 +441,56 @@ class Pdf extends Template implements BlockInterface
} }
} }
/** $dirPath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
* Si différent d'un fichier JPG, pas de traitement $absolutePath = $dirPath->getAbsolutePath('') . $image;
*/
if (substr($image, -3) != 'jpg' && $this->getActiveResize() == false) { if (!is_null($width)) {
$settings['width'] = (int)$width;
}
if (!is_null($height)) {
$settings['height'] = (int)$height;
}
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; return $baseURL . $image;
} }
}
$absolutePath = $dirPath->getAbsolutePath('') . $image; /**
* Récupère la largeur de l'image du widget
*
* @param $imagePath
* @return int|mixed|null
*/
public function getResizedImageWidth($imagePath)
{
$dirPath = $this->filesystem->getDirectoryRead(DirectoryList::PUB);
$absoluteImagePath = $dirPath->getAbsolutePath($imagePath);
return $this->imageOptimizer->getImageWidth($absoluteImagePath);
}
/**
* Récupère la hauteur de l'image du widget
*
* @param $imagePath
* @return int|mixed|null
*/
public function getResizedImageHeight($imagePath)
{
$dirPath = $this->filesystem->getDirectoryRead(DirectoryList::PUB);
$absoluteImagePath = $dirPath->getAbsolutePath($imagePath);
$imageResized = $dirPath->getAbsolutePath('resized/' . $width . '/') . $image; return $this->imageOptimizer->getImageHeight($absoluteImagePath);
/**
* 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->getImageCompression());
$imageAdapter->resize($width, $height);
/**
* Enregistre l'image dans le dossier de destination
*/
$destination = $imageResized ;
$imageAdapter->save($destination);
return $baseURL . 'resized/' . $width . '/' . $image;
} }
} }
<?php <?php
namespace NicolasBejean\MediaManager\Block\Widget; namespace NicolasBejean\MediaManager\Block\Widget;
use \Magento\Framework\Exception\NoSuchEntityException;
use \Magento\Framework\Filesystem; use \Magento\Framework\Filesystem;
use \Magento\Framework\UrlInterface;
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;
use \Magento\Framework\View\Element\Template\Context; use \Magento\Framework\View\Element\Template\Context;
use \Magento\Store\Model\StoreManagerInterface; use \Magento\Store\Model\StoreManagerInterface;
use \Magento\Widget\Block\BlockInterface; use \Magento\Widget\Block\BlockInterface;
use \Magento\Framework\UrlInterface;
use \Magento\Framework\App\Filesystem\DirectoryList; use NicolasBejean\Base\Helper\ImageOptimizer;
use \Magento\Framework\Image\AdapterFactory as ImageAdapterFactory;
use \NicolasBejean\MediaManager\Model\Template\FilterProvider;
use \NicolasBejean\MediaManager\Model\Video as VideoModel; use \NicolasBejean\MediaManager\Model\Video as VideoModel;
use \NicolasBejean\MediaManager\Model\VideoRepository as VideoRepository; use \NicolasBejean\MediaManager\Model\VideoRepository as VideoRepository;
use \NicolasBejean\MediaManager\Model\VideoFactory; use \NicolasBejean\MediaManager\Model\VideoFactory;
use \NicolasBejean\MediaManager\Model\Template\FilterProvider;
use \Exception; use \Exception;
use \Magento\Framework\Exception\LocalizedException; use \Magento\Framework\Exception\LocalizedException;
use \Magento\Framework\Exception\NoSuchEntityException;
/** /**
* Class Video for Widget * Class Video for Widget
...@@ -39,6 +41,20 @@ class Video extends Template implements BlockInterface ...@@ -39,6 +41,20 @@ class Video extends Template implements BlockInterface
*/ */
protected $filterProvider; protected $filterProvider;
/**
* Store manager
*
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* Filesystem instance
*
* @var Filesystem
*/
protected $filesystem;
/** /**
* Video factory * Video factory
* *
...@@ -56,20 +72,6 @@ class Video extends Template implements BlockInterface ...@@ -56,20 +72,6 @@ class Video extends Template implements BlockInterface
*/ */
protected $videoRepository; protected $videoRepository;
/**
* Store manager
*
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* Filesystem instance
*
* @var Filesystem
*/
protected $filesystem;
/** /**
* @var array * @var array
*/ */
...@@ -90,15 +92,26 @@ class Video extends Template implements BlockInterface ...@@ -90,15 +92,26 @@ class Video extends Template implements BlockInterface
'keepAspectRatio' => true, 'keepAspectRatio' => true,
'keepTransparency' => true, 'keepTransparency' => true,
'keepFrame' => false, 'keepFrame' => false,
'backgroundColor' => null 'backgroundColor' => null,
'identifier' => 'mmi',
'basename' => '',
'width' => 1920,
'height' => 1080,
'compression' => 60
]; ];
/**
* @var ImageOptimizer
*/
protected $imageOptimizer;
/** /**
* @param Context $context * @param Context $context
* @param FilterProvider $filterProvider * @param FilterProvider $filterProvider
* @param VideoFactory $videoFactory * @param VideoFactory $videoFactory
* @param ImageAdapterFactory $imageAdapterFactory * @param ImageAdapterFactory $imageAdapterFactory
* @param VideoRepository $videoRepository * @param VideoRepository $videoRepository
* @param ImageOptimizer $imageOptimizer
* @param array $data * @param array $data
*/ */
public function __construct( public function __construct(
...@@ -107,14 +120,17 @@ class Video extends Template implements BlockInterface ...@@ -107,14 +120,17 @@ class Video extends Template implements BlockInterface
VideoFactory $videoFactory, VideoFactory $videoFactory,
ImageAdapterFactory $imageAdapterFactory, ImageAdapterFactory $imageAdapterFactory,
VideoRepository $videoRepository, VideoRepository $videoRepository,
ImageOptimizer $imageOptimizer,
array $data = [] array $data = []
) { ) {
$this->storeManager = $context->getStoreManager();
$this->filesystem = $context->getFilesystem();
$this->filterProvider = $filterProvider; $this->filterProvider = $filterProvider;
$this->videoFactory = $videoFactory; $this->videoFactory = $videoFactory;
$this->imageAdapterFactory = $imageAdapterFactory; $this->imageAdapterFactory = $imageAdapterFactory;
$this->videoRepository = $videoRepository; $this->videoRepository = $videoRepository;
$this->storeManager = $context->getStoreManager(); $this->imageOptimizer = $imageOptimizer;
$this->filesystem = $context->getFilesystem();
parent::__construct($context, $data); parent::__construct($context, $data);
} }
...@@ -407,18 +423,6 @@ class Video extends Template implements BlockInterface ...@@ -407,18 +423,6 @@ class Video extends Template implements BlockInterface
} }
} }
/**
* Récupère image_quality
*/
public function getImageCompression()
{
if (is_null($this->getData('image_quality'))) {
return 60;
}
return $this->getData('image_quality');
}
/** /**
* Récupère active_wrapper * Récupère active_wrapper
*/ */
...@@ -469,17 +473,26 @@ class Video extends Template implements BlockInterface ...@@ -469,17 +473,26 @@ class Video extends Template implements BlockInterface
* @param $image * @param $image
* @param null $width * @param null $width
* @param null $height * @param null $height
* @param array $resizeSettings * @param array $settings
* @return string * @return string
* @throws NoSuchEntityException * @throws NoSuchEntityException
* @throws Exception * @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); $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 ($image) {
if (is_string($image)) { if (is_string($image)) {
...@@ -492,63 +505,28 @@ class Video extends Template implements BlockInterface ...@@ -492,63 +505,28 @@ class Video extends Template implements BlockInterface
} }
} }
/* Si différent d'un fichier JPG, pas de traitement */ $dirPath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
if (substr($image, -3) != 'jpg' && $this->getActiveResize() == false) { $absolutePath = $dirPath->getAbsolutePath('') . $image;
return $baseURL . $image;
if (!is_null($width)) {
$settings['width'] = (int)$width;
} }
$absolutePath = $dirPath->getAbsolutePath('') . $image; if (!is_null($height)) {
$settings['height'] = (int)$height;
}
if (!is_null($this->getData('image_quality'))) {
$settings['compression'] = (int)$this->getData('image_quality');
}
$imageResized = $dirPath->getAbsolutePath('resized/' . $width . '/') . $image; /* Initialise les options de redimensionnement */
$this->initResizeSettings($settings);
/**
* Créé $imageAdapter try {
*/ return $this->imageOptimizer->getResizeImage($absolutePath, $dirPath->getAbsolutePath(''), $this->resizeSettings);
} catch (Exception $e) {
$imageAdapter = $this->imageAdapterFactory->create(); return $baseURL . $image;
$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->getImageCompression());
$imageAdapter->resize($width, $height);
/**
* Enregistre l'image dans le dossier de destination
*/
$destination = $imageResized ;
$imageAdapter->save($destination);
return $baseURL . 'resized/' . $width . '/' . $image;
} }
} }
...@@ -4,9 +4,11 @@ ...@@ -4,9 +4,11 @@
### Added ### Added
- Mise en place de la classe PdfRepository pour supprimer le load (deprecated) - Mise en place de la classe PdfRepository pour supprimer le load (deprecated)
- Mise en place de la classe VideoRepository pour supprimer le load (deprecated) - Mise en place de la classe VideoRepository pour supprimer le load (deprecated)
- Ajout d'une classe spécifique dans chaque template de widget
### Changed ### Changed
- Corrections de l'orthographe de la classe ImageAdapterFactory pour les classes Pdf et Video - Corrections de l'orthographe de la classe ImageAdapterFactory pour les classes Pdf et Video
- Mise à jour des Helpers et suppression des load (deprecated) - Mise à jour des Helpers et suppression des load (deprecated)
- Remplacement du ResizeImage par l'Helper ImageOptimizer
### Deleted ### Deleted
- Suppression de la classe JsonSerializer et modification du traitement des valeurs en booléen pour les classes Pdf et Video - Suppression de la classe JsonSerializer et modification du traitement des valeurs en booléen pour les classes Pdf et Video
- Suppression d'une variable dnas le return d'une fonction pour les classes Pdf et Video - Suppression d'une variable dnas le return d'une fonction pour les classes Pdf et Video
......
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
"name": "nicolasbejean/mediamanager", "name": "nicolasbejean/mediamanager",
"description": "Gestionnaire de médias", "description": "Gestionnaire de médias",
"type": "magento2-module", "type": "magento2-module",
"version": "1.5.12", "version": "1.6.12",
"require": { "require": {
"php": "~7.1.3||~7.2.0||~7.3.0", "php": "~7.1.3||~7.2.0||~7.3.0",
"magento/module-widget": "101.0.*", "magento/module-widget": "101.0.*",
"nicolasbejean/base": "1.0.*" "nicolasbejean/base": ">=1.3.2"
}, },
"license": [ "license": [
"OSL-3.0", "OSL-3.0",
......
<?xml version="1.0"?> <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="NicolasBejean_MediaManager" setup_version="1.5.12"> <module name="NicolasBejean_MediaManager" setup_version="1.6.12">
<sequence> <sequence>
<module name="Magento_Widget" /> <module name="Magento_Widget" />
<module name="NicolasBejean_Base"/> <module name="NicolasBejean_Base"/>
......
...@@ -16,6 +16,8 @@ $activeLink = $block->getActiveLink(); ...@@ -16,6 +16,8 @@ $activeLink = $block->getActiveLink();
if ($activeImage): if ($activeImage):
try { try {
$image = $block->getResizeImage($imagePath, $block->getImageWidth(), $block->getImageHeight()); $image = $block->getResizeImage($imagePath, $block->getImageWidth(), $block->getImageHeight());
$imageWidth = $block->getResizedImageWidth($image);
$imageHeight = $block->getResizedImageHeight($image);
} catch (Exception $e) { } catch (Exception $e) {
} }
...@@ -32,7 +34,7 @@ endif; ...@@ -32,7 +34,7 @@ endif;
<?php if ($activeImage): ?> <?php if ($activeImage): ?>
<?php if ($activeImageWrapper): ?> <?php if ($activeImageWrapper): ?>
<div class="widget block block-static-block mediamanager <?php if ($block->getWrapperCssClasses()): ?><?= /* @noEscape */ $block->getWrapperCssClasses(); ?><?php endif; ?>"> <div class="widget block block-static-block mediamanager mediamanager-image <?php if ($block->getWrapperCssClasses()): ?><?= /* @noEscape */ $block->getWrapperCssClasses(); ?><?php endif; ?>">
<?php endif; ?> <?php endif; ?>
<?php if ($activeTitle || $activeContent): ?> <?php if ($activeTitle || $activeContent): ?>
<div class="mediamanager-title-content <?php if ($block->getWidgetTitleCSS()): ?><?= /* @noEscape */ $block->getWidgetTitleCSS(); ?><?php endif; ?>"> <div class="mediamanager-title-content <?php if ($block->getWidgetTitleCSS()): ?><?= /* @noEscape */ $block->getWidgetTitleCSS(); ?><?php endif; ?>">
...@@ -64,8 +66,8 @@ endif; ...@@ -64,8 +66,8 @@ endif;
<?php if ($block->getExtraCss()): ?>style="<?= /* @noEscape */ $block->getExtraCss(); ?>"<?php endif; ?> <?php if ($block->getExtraCss()): ?>style="<?= /* @noEscape */ $block->getExtraCss(); ?>"<?php endif; ?>
<?php if ($block->getDataBind()): ?>data-bind="<?= /* @noEscape */ $block->getDataBind(); ?>"<?php endif; ?> <?php if ($block->getDataBind()): ?>data-bind="<?= /* @noEscape */ $block->getDataBind(); ?>"<?php endif; ?>
<?php if ($activeResponsiveImage): ?>srcset="<?= /* @noEscape */ $responsiveImage; ?>"<?php endif; ?> <?php if ($activeResponsiveImage): ?>srcset="<?= /* @noEscape */ $responsiveImage; ?>"<?php endif; ?>
<?php if ($block->getImageWidth()): ?>width="<?= /* @noEscape */ $block->getImageWidth(); ?>"<?php endif; ?> width="<?= /* @noEscape */ $imageWidth; ?>"
<?php if ($block->getImageHeight()): ?>height="<?= /* @noEscape */ $block->getImageHeight(); ?>"<?php endif; ?> height="<?= /* @noEscape */ $imageHeight; ?>"
/> />
</div> </div>
<?php endif; ?> <?php endif; ?>
......
...@@ -17,6 +17,8 @@ $activeImageBackground = $block->getActiveImageBackground(); ...@@ -17,6 +17,8 @@ $activeImageBackground = $block->getActiveImageBackground();
if ($imagePath != null): if ($imagePath != null):
try { try {
$image = $block->getResizeImage($imagePath, $block->getImageWidth(), $block->getImageHeight()); $image = $block->getResizeImage($imagePath, $block->getImageWidth(), $block->getImageHeight());
$imageWidth = $block->getResizedImageWidth($image);
$imageHeight = $block->getResizedImageHeight($image);
} catch (Exception $e) { } catch (Exception $e) {
} }
...@@ -25,7 +27,7 @@ endif; ...@@ -25,7 +27,7 @@ endif;
<?php if ($activePdf): ?> <?php if ($activePdf): ?>
<?php if ($activeWrapper): ?> <?php if ($activeWrapper): ?>
<div class="widget block block-static-block mediamanager <?php if ($block->getWrapperCssClasses()): ?><?= /* @noEscape */ $block->getWrapperCssClasses(); ?><?php endif; ?>" <?php if ($block->getWrapperExtraCss()): ?>style="<?= /* @noEscape */ $block->getWrapperExtraCss(); ?>"<?php endif; ?>> <div class="widget block block-static-block mediamanager mediamanager-pdf <?php if ($block->getWrapperCssClasses()): ?><?= /* @noEscape */ $block->getWrapperCssClasses(); ?><?php endif; ?>" <?php if ($block->getWrapperExtraCss()): ?>style="<?= /* @noEscape */ $block->getWrapperExtraCss(); ?>"<?php endif; ?>>
<?php endif; ?> <?php endif; ?>
<?php if ($activeTitle || $activeContent): ?> <?php if ($activeTitle || $activeContent): ?>
<div class="mediamanager-title-content"> <div class="mediamanager-title-content">
...@@ -51,8 +53,8 @@ endif; ...@@ -51,8 +53,8 @@ endif;
<?php if ($block->getImageCssClasses()): ?>class="<?= /* @noEscape */ $block->getImageCssClasses(); ?>"<?php endif; ?> <?php if ($block->getImageCssClasses()): ?>class="<?= /* @noEscape */ $block->getImageCssClasses(); ?>"<?php endif; ?>
<?php if ($block->getImageAlt()): ?>alt="<?= /* @noEscape */ $block->getImageAlt(); ?>"<?php endif; ?> <?php if ($block->getImageAlt()): ?>alt="<?= /* @noEscape */ $block->getImageAlt(); ?>"<?php endif; ?>
<?php if ($block->getImageExtraCss()): ?>style="<?= /* @noEscape */ $block->getImageExtraCss(); ?>"<?php endif; ?> <?php if ($block->getImageExtraCss()): ?>style="<?= /* @noEscape */ $block->getImageExtraCss(); ?>"<?php endif; ?>
<?php if ($block->getImageWidth()): ?>width="<?= /* @noEscape */ $block->getImageWidth(); ?>"<?php endif; ?> width="<?= /* @noEscape */ $imageWidth; ?>"
<?php if ($block->getImageHeight()): ?>height="<?= /* @noEscape */ $block->getImageHeight(); ?>"<?php endif; ?> height="<?= /* @noEscape */ $imageHeight; ?>"
/> />
<?php endif; ?> <?php endif; ?>
<?php else: ?> <?php else: ?>
......
...@@ -24,7 +24,7 @@ endif; ...@@ -24,7 +24,7 @@ endif;
<?php if ($activeVideo): ?> <?php if ($activeVideo): ?>
<?php if ($activeWrapper): ?> <?php if ($activeWrapper): ?>
<div class="widget block block-static-block mediamanager <?php if ($block->getWrapperCssClasses()): ?><?= /* @noEscape */ $block->getWrapperCssClasses(); ?><?php endif; ?>" <?php if ($block->getWrapperExtraCss()): ?>style="<?= /* @noEscape */ $block->getWrapperExtraCss(); ?>"<?php endif; ?>> <div class="widget block block-static-block mediamanager mediamanager-video <?php if ($block->getWrapperCssClasses()): ?><?= /* @noEscape */ $block->getWrapperCssClasses(); ?><?php endif; ?>" <?php if ($block->getWrapperExtraCss()): ?>style="<?= /* @noEscape */ $block->getWrapperExtraCss(); ?>"<?php endif; ?>>
<?php endif; ?> <?php endif; ?>
<?php if ($activeTitle || $activeContent): ?> <?php if ($activeTitle || $activeContent): ?>
<div class="mediamanager-title-content"> <div class="mediamanager-title-content">
......
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