|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Fruitcake\CustomImageUrl\Helper; |
| 4 | + |
| 5 | +use Magento\Catalog\Model\Config\CatalogMediaConfig; |
| 6 | +use Fruitcake\CustomImageUrl\Model\Config\CustomConfig; |
| 7 | +use Magento\Catalog\Model\View\Asset\Image; |
| 8 | +use Magento\Framework\App\Helper\AbstractHelper; |
| 9 | +use Magento\Framework\App\Helper\Context; |
| 10 | +use Magento\Framework\Exception\LocalizedException; |
| 11 | +use Magento\Framework\UrlInterface; |
| 12 | +use Magento\Store\Model\ScopeInterface; |
| 13 | +use Magento\Store\Model\StoreManagerInterface; |
| 14 | + |
| 15 | + |
| 16 | +class Data extends AbstractHelper |
| 17 | +{ |
| 18 | + |
| 19 | + /** @var CatalogMediaConfig */ |
| 20 | + private $catalogMediaConfig; |
| 21 | + |
| 22 | + /** @var CustomConfig */ |
| 23 | + private $customConfig; |
| 24 | + |
| 25 | + /** @var StoreManagerInterface */ |
| 26 | + private $storeManager; |
| 27 | + |
| 28 | + public function __construct( |
| 29 | + Context $context, |
| 30 | + CatalogMediaConfig $catalogMediaConfig, |
| 31 | + CustomConfig $customConfig, |
| 32 | + StoreManagerInterface $storeManager |
| 33 | + ) { |
| 34 | + parent::__construct($context); |
| 35 | + |
| 36 | + $this->catalogMediaConfig = $catalogMediaConfig; |
| 37 | + $this->customConfig = $customConfig; |
| 38 | + $this->storeManager = $storeManager; |
| 39 | + } |
| 40 | + |
| 41 | + public function replaceImageUrlWithCustom(Image $image, string $imageUrl) |
| 42 | + { |
| 43 | + $customType = $this->getCustomUrlType(); |
| 44 | + |
| 45 | + // No action required, just use the default URL |
| 46 | + if ($customType === CustomConfig::TYPE_DEFAULT) { |
| 47 | + return $imageUrl; |
| 48 | + } |
| 49 | + |
| 50 | + $params = $image->getImageTransformationParameters(); |
| 51 | + |
| 52 | + if ($customType === CustomConfig::TYPE_PATTERN) { |
| 53 | + return $this->getCustomUrlFromPattern($imageUrl, $params); |
| 54 | + } |
| 55 | + |
| 56 | + if ($customType === CustomConfig::TYPE_IMGPROXY) { |
| 57 | + return $this->getImgProxyUrl($imageUrl, $params); |
| 58 | + } |
| 59 | + |
| 60 | + throw new LocalizedException( |
| 61 | + __("The specified Custom Catalog media URL type '$customType' is not supported.") |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public function getCustomUrlFromPattern(string $imageUrl, array $params): string |
| 66 | + { |
| 67 | + $customUrl = $this->customConfig->getCustomPattern(); |
| 68 | + |
| 69 | + $urlParts = parse_url($imageUrl); |
| 70 | + $params = $params + $urlParts; |
| 71 | + |
| 72 | + $params['path'] = ltrim($params['path'], '/'); |
| 73 | + $params['base_url'] = $this->getBaseUrl(); |
| 74 | + $params['base_url_media'] = $this->getMediaBaseUrl(); |
| 75 | + $params['image_url'] = explode('?', $imageUrl)[0]; |
| 76 | + |
| 77 | + foreach ($params as $k => $v) { |
| 78 | + $k = str_replace('-', '_', $k); |
| 79 | + if (strpos($customUrl, '{{'.$k) !== false) { |
| 80 | + $customUrl = str_replace('{{'.$k.'}}', $v, $customUrl); |
| 81 | + $customUrl = str_replace('{{'.$k.'|urlencode}}', urlencode($v), $customUrl); |
| 82 | + $customUrl = str_replace('{{'.$k.'|rawurlencode}}', rawurlencode($v), $customUrl); |
| 83 | + $customUrl = str_replace('{{'.$k.'|base64}}', base64_encode($v), $customUrl); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return $customUrl; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Based on https://github.com/imgproxy/imgproxy/blob/master/examples/signature.php |
| 92 | + * @param string $imageUrl |
| 93 | + * @param array $params |
| 94 | + * @return string |
| 95 | + */ |
| 96 | + public function getImgProxyUrl(string $imageUrl, array $params) |
| 97 | + { |
| 98 | + $resize = $this->customConfig->getImgproxyResize(); |
| 99 | + $width = $params['width']; |
| 100 | + $height = $params['height']; |
| 101 | + |
| 102 | + $urlParts = parse_url($imageUrl); |
| 103 | + $path = ltrim($urlParts['path'], '/'); |
| 104 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
| 105 | + |
| 106 | + if ($this->customConfig->getImgproxySourceType() === CustomConfig::IMGPROXY_S3) { |
| 107 | + $prefix = trim($this->customConfig->getImgproxySourcePrefix(), '/'); |
| 108 | + if (strlen($prefix) === 0) { |
| 109 | + throw new \RuntimeException('S3 Bucket cannot be empty'); |
| 110 | + } |
| 111 | + $sourceUrl = 's3://' . $prefix . '/' . $path; |
| 112 | + } elseif ($this->customConfig->getImgproxySourceType() === CustomConfig::IMGPROXY_LOCAL) { |
| 113 | + $prefix = trim($this->customConfig->getImgproxySourcePrefix(), '/'); |
| 114 | + if (strlen($prefix) > 0) { |
| 115 | + $path = $prefix . '/' . $path; |
| 116 | + } |
| 117 | + $sourceUrl = 'local:///' . $path; |
| 118 | + } else { |
| 119 | + $sourceUrl = $imageUrl; |
| 120 | + } |
| 121 | + |
| 122 | + $encodedUrl = rtrim(strtr(base64_encode($sourceUrl), '+/', '-_'), '='); |
| 123 | + $path = "/resize:{$resize}:$width:$height/{$encodedUrl}.{$extension}"; |
| 124 | + |
| 125 | + // Sign the URL |
| 126 | + $key = $this->customConfig->getImgproxyKey(); |
| 127 | + $salt = $this->customConfig->getImgproxySalt(); |
| 128 | + if (strlen($key) > 0 && strlen($salt) > 0) { |
| 129 | + $path = $this->signImgproxyPath($path, $key, $salt); |
| 130 | + } else { |
| 131 | + $path = '/insecure' . $path; |
| 132 | + } |
| 133 | + |
| 134 | + return rtrim($this->customConfig->getImgproxyHost(), '/') . $path; |
| 135 | + } |
| 136 | + |
| 137 | + private function signImgproxyPath(string $path, string $key, string $salt): string |
| 138 | + { |
| 139 | + $keyBin = pack("H*" , $key); |
| 140 | + if(empty($keyBin)) { |
| 141 | + throw new \RuntimeException('Key expected to be hex-encoded string'); |
| 142 | + } |
| 143 | + |
| 144 | + $saltBin = pack("H*" , $salt); |
| 145 | + if(empty($saltBin)) { |
| 146 | + throw new \RuntimeException('Salt expected to be hex-encoded string'); |
| 147 | + } |
| 148 | + |
| 149 | + $signature = rtrim(strtr(base64_encode(hash_hmac('sha256', $saltBin.$path, $keyBin, true)), '+/', '-_'), '='); |
| 150 | + |
| 151 | + return sprintf("/%s%s", $signature, $path); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @param null|int $storeId |
| 156 | + * |
| 157 | + * @return string|null |
| 158 | + */ |
| 159 | + private function getCustomUrlType($storeId = null) |
| 160 | + { |
| 161 | + // When Dynamic hashing is disabled, return false |
| 162 | + if ($this->catalogMediaConfig->getMediaUrlFormat(ScopeInterface::SCOPE_STORE, $storeId) !== CatalogMediaConfig::IMAGE_OPTIMIZATION_PARAMETERS) { |
| 163 | + return CustomConfig::TYPE_DEFAULT; |
| 164 | + } |
| 165 | + |
| 166 | + return $this->customConfig->getCustomType($storeId); |
| 167 | + } |
| 168 | + |
| 169 | + private function getBaseUrl($storeId = null) |
| 170 | + { |
| 171 | + return $this->storeManager->getStore($storeId)->getBaseUrl(UrlInterface::URL_TYPE_LINK); |
| 172 | + } |
| 173 | + |
| 174 | + private function getMediaBaseUrl($storeId = null) |
| 175 | + { |
| 176 | + return $this->storeManager->getStore($storeId)->getBaseUrl(UrlInterface::URL_TYPE_MEDIA); |
| 177 | + } |
| 178 | +} |
0 commit comments