Skip to content

Commit 2ccc271

Browse files
authored
Merge pull request #330 from Inchoo/bugfix/#261-IO-issue-with-renamed-images
Added fix for image placeholder when IO is enabled
2 parents 883a73d + a3cdc51 commit 2ccc271

File tree

5 files changed

+73
-44
lines changed

5 files changed

+73
-44
lines changed

Diff for: Model/Config.php

+6
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ class Config extends \Magento\PageCache\Model\Config
327327
const XML_FASTLY_IMAGE_OPTIMIZATIONS
328328
= 'system/full_page_cache/fastly/fastly_image_optimization_configuration/image_optimizations';
329329

330+
/**
331+
* XML path to image verify flag
332+
*/
333+
const XML_FASTLY_IMAGE_VERIFY
334+
= 'system/full_page_cache/fastly/fastly_image_optimization_configuration/image_verify';
335+
330336
/**
331337
* XML path to image optimization force lossy flag
332338
*/

Diff for: Model/Product/Image.php

-43
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,6 @@ private function fastlyResize()
182182

183183
private function adjustSize()
184184
{
185-
$originalImage = $this->_mediaDirectory->getAbsolutePath($this->getBaseFile());
186-
$originalSize = getimagesize($originalImage);
187-
188-
if ($this->getWidth() > $originalSize[0]) {
189-
$this->setWidth($originalSize[0]);
190-
}
191-
192-
if ($this->getHeight() > $originalSize[1]) {
193-
$this->setHeight($originalSize[1]);
194-
}
195-
196185
$this->fastlyParameters['width'] = $this->_width;
197186
$this->fastlyParameters['height'] = $this->_height;
198187

@@ -203,38 +192,6 @@ private function adjustSize()
203192
}
204193
}
205194

206-
/**
207-
* Return resized product image information
208-
*
209-
* @return array
210-
*/
211-
public function getResizedImageInfo()
212-
{
213-
if ($this->isFastlyImageOptimizationEnabled() == false) {
214-
return parent::getResizedImageInfo();
215-
}
216-
217-
// Return image data
218-
if ($this->getBaseFile() !== null) {
219-
return [
220-
0 => $this->getWidth(),
221-
1 => $this->getHeight()
222-
];
223-
}
224-
225-
// No image, parse the placeholder
226-
$asset = $this->_assetRepo->createAsset(
227-
"Magento_Catalog::images/product/placeholder/{$this->getDestinationSubdir()}.jpg"
228-
);
229-
$img = $asset->getSourceFile();
230-
$imageInfo = getimagesize($img);
231-
232-
$this->setWidth($imageInfo[0]);
233-
$this->setHeight($imageInfo[1]);
234-
235-
return $imageInfo;
236-
}
237-
238195
/**
239196
* Wrapper for original saveFile()
240197
*

Diff for: Model/View/Asset/Image.php

+55-1
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424
use Magento\Catalog\Helper\Image as ImageHelper;
2525
use Magento\Catalog\Model\Product\Media\ConfigInterface;
2626
use Magento\Catalog\Model\View\Asset\Image as ImageModel;
27+
use Magento\Framework\App\Filesystem\DirectoryList;
2728
use Magento\Framework\Encryption\EncryptorInterface;
29+
use Magento\Framework\Exception\FileSystemException;
2830
use Magento\Framework\View\Asset\ContextInterface;
2931
use Magento\PageCache\Model\Config as PageCacheConfig;
3032
use Magento\Framework\App\Config\ScopeConfigInterface;
3133
use Magento\Framework\UrlInterface;
3234
use Magento\Store\Model\StoreManagerInterface;
3335
use Magento\Framework\Exception\NoSuchEntityException;
36+
use Magento\Framework\Filesystem\Io\File;
37+
use Magento\Framework\Filesystem;
3438

3539
/**
3640
* Class Image
@@ -62,6 +66,10 @@ class Image extends ImageModel
6266
* @var null
6367
*/
6468
private $fastlyUrl = null;
69+
/**
70+
* @var null
71+
*/
72+
private $isImageVerifyEnabled = null;
6573
/**
6674
* @var ScopeConfigInterface
6775
*/
@@ -81,6 +89,16 @@ class Image extends ImageModel
8189
*/
8290
private $miscParams;
8391

92+
/**
93+
* @var File
94+
*/
95+
private $file;
96+
97+
/**
98+
* @var Filesystem
99+
*/
100+
private $filesystem;
101+
84102
/**
85103
* Image constructor.
86104
* @param ConfigInterface $mediaConfig
@@ -89,7 +107,9 @@ class Image extends ImageModel
89107
* @param ScopeConfigInterface $scopeConfig
90108
* @param ImageHelper $imageHelper
91109
* @param StoreManagerInterface $storeManager
92-
* @param string $filePath
110+
* @param File $file
111+
* @param Filesystem $filesystem
112+
* @param $filePath
93113
* @param array $miscParams
94114
*/
95115
public function __construct(
@@ -99,18 +119,23 @@ public function __construct(
99119
ScopeConfigInterface $scopeConfig,
100120
ImageHelper $imageHelper,
101121
StoreManagerInterface $storeManager,
122+
File $file,
123+
Filesystem $filesystem,
102124
$filePath,
103125
array $miscParams
104126
) {
105127
$this->scopeConfig = $scopeConfig;
106128
$this->imageHelper = $imageHelper;
107129
$this->storeManager = $storeManager;
108130
$this->miscParams = $miscParams;
131+
$this->file = $file;
132+
$this->filesystem = $filesystem;
109133
parent::__construct($mediaConfig, $context, $encryptor, $filePath, $miscParams);
110134
}
111135

112136
/**
113137
* @return string|null
138+
* @throws FileSystemException
114139
* @throws NoSuchEntityException
115140
*/
116141
public function getUrl()
@@ -218,12 +243,23 @@ public function getBaseFileUrl($baseFile)
218243

219244
/**
220245
* @throws NoSuchEntityException
246+
* @throws FileSystemException
221247
*/
222248
public function getFastlyUrl()
223249
{
224250
$baseFile = $this->getSourceFile();
225251
$url = $this->getBaseFileUrl($baseFile);
226252

253+
if ($this->isImageVerifyEnabled()) {
254+
$mediaDir = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
255+
$imageAbsolutePath = $mediaDir->getAbsolutePath($baseFile);
256+
257+
if (!$this->file->fileExists($imageAbsolutePath)) {
258+
$imageHelper = \Magento\Framework\App\ObjectManager::getInstance()->get(ImageHelper::class);
259+
$url = $imageHelper->getDefaultPlaceholderUrl($this->miscParams['image_type']);
260+
}
261+
}
262+
227263
$imageQuality = $this->scopeConfig->getValue(Config::XML_FASTLY_IMAGE_OPTIMIZATION_IMAGE_QUALITY);
228264

229265
$this->fastlyParameters['quality'] = $imageQuality;
@@ -294,4 +330,22 @@ private function fastlyRotate($angle)
294330

295331
return $this;
296332
}
333+
334+
/**
335+
* @return bool|null
336+
*/
337+
private function isImageVerifyEnabled()
338+
{
339+
if ($this->isImageVerifyEnabled !== null) {
340+
return $this->isImageVerifyEnabled;
341+
}
342+
343+
$this->isImageVerifyEnabled = true;
344+
345+
if ($this->scopeConfig->isSetFlag(Config::XML_FASTLY_IMAGE_VERIFY) == false) {
346+
$this->isImageVerifyEnabled = false;
347+
}
348+
349+
return $this->isImageVerifyEnabled;
350+
}
297351
}

Diff for: etc/adminhtml/system.xml

+11
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,17 @@
573573
<source_model>Fastly\Cdn\Model\Config\Backend\PixelRatios</source_model>
574574
<validate>required_entry</validate>
575575
</field>
576+
<!-- =========================================
577+
Image Verify select field
578+
========================================== -->
579+
<field id="image_verify" translate="label comment" type="select" sortOrder="40" showInDefault="1" showInWebsite="0" showInStore="0">
580+
<label>Verify Image</label>
581+
<comment>Verify image actually exists on the disk. May cause performance issues especially on systems with shared filesystems.</comment>
582+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
583+
<depends>
584+
<field id="image_optimizations">1</field>
585+
</depends>
586+
</field>
576587
</group>
577588
<!-- ============================
578589
Maintenance Support group

Diff for: etc/config.xml

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<image_optimization_bg_color>1</image_optimization_bg_color>
5151
<image_optimization_canvas>1</image_optimization_canvas>
5252
<image_optimizations_ratios>2,3</image_optimizations_ratios>
53+
<image_verify>0</image_verify>
5354
</fastly_image_optimization_configuration>
5455
<fastly_web_hooks>
5556
<enable_webhooks>0</enable_webhooks>

0 commit comments

Comments
 (0)