Magento – Magento resizing images for cache and making them bigger

cacheimagemagento-1.9product-images

I've noticed on our Magento 1.9.3.1 store that when Magento puts product images into cache it's actually making them bigger. I'm not sure where to start debugging this or if there is a fix for it?

To give you an example I uploaded a image which was compressed to 92kb with PhotoShops Save for Web feature. However when I checked the frontend of the website I found that it had increased to 209kb.

The larger version of the file is coming from the Magento cache for product images, it's file path is:

media/catalog/product/cache/9/thumbnail/c96a280f94e22e3ee3823dd0a1a87606/i/m/img_4901-3v2.jpg

enter image description here

The original file still has the 92kb file size and is located in:

media/catalog/product/i/m/img_4901-3v2.jpg

enter image description here

Why & How could Magento be making the file bigger? Also why is it putting the cahed image in a thumbnail directory, all the cached versions of this image are being put in the thumbnail directory, i.e.

media/catalog/product/cache/9/thumbnail/200x/1ac472b2e3bed24f4b7f75082897d970/i/m/img_4901-3v3.jpg

media/catalog/product/cache/9/thumbnail/535x/1ac472b2e3bed24f4b7f75082897d970/i/m/img_4901-3v3.jpg

Where in the code does Magento process images for the cache?

I've looked in lib/tcpdf/tcpdf.php at $img->setCompressionQuality($this->jpeg_quality);. I've also checked template/catalog/product/view/media.phtml but couldn't find anything.

Best Answer

I found where Magento is saving the file, it actually re-saving it with a higher compression/image quality than was used in PS, thus the file is getting bigger. I can fix it but it would mean overriding a Magento core file.

This is the file responsible for increasing the image quality: app/code/core/Mage/Catalog/Model/Product/Image.php

class Mage_Catalog_Model_Product_Image extends Mage_Core_Model_Abstract
{
    protected $_width;
    protected $_height;
    protected $_quality = 90;

    /** CODE OMMITED **/

    /**
     * Set image quality, values in percentage from 0 to 100
     *
     * @param int $quality
     * @return Mage_Catalog_Model_Product_Image
     */
    public function setQuality($quality)
    {
        $this->_quality = $quality;
        return $this;
    }

    /** CODE OMMITED **/

    /** This function resizes the image**/
    /**
     * @return Varien_Image
     */
    public function getImageProcessor()
    {
        if( !$this->_processor ) {
//            var_dump($this->_checkMemory());
//            if (!$this->_checkMemory()) {
//                $this->_baseFile = null;
//            }
            $this->_processor = new Varien_Image($this->getBaseFile());
        }
        $this->_processor->keepAspectRatio($this->_keepAspectRatio);
        $this->_processor->keepFrame($this->_keepFrame);
        $this->_processor->keepTransparency($this->_keepTransparency);
        $this->_processor->constrainOnly($this->_constrainOnly);
        $this->_processor->backgroundColor($this->_backgroundColor);
        $this->_processor->quality($this->_quality);
        return $this->_processor;
    } 

At the beginning of the file the quality level is hardcoded to 90 and then applied in getImageProcessor(). By commenting out $this->_processor->quality($this->_quality); temporarily I can see that the issue goes away.

Why would Magento do this? This seems like it should be a common issue too. Is the best solution just to create a class override for this core Magento file?

Related Topic