diff --git a/Model/Config.php b/Model/Config.php
index 3443c024..803766c6 100644
--- a/Model/Config.php
+++ b/Model/Config.php
@@ -174,6 +174,9 @@ class Config extends \Magento\PageCache\Model\Config
const XML_FASTLY_IMAGE_OPTIMIZATIONS
= 'system/full_page_cache/fastly/fastly_image_optimization_configuration/image_optimizations';
+ const XML_FASTLY_FORCE_LOSSY
+ = 'system/full_page_cache/fastly/fastly_image_optimization_configuration/image_optimization_force_lossy';
+
/**
* XML path to image optimizations pixel ratio flag
*/
@@ -493,6 +496,11 @@ public function isImageOptimizationPixelRatioEnabled()
return $this->_scopeConfig->isSetFlag(self::XML_FASTLY_IMAGE_OPTIMIZATIONS_PIXEL_RATIO);
}
+ public function isForceLossyEnabled()
+ {
+ return $this->_scopeConfig->getValue(self::XML_FASTLY_FORCE_LOSSY);
+ }
+
/**
* Return image optimization pixel ratios
*
diff --git a/Model/Product/Image.php b/Model/Product/Image.php
index 48f7ea6f..e5a3473d 100644
--- a/Model/Product/Image.php
+++ b/Model/Product/Image.php
@@ -37,6 +37,26 @@ class Image extends ImageModel
*/
private $isFastlyEnabled = null;
+ /**
+ * @var null
+ */
+ private $isForceLossyEnabled = null;
+
+ /**
+ * @var null
+ */
+ private $lossyParam = null;
+
+ /**
+ * @var null
+ */
+ private $lossyUrl = null;
+
+ /**
+ * @var null
+ */
+ private $fastlyUrl = null;
+
/**
* On/Off switch based on config value
*
@@ -61,6 +81,28 @@ private function isFastlyImageOptimizationEnabled()
return $this->isFastlyEnabled;
}
+ /**
+ * @return bool|null
+ */
+ public function isForceLossyEnabled()
+ {
+ if ($this->isForceLossyEnabled !== null) {
+ return $this->isForceLossyEnabled;
+ }
+
+ $this->isForceLossyEnabled = true;
+
+ if ($this->_scopeConfig->isSetFlag(Config::XML_FASTLY_FORCE_LOSSY) == false) {
+ $this->isForceLossyEnabled = false;
+ }
+
+ if ($this->_scopeConfig->getValue(PageCacheConfig::XML_PAGECACHE_TYPE) !== Config::FASTLY) {
+ $this->isForceLossyEnabled = false;
+ }
+
+ return $this->isForceLossyEnabled;
+ }
+
/**
* Wrapper for original rotate()
*
@@ -208,21 +250,63 @@ public function saveFile()
*/
public function getUrl()
{
- if ($this->isFastlyImageOptimizationEnabled() == false) {
+ // returns original url if IO and force lossy are disabled
+ if ($this->isFastlyImageOptimizationEnabled() == false && $this->isForceLossyEnabled() == false) {
return parent::getUrl();
}
+ // retrieves force lossy url or param if force lossy is enabled
+ if ($this->isForceLossyEnabled() != false) {
+ $this->getForceLossyUrl();
+ }
+ // retrieves Fastly url if force lossy url is not set
+ if (!$this->lossyUrl) {
+ $this->getFastlyUrl();
+ }
+ // returns url with set parameters
+ if ($this->lossyParam) {
+ return $this->fastlyUrl . $this->lossyParam;
+ } elseif ($this->lossyUrl) {
+ return $this->lossyUrl;
+ } else {
+ return $this->fastlyUrl;
+ }
+ }
- return $this->getFastlyUrl();
+ /**
+ * Creates a force lossy url param or url + param depending if IO is disabled or enabled
+ */
+ public function getForceLossyUrl()
+ {
+ $baseFile = $this->getBaseFile();
+ $extension = pathinfo($baseFile, PATHINFO_EXTENSION); // @codingStandardsIgnoreLine
+ $url = $this->getBaseFileUrl($baseFile);
+ if ($extension == 'png' || $extension == 'bmp') {
+ if ($this->isFastlyImageOptimizationEnabled() == false) {
+ $this->lossyUrl = $url . '?format=jpeg';
+ } else {
+ $this->lossyParam = '&format=jpeg';
+ }
+ }
}
/**
- * Builds URL used for fastly service
- *
- * @return string
+ * Creates a url with fastly parameters
*/
public function getFastlyUrl()
{
$baseFile = $this->getBaseFile();
+ $url = $this->getBaseFileUrl($baseFile);
+
+ $this->fastlyParameters['quality'] = $this->_quality;
+ $this->fastlyParameters['bg-color'] = implode(',', $this->_backgroundColor);
+ if ($this->_keepAspectRatio == true) {
+ $this->fastlyParameters['fit'] = 'bounds';
+ }
+ $this->fastlyUrl = $url . '?' . $this->compileFastlyParameters();
+ }
+
+ public function getBaseFileUrl($baseFile)
+ {
if ($baseFile === null) {
$url = $this->_assetRepo->getUrl(
"Magento_Catalog::images/product/placeholder/{$this->getDestinationSubdir()}.jpg"
@@ -234,15 +318,6 @@ public function getFastlyUrl()
$url .= $baseFile;
}
- // Add some default parameters
- $this->fastlyParameters['quality'] = $this->_quality;
- $this->fastlyParameters['bg-color'] = implode(',', $this->_backgroundColor);
- if ($this->_keepAspectRatio == true) {
- $this->fastlyParameters['fit'] = 'bounds';
- }
-
- $url .= '?' . $this->compileFastlyParameters();
-
return $url;
}
diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml
index ae95847b..e7272b14 100644
--- a/etc/adminhtml/system.xml
+++ b/etc/adminhtml/system.xml
@@ -213,6 +213,11 @@
Fastly\Cdn\Block\System\Config\Form\Field\IoConfigOptionsBtn
+
+
+ Enables using lossy conversion when original image is lossless.
+ Magento\Config\Model\Config\Source\Yesno
+