Skip to content

Instantly share code, notes, and snippets.

@esterTion
Last active April 26, 2018 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esterTion/062d674ddcf7e0ab9f18cfa16afbfc55 to your computer and use it in GitHub Desktop.
Save esterTion/062d674ddcf7e0ab9f18cfa16afbfc55 to your computer and use it in GitHub Desktop.
Serves webp as png for Firefox and Safari
--[[
* @Author: esterTion
* requirement: nginx-lua-module, working ffmpeg
* explaination: webp decode and png encode speed should be quick enough to serve on the fly.
* use under {location ~ .+\.webp$}
* Currently deployed under https://redive.estertion.win/
]]
local ua=string.lower(ngx.var.http_user_agent)
local file=ngx.var.document_root..ngx.var.uri
if ( (string.match(ua, "safari") and not string.match(ua, 'chrome')) or string.match(ua, "firefox") ) and (os.rename(file, file) and true or false) then
ngx.header.content_type="image/png"
local p=io.popen("/usr/local/bin/ffmpeg -hide_banner -loglevel quiet -i \""..file.."\" -f image2 -c:v png -", "r")
local buf = p:read('*a')
p:close()
ngx.header.content_length = string.len(buf)
ngx.print(buf)
ngx.exit(200)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment