Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Model/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
*/
Expand Down
49 changes: 41 additions & 8 deletions Model/Transformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

class Transformation extends AbstractModel
{
/**
* @var string
*/
private $imageNameCacheKey;

private $configuration;

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
14 changes: 10 additions & 4 deletions Plugin/Catalog/Block/Product/ImageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
14 changes: 10 additions & 4 deletions Plugin/Catalog/Model/Product/Image/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 10 additions & 4 deletions Plugin/ImageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,11 @@
<field id="product_gallery_api_queue_enabled">1</field>
</depends>
</field>
<field id="enable_product_free_transformations" translate="label comment" type="select" sortOrder="8" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
<label>Enable Product Free Transformations On Store Front</label>
<comment>If not in use on your website, you may spare DB queries on the frontend by setting this to 'No'.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<product_gallery_api_queue_enabled>1</product_gallery_api_queue_enabled>
<product_gallery_api_queue_limit>20</product_gallery_api_queue_limit>
<product_gallery_api_queue_max_tryouts>5</product_gallery_api_queue_max_tryouts>
<enable_product_free_transformations>1</enable_product_free_transformations>
</advanced>
<product_gallery>
<enabled>0</enabled>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Cloudinary_Cloudinary" setup_version="1.14.10">
<module name="Cloudinary_Cloudinary" setup_version="1.14.11">
<sequence>
<module name="Magento_ProductVideo"/>
<module name="Magento_PageBuilder"/>
Expand Down
2 changes: 1 addition & 1 deletion marketplace.composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down