image_filter_jpeg_quality based on size

Antonio P.P. Almeida appa at perusio.net
Tue Mar 20 09:08:55 UTC 2012


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

Using set_by_lua (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



More information about the nginx mailing list