Skip to content

Commit 24b1255

Browse files
authored
Merge pull request #26 from magefan/6822-added-compatibility-for-the-latest-version-of-amasty-image-optimizer
6822 added compatibility for the latest version amasty image optimizer
2 parents 86e7e5e + b4b34e2 commit 24b1255

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Plugin/Amasty/PageSpeedOptimizer/Model/Output/LazyLoadProcessorPlugin.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,55 @@ public function aroundReplaceWithPictureTag($subject, callable $proceed, $image,
7979

8080
return $html;
8181
}
82+
83+
/**
84+
* @param $subject
85+
* @param callable $proceed
86+
* @param algorithm
87+
* @param $image
88+
* @param $imagePath
89+
* @return mixed|null|string|string[]
90+
*/
91+
public function aroundReplace($subject, callable $proceed, $algorithm, $image, $imagePath)
92+
{
93+
94+
if (!$this->config->getEnabled()) {
95+
return $proceed($algorithm, $image, $imagePath);
96+
}
97+
98+
$originImagePath = $imagePath;
99+
100+
if (strpos($imagePath, 'Magefan_LazyLoad/images/pixel.jpg')) {
101+
102+
$doStr = 'data-original="';
103+
$p1 = strpos($image, $doStr);
104+
105+
if ($p1 !== false) {
106+
$p1 += strlen($doStr);
107+
$p2 = strpos($image, '"', $p1);
108+
if ($p2 !== false) {
109+
$imagePath = substr($image, $p1, $p2 - $p1);
110+
}
111+
}
112+
}
113+
114+
$html = $proceed($algorithm, $image, $imagePath);
115+
116+
if ($originImagePath != $imagePath) {
117+
118+
if (strpos($html, '<picture') !== false) {
119+
$tmpSrc = 'TMP_SRC';
120+
$pixelSrc = 'srcset="' . $originImagePath . '"';
121+
122+
$html = str_replace($pixelSrc, $tmpSrc, $html);
123+
124+
$html = preg_replace('#<source\s+([^>]*)(?:srcset="([^"]*)")([^>]*)?>#isU', '<source ' . $pixelSrc .
125+
' data-originalset="$2" $1 $3/>', $html);
126+
127+
$html = str_replace($tmpSrc, $pixelSrc, $html);
128+
}
129+
}
130+
131+
return $html;
132+
}
82133
}

etc/frontend/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
<plugin name="magefan_lazyLoad_plugin_amasty_pagespeedoptimizer_model_output_lazyloadprocessor_plugin"
2222
type="Magefan\LazyLoad\Plugin\Amasty\PageSpeedOptimizer\Model\Output\LazyLoadProcessorPlugin" sortOrder="1000"/>
2323
</type>
24+
25+
<type name="Amasty\PageSpeedTools\Model\Image\ReplacerComposite">
26+
<plugin name="magefan_lazyLoad_plugin_amasty_imagege_replacer_composite_plugin"
27+
type="Magefan\LazyLoad\Plugin\Amasty\PageSpeedOptimizer\Model\Output\LazyLoadProcessorPlugin" sortOrder="10000"/>
28+
</type>
29+
30+
2431
<!-- Integration with Magefan_WebP -->
2532
<type name="Magefan\WebP\Model\WebP">
2633
<plugin name="magefan_lazyLoad_plugin_for_webp"

0 commit comments

Comments
 (0)