Skip to content

Commit 94f9349

Browse files
authored
Merge pull request #12 from magefan/compatibility-with-webp
added compatibility with magefan webp
2 parents 2e78904 + 186229c commit 94f9349

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,26 @@
88

99
namespace Magefan\LazyLoad\Plugin\Amasty\PageSpeedOptimizer\Model\Output;
1010

11+
use Magefan\LazyLoad\Model\Config;
12+
1113
/**
1214
* Class LazyLoadProcessorPlugin
1315
* @package Magefan\LazyLoad\Plugin\Amasty\PageSpeedOptimizer\Model\Output
1416
*/
1517
class LazyLoadProcessorPlugin
1618
{
19+
/**
20+
* @var Config
21+
*/
22+
private $config;
23+
24+
public function __construct(
25+
Config $config
26+
27+
) {
28+
$this->config = $config;
29+
}
30+
1731
/**
1832
* @param $subject
1933
* @param callable $proceed
@@ -23,11 +37,17 @@ class LazyLoadProcessorPlugin
2337
*/
2438
public function aroundReplaceWithPictureTag($subject, callable $proceed, $image, $imagePath)
2539
{
40+
if (!$this->config->getEnabled()) {
41+
return $proceed($image, $imagePath);
42+
}
43+
2644
$originImagePath = $imagePath;
45+
2746
if (strpos($imagePath, 'Magefan_LazyLoad/images/pixel.jpg')) {
2847

2948
$doStr = 'data-original="';
3049
$p1 = strpos($image, $doStr);
50+
3151
if ($p1 !== false) {
3252
$p1 += strlen($doStr);
3353
$p2 = strpos($image, '"', $p1);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\LazyLoad\Plugin\Magefan\WebP\Model;
10+
11+
use Magefan\LazyLoad\Model\Config;
12+
13+
/**
14+
* Class WebPPlugin
15+
*/
16+
class WebPPlugin
17+
{
18+
19+
/**
20+
* @var Config
21+
*/
22+
private $config;
23+
24+
/**
25+
* WebPPlugin constructor.
26+
* @param Config $config
27+
*/
28+
public function __construct(
29+
Config $config
30+
31+
) {
32+
$this->config = $config;
33+
}
34+
35+
/**
36+
* @param $subject
37+
* @param callable $proceed
38+
* @param $imagePath
39+
* @param $htmlTag
40+
* @return mixed|string|string[]|null
41+
*/
42+
public function aroundGetPictureTagHtml($subject, callable $proceed, $imagePath, $image)
43+
{
44+
if (!$this->config->getEnabled()) {
45+
return $proceed($imagePath, $image);
46+
}
47+
48+
$originImagePath = $imagePath;
49+
50+
if (strpos($imagePath, 'Magefan_LazyLoad/images/pixel.jpg')) {
51+
52+
$doStr = 'data-original="';
53+
$p1 = strpos($image, $doStr);
54+
55+
if ($p1 !== false) {
56+
$p1 += strlen($doStr);
57+
$p2 = strpos($image, '"', $p1);
58+
if ($p2 !== false) {
59+
$imagePath = substr($image, $p1, $p2 - $p1);
60+
}
61+
}
62+
}
63+
64+
$html = $proceed($imagePath, $image);
65+
66+
if ($originImagePath != $imagePath) {
67+
68+
if (strpos($html, '<picture') !== false) {
69+
$tmpSrc = 'TMP_SRC';
70+
$pixelSrc = 'srcset="' . $originImagePath . '"';
71+
72+
$html = str_replace($pixelSrc, $tmpSrc, $html);
73+
74+
$html = preg_replace('#<source\s+([^>]*)(?:srcset="([^"]*)")([^>]*)?>#isU', '<source ' . $pixelSrc .
75+
' data-originalset="$2" $1 $3/>', $html);
76+
77+
$html = str_replace($tmpSrc, $pixelSrc, $html);
78+
}
79+
}
80+
81+
return $html;
82+
}
83+
}

etc/frontend/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@
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+
<!-- Integration with Magefan_WebP -->
25+
<type name="Magefan\WebP\Model\WebP">
26+
<plugin name="magefan_lazyLoad_plugin_for_webp"
27+
type="Magefan\LazyLoad\Plugin\Magefan\WebP\Model\WebPPlugin" sortOrder="10000"/>
28+
</type>
2429
</config>

0 commit comments

Comments
 (0)