0

I am trying to upload .gif image for few products , image is uploading from csv successfully

and animation also working in admin panel.

But issue here is when i am checking this product on frontend - image is displayed but Animation is not working on frontend

But unable to find any solution.

please help me out on this.

Thanks...............

0

Below is the code :

di.xml

<?xml version="1.0"?>    
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Model\Product\Image" type="ProductAjax\Animation\Model\Image" />
</config>

image.php - overrided function setBaseFile.

public function setBaseFile($file)
    {
        $this->_isBaseFilePlaceholder = false;

        if ($file && 0 !== strpos($file, '/', 0)) {
            $file = '/' . $file;
        }
        $baseDir = $this->_catalogProductMediaConfig->getBaseMediaPath();

        if ('/no_selection' == $file) {
            $file = null;
        }
        if ($file) {
            if (!$this->_fileExists($baseDir . $file) || !$this->_checkMemory($baseDir . $file)) {
                $file = null;
            }
        }
        if (!$file) {
            $this->_isBaseFilePlaceholder = true;
            // check if placeholder defined in config
            $isConfigPlaceholder = $this->_scopeConfig->getValue(
                "catalog/placeholder/{$this->getDestinationSubdir()}_placeholder",
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE
            );
            $configPlaceholder = '/placeholder/' . $isConfigPlaceholder;
            if (!empty($isConfigPlaceholder) && $this->_fileExists($baseDir . $configPlaceholder)) {
                $file = $configPlaceholder;
            } else {
                $this->_newFile = true;
                return $this;
            }
        }

        $baseFile = $baseDir . $file;

        if (!$file || !$this->_mediaDirectory->isFile($baseFile)) {
            throw new \Exception(__('We can\'t find the image file.'));
        }

        $this->_baseFile = $baseFile;
        $test_extension = substr($file,strpos($file, ".") + 1); 
            if($test_extension == "gif" || $test_extension == "GIF")
            {
                $path [] = $this->_catalogProductMediaConfig->getBaseMediaPath();
            }
            else
            {
                // build new filename (most important params)
                $path = [
                    $this->_catalogProductMediaConfig->getBaseMediaPath(),
                    'cache',
                    $this->getDestinationSubdir(),
                ];
                if (!empty($this->_width) || !empty($this->_height)) {
                    $path[] = "{$this->_width}x{$this->_height}";
                }

                // add misk params as a hash
                $miscParams = [
                    ($this->_keepAspectRatio ? '' : 'non') . 'proportional',
                    ($this->_keepFrame ? '' : 'no') . 'frame',
                    ($this->_keepTransparency ? '' : 'no') . 'transparency',
                    ($this->_constrainOnly ? 'do' : 'not') . 'constrainonly',
                    $this->_rgbToString($this->_backgroundColor),
                    'angle' . $this->_angle,
                    'quality' . $this->_quality,
                ];

                // if has watermark add watermark params to hash
                if ($this->getWatermarkFile()) {
                    $miscParams[] = $this->getWatermarkFile();
                    $miscParams[] = $this->getWatermarkImageOpacity();
                    $miscParams[] = $this->getWatermarkPosition();
                    $miscParams[] = $this->getWatermarkWidth();
                    $miscParams[] = $this->getWatermarkHeight();
                }

                $path[] = md5(implode('_', $miscParams));
            }
        // append prepared filename
        $this->_newFile = implode('/', $path) . $file;
        // the $file contains heading slash

        return $this;
    }

Reference Link

Hope it helps :)

  • thanks for comment , ... can you elaborate please i cant understand as module view – Chala Chalapathi Aug 7 at 9:36
  • removed that md5 encryption code and call image path directly. – Rk Rathod Aug 7 at 9:52
  • i have tried , as you said but total detail messedup................pls help me out on this please – Chala Chalapathi Aug 7 at 10:09
  • simple code this code for override function "setBaseFile()" in image.php file using preference and preference define in di.xml file – Rk Rathod Aug 7 at 10:19
  • please check above code what i did once , if any wrong please let me know – Chala Chalapathi Aug 7 at 10:40
0

I have done as below please let me know any changes

Step:1 --- ProductAjax/Animation/etc/module.xml

Step:2 --- ProductAjax/Animation/etc/di.xml

       <?xml version="1.0"?>    
         <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
       <preference for="Magento\Catalog\Model\Product\Image" type="ProductAjax\Animation\Model\Image" />
     </config>

Step:3 --- ProductAjax/Animation/Model/Product/Image.php

     <?php

   namespace ProductAjax\Animation\Model\Product;

   public function setBaseFile($file)
     {
    $this->_isBaseFilePlaceholder = false;

    if ($file && 0 !== strpos($file, '/', 0)) {
        $file = '/' . $file;
    }
    $baseDir = $this->_catalogProductMediaConfig->getBaseMediaPath();

    if ('/no_selection' == $file) {
        $file = null;
    }
    if ($file) {
        if (!$this->_fileExists($baseDir . $file) || !$this->_checkMemory($baseDir . $file)) {
            $file = null;
        }
    }
    if (!$file) {
        $this->_isBaseFilePlaceholder = true;
        // check if placeholder defined in config
        $isConfigPlaceholder = $this->_scopeConfig->getValue(
            "catalog/placeholder/{$this->getDestinationSubdir()}_placeholder",
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
        $configPlaceholder = '/placeholder/' . $isConfigPlaceholder;
        if (!empty($isConfigPlaceholder) && $this->_fileExists($baseDir . $configPlaceholder)) {
            $file = $configPlaceholder;
        } else {
            $this->_newFile = true;
            return $this;
        }
    }

    $baseFile = $baseDir . $file;

    if (!$file || !$this->_mediaDirectory->isFile($baseFile)) {
        throw new \Exception(__('We can\'t find the image file.'));
    }

    $this->_baseFile = $baseFile;
    $test_extension = substr($file,strpos($file, ".") + 1); 
        if($test_extension == "gif" || $test_extension == "GIF")
        {
            $path [] = $this->_catalogProductMediaConfig->getBaseMediaPath();
        }
        else
        {
            // build new filename (most important params)
            $path = [
                $this->_catalogProductMediaConfig->getBaseMediaPath(),
                'cache',
                $this->getDestinationSubdir(),
            ];
            if (!empty($this->_width) || !empty($this->_height)) {
                $path[] = "{$this->_width}x{$this->_height}";
            }

            // add misk params as a hash
            $miscParams = [
                ($this->_keepAspectRatio ? '' : 'non') . 'proportional',
                ($this->_keepFrame ? '' : 'no') . 'frame',
                ($this->_keepTransparency ? '' : 'no') . 'transparency',
                ($this->_constrainOnly ? 'do' : 'not') . 'constrainonly',
                $this->_rgbToString($this->_backgroundColor),
                'angle' . $this->_angle,
                'quality' . $this->_quality,
            ];

            // if has watermark add watermark params to hash
            if ($this->getWatermarkFile()) {
                $miscParams[] = $this->getWatermarkFile();
                $miscParams[] = $this->getWatermarkImageOpacity();
                $miscParams[] = $this->getWatermarkPosition();
                $miscParams[] = $this->getWatermarkWidth();
                $miscParams[] = $this->getWatermarkHeight();
            }

            $path[] = md5(implode('_', $miscParams));
        }
    // append prepared filename
    $this->_newFile = implode('/', $path) . $file;
    // the $file contains heading slash

    return $this;
}

please let me know any change i need to do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.