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 adjustments #160

Merged
merged 9 commits into from
Mar 16, 2018
1 change: 0 additions & 1 deletion Controller/Adminhtml/FastlyCdn/Vcl/PushImageSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public function execute()
'name' => $reqName,
'service_id' => $service->id,
'version' => $currActiveVersion['active_version'],
'force_ssl' => true,
'request_condition' => $createCondition->name
];

Expand Down
26 changes: 25 additions & 1 deletion Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,13 @@ class Config extends \Magento\PageCache\Model\Config
* XML path to image optimizations flag
*/
const XML_FASTLY_IMAGE_OPTIMIZATIONS
= 'system/full_page_cache/fastly/fastly_advanced_configuration/image_optimizations';
= 'system/full_page_cache/fastly/fastly_image_optimization_configuration/image_optimizations';

/**
* XML path to image optimizations pixel ratio flag
*/
const XML_FASTLY_IMAGE_OPTIMIZATIONS_PIXEL_RATIO
= 'system/full_page_cache/fastly/fastly_image_optimization_configuration/image_optimizations_pixel_ratio';

/**
* XML path to Google analytics CID
Expand Down Expand Up @@ -455,9 +461,27 @@ public function getGeoIpRedirectMapping()
*/
public function isImageOptimizationEnabled()
{
if ($this->isFastlyEnabled() !== true) {
return false;
}

return $this->_scopeConfig->isSetFlag(self::XML_FASTLY_IMAGE_OPTIMIZATIONS);
}

/**
* Determines should Image optimization pixel ratios be used
*
* @return bool
*/
public function isImageOptimizationPixelRatioEnabled()
{
if ($this->isImageOptimizationEnabled() !== true) {
return false;
}

return $this->_scopeConfig->isSetFlag(self::XML_FASTLY_IMAGE_OPTIMIZATIONS_PIXEL_RATIO);
}

/**
* Return blocked countries
*
Expand Down
4 changes: 0 additions & 4 deletions Model/Product/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ private function adjustSize()
$originalImage = $this->_mediaDirectory->getAbsolutePath($this->getBaseFile());
$originalSize = getimagesize($originalImage);

if ($this->_keepFrame) {
$this->fastlyParameters['canvas'] = implode(',', [$this->getWidth(), $this->getHeight()]);
}

if ($this->getWidth() > $originalSize[0]) {
$this->setWidth($originalSize[0]);
}
Expand Down
46 changes: 46 additions & 0 deletions Plugin/AdaptivePixelRationPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Fastly\Cdn\Plugin;

/**
* Class AdaptivePixelRationPlugin
* @package Fastly\Cdn\Plugin
*/
class AdaptivePixelRationPlugin
{
/**
* @var \Fastly\Cdn\Model\Config
*/
public $config;

/**
* AdaptivePixelRationPlugin constructor.
*
* @param \Fastly\Cdn\Model\Config $config
*/
public function __construct(\Fastly\Cdn\Model\Config $config)
{
$this->config = $config;
}

/**
* Adjust srcset if required
*
* @param \Magento\Catalog\Block\Product\Image $subject
*/
public function beforeToHtml(\Magento\Catalog\Block\Product\Image $subject)
{
if ($this->config->isImageOptimizationPixelRatioEnabled() !== true) {
return;
}

$imageUrl = $subject->getData('image_url');
$glue = (strpos($imageUrl, '?') !== false) ? '&' : '?';
$srcSet = [
$imageUrl . $glue . 'dpr=1.5 1.5x',
$imageUrl . $glue . 'dpr=2 2x'
];

$subject->setData('custom_attributes', 'srcset="' . implode(',', $srcSet) . '"');
}
}
13 changes: 12 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,13 @@
<field id="enable_geoip">1</field>
</depends>
</field>
</group>
<group id="fastly_image_optimization_configuration" sortOrder="450" translate="label comment" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Image optimization</label>
<field id="fastly_push_image_config" translate="label comment" sortOrder="90" showInDefault="1" showInStore="0" showInWebsite="0">
<label><![CDATA[Enable Fastly image optimization snippet]]></label>
<comment>
<![CDATA[Enable the Fastly image optimization snippet.]]>
<![CDATA[Push VCL snippet required for image optimization.]]>
</comment>
<frontend_model>Fastly\Cdn\Block\System\Config\Form\Field\ImageBtn</frontend_model>
</field>
Expand All @@ -208,6 +211,14 @@
<comment><![CDATA[<a href="https://docs.fastly.com/guides/imageopto-setup-use/setup" target="_blank">Image optimization</a> needs to be enabled for your Fastly service.]]></comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="image_optimizations_pixel_ratio" translate="label comment" type="select" sortOrder="95" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable adaptive device pixel ratios</label>
<comment><![CDATA[Magento will use <a href="https://docs.fastly.com/guides/imageopto-setup-use/serving-responsive-images#adaptive-device-pixel-ratios" target="_blank">Adaptive device pixel rations</a> through Fastly service.]]></comment>
<depends>
<field id="image_optimizations">1</field>
</depends>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
<group id="fastly_blocking" sortOrder="245" translate="label comment" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Blocking</label>
Expand Down
3 changes: 3 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<type name="Magento\CacheInvalidate\Model\PurgeCache">
<plugin name="fastly_disable_varnish_calls" type="Fastly\Cdn\Model\PageCache\PurgeCachePlugin" />
</type>
<type name="Magento\Catalog\Block\Product\Image">
<plugin name="fastly_adaptive_pixel_ratio" type="Fastly\Cdn\Plugin\AdaptivePixelRationPlugin" />
</type>
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
Expand Down