How to avoid gzip on php resized images

apache-2.2gzip

We should not gzip images, right? How do I avoid gzipping on images like this: img/sample.php?id=image_name.jpg can also be called like this img/sample.php?id=image_name.jpg&size=3 the actual images live here /images/items/

I tried using 2 types of configuration for gzip in httpd.conf (see below), but in both cases the images were gzipped anyway.

Images are obviously not being treated like a regular .jpg file, because if it was it would not have been gzipped with any of the below configurations. However live headers shows it as a regular image/jpeg

Any idea how to fix this?

1st try:

<Location />
# Insert filter
SetOutputFilter DEFLATE

# Netscape 4.x has some problems...
<Location />
# Insert filter
SetOutputFilter DEFLATE

# Netscape 4.x has some problems...
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

# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !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
</Location>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

# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !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
</Location>

2nd try:

    <IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain    
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE text/xml    
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/json
</IfModule>

Best Answer

This part:

# Don't compress images
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary

Is looking for image file extensions in the request URI. Your file extensions are in the query string.

Unfortunately, mod_setenvif doesn't have access to the query string and if I am reading the documentation correctly, neither does LocationMatch.

mod_rewrite, however, does have access to the query string and can set environment variables.

RewriteCond %{QUERY_STRING} \.(?:gif|jpe?g|png)$
RewriteRule ^ - [E=no-gzip,dont-vary] 

If you're using mod_rewrite anyway, it might be a better idea to use it to rewrite the URLs so that they don't have a query string at all. The following makes the above two lines unnecessary.

RewriteRule /generated_images/(.*\.(?:gif|jpe?g|png))$ /sample.php?id=$1