Last active
April 26, 2018 14:05
-
-
Save esterTion/062d674ddcf7e0ab9f18cfa16afbfc55 to your computer and use it in GitHub Desktop.
Serves webp as png for Firefox and Safari
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
* @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