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;
}
}


Copyright © 2013-present Magento, Inc. All rights reserved.