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

Ajout de l'Helper ImageOptimizer

parent 02385d9f
No related branches found
No related tags found
No related merge requests found
Pipeline #496 failed
# Changelog # Changelog
## [1.3.2] - 2020-04-17
### Added
- Ajout de l'Helper ImageOptimizer
## [1.2.2] - 2020-01-01 ## [1.2.2] - 2020-01-01
### Deleted ### Deleted
- Suppression de l'API Alexa - Suppression de l'API Alexa
......
<?php
namespace NicolasBejean\Base\Helper;
use \Magento\Framework\App\Helper\AbstractHelper;
use \Magento\Framework\App\Helper\Context;
use \Magento\Framework\Image\Factory as ImageFactory;
use \Magento\Framework\Image\AdapterFactory as ImageAdapterFactory;
use \Exception;
/**
* Class ImageOptimizer
*
* @category PHP
* @package NicolasBejean\Base\Helper
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/base/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class ImageOptimizer extends AbstractHelper
{
/**
* @var ImageAdapterFactory
*/
protected $imageAdapterFactory;
/**
* @var ImageFactory
*/
protected $imageFactory;
/**
* @var array
*/
protected $resizeSettings = [];
/**
* @var array
*
* - constrainOnly[true]: Guarantee, that image picture will not be bigger, than it was. It is false by default.
* - keepAspectRatio[true]: Guarantee, that image picture width/height will not be distorted. It is true by default.
* - keepTransparency[true]: Guarantee, that image will not lose transparency if any. It is true by default.
* - keepFrame[false]: Guarantee, that image will have dimensions, set in $width/$height. Not applicable,
* if keepAspectRatio(false).
* - backgroundColor[null]: Default white
*/
protected $defaultSettings = [
'constrainOnly' => true,
'keepAspectRatio' => true,
'keepTransparency' => true,
'keepFrame' => false,
'backgroundColor' => null,
'identifier' => '',
'basename' => '',
'width' => 1920,
'height' => 1080,
'compression' => 60
];
/**
* ImageOptimizer constructor.
*
* @param Context $context
* @param ImageAdapterFactory $imageAdapterFactory
* @param ImageFactory $imageFactory
*/
public function __construct(
Context $context,
ImageAdapterFactory $imageAdapterFactory,
ImageFactory $imageFactory
) {
$this->imageAdapterFactory = $imageAdapterFactory;
$this->imageFactory = $imageFactory;
parent::__construct($context);
}
/**
* Retourne la largeur de l'image
*
* @param string $imagePath
* @return int|string|null
*/
public function getImageWidth (string $imagePath)
{
try {
$image = $this->imageFactory->create($imagePath);
$image->open();
return $image->getOriginalWidth();
} catch (Exception $e) {
return '';
}
}
/**
* Retourne la hauteur de l'image
*
* @param string $imagePath
* @return int|string|null
*/
public function getImageHeight (string $imagePath)
{
try {
$image = $this->imageFactory->create($imagePath);
$image->open();
return $image->getOriginalHeight();
} catch (Exception $e) {
return '';
}
}
/**
* Permet d'optimiser les images
*
* @param string $image
* @param string $destinationPath
* @param array $settings
* @return string
* @throws Exception
*/
public function getResizeImage(string $image, string $destinationPath, array $settings = [])
{
/* Initialise les options de redimensionnement */
$this->initResizeSettings($settings);
$imageAdapter = $this->imageAdapterFactory->create();
$imageAdapter->open($image);
/**
* 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->resizeSettings['compression']);
$imageAdapter->resize($this->resizeSettings['width'], $this->resizeSettings['height']);
$savedFile = '/imageoptimizer/' . $this->resizeSettings['identifier'] . '/' . $this->resizeSettings['width'] . '/' . $this->resizeSettings['compression'] . '/' . $this->resizeSettings['basename'];
/* Enregistrement de l'image redimensionnée */
$imageAdapter->save($destinationPath . $savedFile);
return '/media' . $savedFile;
}
/**
* Fusionne les paramètres avec ceux par défaut
*
* @param array $data
*/
protected function initResizeSettings(array $data)
{
// Init resize settings with default
$this->resizeSettings = $this->defaultSettings;
// Override resizeSettings only if key matches with existing settings
foreach ($data as $key => $value) {
if (array_key_exists($key, $this->resizeSettings)) {
$this->resizeSettings[$key] = $value;
}
}
}
}
...@@ -2,9 +2,10 @@ ...@@ -2,9 +2,10 @@
"name": "nicolasbejean/base", "name": "nicolasbejean/base",
"description": "Gestionnaire de base des modules de Nicolas Béjean", "description": "Gestionnaire de base des modules de Nicolas Béjean",
"type": "magento2-module", "type": "magento2-module",
"version": "1.2.2", "version": "1.3.2",
"require": { "require": {
"php": "^7.0" "php": "~7.1.3||~7.2.0||~7.3.0",
"magento/framework": "102.0.*"
}, },
"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_Base" setup_version="1.1.2" /> <module name="NicolasBejean_Base" setup_version="1.3.2" />
</config> </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