Magento2 display the original GIF animated image in thumbnails without cropping or compression
When uploading a GIF product image in Magento 2, the animation is lost after cropping.
Modify through a plugin to ensure that GIF images return as the original images.
1. app/code/DMTQ/Catalog/etc/di.xml
<type name="Magento\Catalog\Model\View\Asset\Image">
<plugin name="return_original_image_url_for_gif" type="DMTQ\Catalog\Plugin\Catalog\Product\ReturnOriginalImageUrlForGif" sortOrder="10" />
</type>
2. app/code/DMTQ/Catalog/Plugin/Catalog/Product/ReturnOriginalImageUrlForGif.php
namespace DMTQ\Catalog\Plugin\Catalog\Product;
use Magento\Catalog\Model\View\Asset\Image;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;
/**
* Return Original Image Url For Gif
*/
class ReturnOriginalImageUrlForGif
{
/**
* @var StoreManagerInterface
*/
protected StoreManagerInterface $storeManager;
/**
* @param StoreManagerInterface $storeManager
*/
public function __construct(
StoreManagerInterface $storeManager,
)
{
$this->storeManager = $storeManager;
}
/**
* @throws NoSuchEntityException
*/
public function afterGetUrl(Image $subject, $result)
{
if (strtolower(substr($result, -4)) === '.gif') {
if ($subject->getSourceFile()) {
return $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) . $subject->getSourceFile();
}
}
return $result;
}
}
April 28, 2024
|
View: 102
|
Categories: <a class="mp-info" href="https://www.dmtq.com/blog/category/development.html">Development</a>
| Tags: <a class="mp-info" href="https://www.dmtq.com/blog/tag/magento.html">Magento</a>, <a class="mp-info" href="https://www.dmtq.com/blog/tag/image.html">Image</a>
|
By: <a class="mp-info" href="https://www.dmtq.com/blog/author.html">Admin</a>