如何避免在PHPresize的图像gzip?

我们不应该gzip图像,对不对? 如何避免像这样的图像img/sample.php?id=image_name.jpgimg/sample.php?id=image_name.jpg也可以这样调用img/sample.php?id=image_name.jpg&size=3实际的图像住在这里/images/items/

我尝试在httpd.conf中使用两种types的gzipconfiguration(请参见下文),但在这两种情况下,图像都是gzip。

图像显然不像普通的.jpg文件那样被处理,因为如果是这样的话,它不会被下面的任何configuration所压缩。 然而,实时标题将其显示为常规图像/ jpeg

任何想法如何解决这个问题?

第一次尝试:

 <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> 

第二次尝试:

  <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> 

这部分:

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

正在寻找请求URI中的图像文件扩展名。 您的文件扩展名位于查询string中

不幸的是, mod_setenvif没有访问查询string,如果我正确阅读文档, LocationMatch也没有。

但是, mod_rewrite可以访问查询string,并可以设置环境variables。

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

如果您使用的是mod_rewrite,那么使用它来重写URL可能是一个更好的主意,因此它们根本没有查询string。 以下使得上述两行不必要。

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

尝试包括这个:

 <LocationMatch \.jpg$> SetOutputFilter none </LocationMatch> 

我在.htaccess文件中使用这个很简单:

 <FilesMatch "\.(gif|jpg|png|zip|7z|rar|mp3|avi|swf)$"> SetEnv no-gzip 1 SetEnv dont-vary 1 </FilesMatch> 

对于使用脚本和查询string发送的图像文件,添加以下内容:

RewriteEngine on

RewriteCond %{QUERY_STRING} gif|jpg|png [NC]

RewriteRule ^ - [E=no-gzip,dont-vary]

注意:这些是区分大小写的,所以如果这个文件被命名为IMAGE.JPG,那么服务器仍然会介入并且将它们压缩。 如果您需要,请添加大写的扩展名。 我没有使用'$'作为你的查询string附加一个大小variables,所以你需要一个子stringsearch,而不是string的结尾。

另外,最好为处理图像文件(如“image.php”)的脚本select一个唯一的名称,或者在查询string中使用唯一的variables来捕获,而不必担心每个图像扩展名。

例如RewriteCond %{QUERY_STRING} op=download_image [NC]

RewriteRule ^ - [E=no-gzip,dont-vary]

例如说/sample.php?op=download_image&id=image_name.jpg&size=3

当然,使用请求string捕获“image.php”将会更短,但无论如何。