diff --git a/Model/Configuration.php b/Model/Configuration.php index 3dcc5347..b23b5101 100644 --- a/Model/Configuration.php +++ b/Model/Configuration.php @@ -61,6 +61,7 @@ class Configuration implements ConfigurationInterface const CONFIG_PATH_PG_API_QUEUE_ENABLED = 'cloudinary/advanced/product_gallery_api_queue_enabled'; const CONFIG_PATH_PG_API_QUEUE_LIMIT = 'cloudinary/advanced/product_gallery_api_queue_limit'; const CONFIG_PATH_PG_API_QUEUE_MAX_TRYOUTS = 'cloudinary/advanced/product_gallery_api_queue_max_tryouts'; + const CONFIG_PATH_ENABLE_PRODUCT_FREE_TRANSFORMATIONS = 'cloudinary/advanced/enable_product_free_transformations'; //= Product Gallery const CONFIG_PATH_PG_ALL = 'cloudinary/product_gallery'; @@ -483,6 +484,14 @@ public function isEnabledLocalMapping() return (bool) $this->configReader->getValue(self::CONFIG_PATH_ENABLE_LOCAL_MAPPING); } + /** + * @return bool + */ + public function isEnabledProductFreeTransformations() + { + return (bool) $this->configReader->getValue(self::CONFIG_PATH_ENABLE_PRODUCT_FREE_TRANSFORMATIONS); + } + /** * @return bool */ diff --git a/Model/Transformation.php b/Model/Transformation.php index 954f53cd..116e9692 100644 --- a/Model/Transformation.php +++ b/Model/Transformation.php @@ -13,6 +13,11 @@ class Transformation extends AbstractModel { + /** + * @var string + */ + private $imageNameCacheKey; + private $configuration; /** @@ -75,27 +80,34 @@ public function getFreeTransformation() } /** - * @param string $imageFile + * @param string $imageName * @return ImageTransformation */ - public function transformationForImage($imageFile) + public function transformationForImage($imageName) { return $this->addFreeformTransformationForImage( $this->configuration->getDefaultTransformation(), - $imageFile + $imageName ); } /** * @param ImageTransformation $transformation - * @param string $imageFile + * @param string $imageName + * @param bool $refresh * @return ImageTransformation */ - public function addFreeformTransformationForImage(ImageTransformation $transformation, $imageFile) + public function addFreeformTransformationForImage(ImageTransformation $transformation, $imageName, $refresh = false) { - $this->load($imageFile); - if (($this->getImageName() === $imageFile) && $this->hasFreeTransformation()) { - $transformation->withFreeform(Freeform::fromString($this->getFreeTransformation())); + $this->imageNameCacheKey = 'cldfreetransformcachekey_' . (string) $imageName; + if (!$refresh && ($cacheResult = $this->getFromCache()) !== null) { + $model = $cacheResult; + } else { + $model = $this->cacheResult($this->load($imageName)); + } + + if ($model->getImageName() === $imageName && $model->hasFreeTransformation()) { + $transformation->withFreeform(Freeform::fromString($model->getFreeTransformation())); } return $transformation; @@ -108,4 +120,25 @@ private function hasFreeTransformation() { return !empty($this->getFreeTransformation()); } + + /** + * @method cacheResult + * @param bool $result + * @return mixed + */ + private function cacheResult($result) + { + $this->_registry->unregister($this->imageNameCacheKey); + $this->_registry->register($this->imageNameCacheKey, $result); + return $result; + } + + /** + * @method cacheResult + * @return mixed + */ + private function getFromCache() + { + return $this->_registry->registry($this->imageNameCacheKey); + } } diff --git a/Plugin/Catalog/Block/Product/ImageFactory.php b/Plugin/Catalog/Block/Product/ImageFactory.php index 00d2464f..1feb1bbe 100644 --- a/Plugin/Catalog/Block/Product/ImageFactory.php +++ b/Plugin/Catalog/Block/Product/ImageFactory.php @@ -177,14 +177,20 @@ function () use ($imageBlock) { } ); - $transformations = $this->transformationModel->addFreeformTransformationForImage( - $this->createTransformation($imageMiscParams), - $imagePath - ); + $transformations = $this->createTransformation($imageMiscParams); + + if ($this->configuration->isEnabledProductFreeTransformations()) { + $transformations = $this->transformationModel->addFreeformTransformationForImage( + $transformations, + $imagePath + ); + } + $generatedImageUrl = $this->urlGenerator->generateFor( $image, $transformations ); + $imageBlock->setOriginalImageUrl($imageBlock->setImageUrl()); $imageBlock->setImageUrl($generatedImageUrl); diff --git a/Plugin/Catalog/Model/Product/Image/UrlBuilder.php b/Plugin/Catalog/Model/Product/Image/UrlBuilder.php index 1f921a15..2d38ab4c 100644 --- a/Plugin/Catalog/Model/Product/Image/UrlBuilder.php +++ b/Plugin/Catalog/Model/Product/Image/UrlBuilder.php @@ -147,12 +147,18 @@ function () use ($url) { } ); + $transformations = $this->createTransformation($imageMiscParams); + + if ($this->configuration->isEnabledProductFreeTransformations()) { + $transformations = $this->transformationModel->addFreeformTransformationForImage( + $transformations, + $imagePath + ); + } + $generatedImageUrl = $this->urlGenerator->generateFor( $image, - $this->transformationModel->addFreeformTransformationForImage( - $this->createTransformation($imageMiscParams), - $imagePath - ) + $transformations ); $url = $generatedImageUrl; diff --git a/Plugin/ImageHelper.php b/Plugin/ImageHelper.php index 65ab9787..83a94440 100644 --- a/Plugin/ImageHelper.php +++ b/Plugin/ImageHelper.php @@ -142,12 +142,18 @@ public function aroundGetUrl(CatalogImageHelper $helper, \Closure $originalMetho $image = $this->imageFactory->build(sprintf('catalog/product%s', $imagePath), $originalMethod); + $transformations = $this->createTransformation($helper); + + if ($this->configuration->isEnabledProductFreeTransformations()) { + $transformations = $this->transformationModel->addFreeformTransformationForImage( + $transformations, + $imagePath + ); + } + return $this->urlGenerator->generateFor( $image, - $this->transformationModel->addFreeformTransformationForImage( - $this->createTransformation($helper), - $imagePath - ) + $transformations ); } diff --git a/composer.json b/composer.json index f7f812e4..b39a7f8d 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "cloudinary/cloudinary-magento2", "description": "Cloudinary Magento 2 Integration.", "type": "magento2-module", - "version": "1.14.10", + "version": "1.14.11", "license": "MIT", "require": { "cloudinary/cloudinary_php": "^1.20.0" diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 29382759..291ec2f6 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -368,6 +368,11 @@ 1 + + + If not in use on your website, you may spare DB queries on the frontend by setting this to 'No'. + Magento\Config\Model\Config\Source\Yesno + diff --git a/etc/config.xml b/etc/config.xml index 67d7e0d5..2a6615ec 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -26,6 +26,7 @@ 1 20 5 + 1 0 diff --git a/etc/module.xml b/etc/module.xml index ce524575..b92e87a8 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - + diff --git a/marketplace.composer.json b/marketplace.composer.json index a5b25db7..7ff072fc 100644 --- a/marketplace.composer.json +++ b/marketplace.composer.json @@ -2,7 +2,7 @@ "name": "cloudinary/cloudinary", "description": "Cloudinary Magento 2 Integration.", "type": "magento2-module", - "version": "1.14.10", + "version": "1.14.11", "license": "MIT", "require": { "cloudinary/cloudinary_php": "^1.20.0"