Skip to content

Commit 5fe7510

Browse files
author
Alexander Tolochko
committed
Merge branch 'develop'
2 parents 80047b4 + b9cc34c commit 5fe7510

File tree

4 files changed

+132
-153
lines changed

4 files changed

+132
-153
lines changed

App/MediaRewrite.php

Lines changed: 0 additions & 152 deletions
This file was deleted.

Plugin/App/MediaPlugin.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace MagestyApps\WebImages\Plugin\App;
4+
5+
use Magento\Catalog\Model\Product\Media\ConfigInterface as MediaConfig;
6+
use Magento\Framework\App\Filesystem\DirectoryList;
7+
use Magento\Framework\App\Request\Http;
8+
use Magento\Framework\Filesystem;
9+
use Magento\Framework\Filesystem\Directory\WriteInterface;
10+
use Magento\MediaStorage\App\Media;
11+
use MagestyApps\WebImages\Helper\ImageHelper;
12+
use Psr\Log\LoggerInterface;
13+
14+
class MediaPlugin
15+
{
16+
/**
17+
* @var ImageHelper
18+
*/
19+
private $imageHelper;
20+
21+
/**
22+
* @var MediaConfig
23+
*/
24+
private $imageConfig;
25+
26+
/**
27+
* @var Http
28+
*/
29+
private $request;
30+
31+
/**
32+
* @var LoggerInterface
33+
*/
34+
private $logger;
35+
36+
/**
37+
* @var WriteInterface
38+
*/
39+
private $directoryPub;
40+
41+
/**
42+
* @var WriteInterface
43+
*/
44+
private $directoryMedia;
45+
46+
/**
47+
* MediaPlugin constructor.
48+
* @param ImageHelper $imageHelper
49+
* @param Filesystem $filesystem
50+
* @param MediaConfig $imageConfig
51+
* @param Http $request
52+
* @param LoggerInterface $logger
53+
* @throws \Magento\Framework\Exception\FileSystemException
54+
*/
55+
public function __construct(
56+
ImageHelper $imageHelper,
57+
Filesystem $filesystem,
58+
MediaConfig $imageConfig,
59+
Http $request,
60+
LoggerInterface $logger
61+
) {
62+
$this->imageHelper = $imageHelper;
63+
$this->imageConfig = $imageConfig;
64+
$this->request = $request;
65+
$this->logger = $logger;
66+
67+
$this->directoryPub = $filesystem->getDirectoryWrite(
68+
DirectoryList::PUB,
69+
Filesystem\DriverPool::FILE
70+
);
71+
$this->directoryMedia = $filesystem->getDirectoryWrite(
72+
DirectoryList::MEDIA,
73+
Filesystem\DriverPool::FILE
74+
);
75+
}
76+
77+
/**
78+
* When trying to process a vector image, just copy it to the cache folder instead of resizing
79+
*
80+
* @param Media $subject
81+
* @return array
82+
*/
83+
public function beforeLaunch(Media $subject)
84+
{
85+
try {
86+
$relativeFileName = $this->getRelativeFileName();
87+
88+
if ($this->imageHelper->isVectorImage($relativeFileName)) {
89+
$originalImage = $this->getOriginalImage($relativeFileName);
90+
$originalImagePath = $this->directoryMedia->getAbsolutePath(
91+
$this->imageConfig->getMediaPath($originalImage)
92+
);
93+
94+
$this->directoryMedia->copyFile(
95+
$originalImagePath,
96+
$this->directoryPub->getAbsolutePath($relativeFileName)
97+
);
98+
}
99+
} catch (\Exception $e) {
100+
$this->logger->error('Could not process vector image', [
101+
'message' => $e->getMessage()
102+
]);
103+
}
104+
105+
return [];
106+
}
107+
108+
/**
109+
* Get relative file name
110+
*
111+
* @return string
112+
*/
113+
private function getRelativeFileName()
114+
{
115+
return str_replace('..', '', ltrim($this->request->getPathInfo(), '/'));
116+
}
117+
118+
/**
119+
* Find the path to the original image of the cache path
120+
*
121+
* @param string $resizedImagePath
122+
* @return string
123+
*/
124+
private function getOriginalImage(string $resizedImagePath): string
125+
{
126+
return preg_replace('|^.*?((?:/([^/])/([^/])/\2\3)?/?[^/]+$)|', '$1', $resizedImagePath);
127+
}
128+
}

etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<extensions>
1212
<vector>
1313
<svg>svg</svg>
14+
<svg_xml>svg+xml</svg_xml>
1415
</vector>
1516
<web_image>
1617
<webp>webp</webp>

etc/di.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
</arguments>
3737
</virtualType>
3838

39-
<preference for="Magento\MediaStorage\App\Media" type="MagestyApps\WebImages\App\MediaRewrite" />
39+
<type name="Magento\MediaStorage\App\Media">
40+
<plugin name="web-images-media-plugin" type="MagestyApps\WebImages\Plugin\App\MediaPlugin" />
41+
</type>
4042
<type name="Magento\MediaStorage\Model\File\Uploader">
4143
<plugin name="allow-web-images" type="MagestyApps\WebImages\Plugin\File\UploaderPlugin" />
4244
</type>

0 commit comments

Comments
 (0)