Search code examples
.htaccessmagentogzippagespeed

Magento: compressing image not working effectively in .htaccess


I have the version 1.7.0.2 of magneto.I have enabled the gzip compression css htaccess js and css compression showing 100% on web page test org http://www.webpagetest.org/result/140812_2H_JQX/ but the problem is that image compression is only 38% i just want increase image compression.

I tried lots of code and extensions but not getting any improvement on image compression.

Any one can tell me how can i compress the image via .htaccess or using any extensions.

I am using bellow code in htaccess file

<IfModule mod_deflate.c>

############################################
## enable apache served files compression
## http://developer.yahoo.com/performance/rules.html#gzip
    # Insert filter on all content
    SetOutputFilter DEFLATE
    # Insert filter on selected content types only
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript

    BrowserMatch ^Mozilla/4 gzip-only-text/html

    # Netscape 4.06-4.08 have some more problems
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    # MSIE masquerades as Netscape, but it is fine
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html


    # Don't compress images
   SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    # Make sure proxies don't deliver the wrong content
    Header append Vary User-Agent env=!dont-vary

</IfModule>

Solution

  • Short answer is; You're not going to find a method to easily compress images properly using only .htaccess.

    If you want compression levels similar to the PageSpeed plugin you might want to put your attention to the following documentation: https://developers.google.com/speed/docs/best-practices/payload?hl=fr#CompressImages

    Method 1

    I'm going to take a guess and assume the plugins you tried to use either not doing what they advertise (it happens with Magento community plugins) or used a tool you either didn't have installed or needed to install, but this is the automatic method.

    In the linked article under "Use an image compressor" there are several tools outlined which can be used to compress images on linux environments. You could build a Magento plugin to automate that process. Short of that there isn't anything to do this easily.

    Method 2

    What you can do however is have the images which PageSpeed optimizes be saved in a configured directory. Now the link for that is broken but you can find that here: https://developers.google.com/speed/docs/insights/OptimizeImages

    After running PageSpeed you can then just upload the images to their respective directories.

    I would suggest the second method, as the first method isn't exactly easy and requires you to be able to control the live hosting environment to install plugins/tools you need.

    Also, don't bite down on PageSpeed scores too much, seeing that report makes me wonder how many plugins you installed on Magento that are now causing a lot of overhead. Note that any plugin slows down Magento a bit, and it's not fast in and on itself either, so other methods / caching are way more valuable than micro-managing image sizes. Time to first byte is easier to optimize than image compression.