|
| 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 | +} |
0 commit comments