Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/image optimization force lossy #186

Merged
merged 2 commits into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*
Expand Down
103 changes: 89 additions & 14 deletions Model/Product/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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()
*
Expand Down Expand Up @@ -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"
Expand All @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@
</comment>
<frontend_model>Fastly\Cdn\Block\System\Config\Form\Field\IoConfigOptionsBtn</frontend_model>
</field>
<field id="image_optimization_force_lossy" translate="label comment" type="select" sortOrder="93" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable Force Lossy conversion</label>
<comment>Enables using lossy conversion when original image is lossless.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="image_optimizations" translate="label comment" type="select" sortOrder="95" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable deep image optimization</label>
<comment><![CDATA[Turns off Magento built-in image resizing and manipulation and offloads it onto Fastly IO.]]></comment>
Expand Down