# Image\_filter\_jpeg\_quality based on size

**URL:** <https://www.ruby-forum.com/t/image-filter-jpeg-quality-based-on-size/217955>\
**Category:** NGINX\
**Created:** [March 17, 2012, 5:02am UTC](https://www.ruby-forum.com/t/image-filter-jpeg-quality-based-on-size/217955 "2012-03-17T05:02:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [March 17, 2012, 5:02am UTC](https://www.ruby-forum.com/t/image-filter-jpeg-quality-based-on-size/217955/1 "2012-03-17T05:02:40Z")

</div>

Hi all,

In the --with-http\_image\_filter\_module:

Can I set image\_filter\_jpeg\_quality based on image size? Say I want to  
apply a loss of 75 to all images greater than 400KB. For the rest do  
nothing.

-Quintin

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [March 20, 2012, 8:10am UTC](https://www.ruby-forum.com/t/image-filter-jpeg-quality-based-on-size/217955/2 "2012-03-20T08:10:01Z")

</div>

Let me rephrase my Q 🙂

Can I find out the file size in a location directive?  
-Quintin

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [March 20, 2012, 10:09am UTC](https://www.ruby-forum.com/t/image-filter-jpeg-quality-based-on-size/217955/3 "2012-03-20T10:09:25Z")

</div>

> Let me rephrase my Q 🙂
> 
> Can I find out the file size in a location directive?  
> -Quintin

Using set\_by\_lua ([GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpLuaModule#set_by_lua)):

location ~\* .(?:gif|jpe?g|png)$ {  
set\_by\_lua $img\_file\_size ’  
function fsize (file)  
local current = file:seek() – get current position  
local size = file:seek(“end”) – get file size  
file:seek(“set”, current) – restore position  
return size  
end  
fsize(ngx.var.request\_filename)';  
}

Untested. This will give you the file size on the variable  
$img\_file\_size.

–appa

---

<div class="post-metadata">

**Author:** ![Quintin\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/13edae/32.png) [@Quintin\_P](https://www.ruby-forum.com/u/Quintin_P)\
**Post date:** [March 21, 2012, 3:41am UTC](https://www.ruby-forum.com/t/image-filter-jpeg-quality-based-on-size/217955/4 "2012-03-21T03:41:50Z")

</div>

Thanks Appa.
