rss
twitter
    Find out what I'm doing, Follow Me :)

Friday, March 26, 2010

Pin It

Widgets

Increase the quality of photos on Magento

I was recently confronted with problems of quality photos on Magento.

At first, I vainly sought an option to resolve this with the Magento backend... I finally get to the obvious: this is not possible (at least until version 1.3).
I then studied the source code to determine how the framework manages and generates mainly the different sizes of pictures (on the page list, thumbnails, etc..) products.

I discovered that Magento uses GD2, with a quality setting to 80% by default (not changeable via configuration, back-office or XML). A value 80/100 quality is good in most cases. Nevertheless in e-commerce have known that a very good picture quality can make the difference.

The idea is to push the compression quality (jpeg) to 90%, the possible options are:

1. modify the PHP code found above: No, you should never directly edit the code from the core of Magento! (just as one shouldn’t cross the streams.)
2. create a module to administer the value of the compression quality via the back office: interesting, but reusable too long to achieve. I pass! :-)
3. override the code: easy, fast and clean: the solution I have chosen

Change the compression quality photos Magento:

1.
Step 1

Copy the file "/ lib/Varien/Image/Adapter/Gd2.php" to "/ app/code/local/Varien/Image/Adapter/Gd2.php" by creating the missing directories if necessary.
2.
Step 2

Open the file Gd2.php (copy, not original) at about line 80 and substitute:
call_user_func($this->_getCallback('output'), $this->_imageHandler, $fileName);

by:

if ($this->_fileType === IMAGETYPE_JPEG) {
call_user_func($this->_getCallback('output'), $this->_imageHandler, $fileName, 90);
} else {
call_user_func($this->_getCallback('output'), $this->_imageHandler, $fileName);
}

In the code above, I opted for 90, but you can change this value from 0 to 100 quality.
3.
Step 3

Finally, don’t forget to empty the cache of images via System> Cache Management.

That was simple, effective and reusable on any project, from the time you work with images in jpeg format (which format most common with digital photography) and your server supports GD2.

3 comments:

  1. I am using Magento 1.4.0.1 and it does not work for me. You have any idea?

    ReplyDelete
  2. 1.4.0.1 has different code in the Gd2.php file, can we still use this or do we even need to? If so, what do we change?

    // set quality param for JPG file type
    if (!is_null($this->quality()) && $this->_fileType == IMAGETYPE_JPEG)
    {
    $functionParameters[] = $this->quality();
    }

    Could we just change it to $this->quality(95)

    ReplyDelete
  3. It looks like the empty brackets - $this->quality(); - gives the same as 100. So - you can only degrade the image not improve it...

    bummer!

    ReplyDelete