FastDFS 之普通图片裁剪lua脚本
1.nginx配置
# attached img
server {
listen 8802;
server_name 10.1.8.54;
location / {
root /data/image/attached/b2b;
}
# 图片裁剪
location ~* ^(.+\.(jpg|jpeg|gif|png))_(\d+)x(\d+)\.(jpg|jpeg|gif|png)$ {
root /data/image/attached/b2b;
if (!-f $request_filename) {
add_header X-Powered-By 'Lua GraphicsMagick';
add_header file-path $request_filename;
#lua_code_cache off;
set $request_filepath /data/image/attached/b2b$1;
set $width $3;
set $height $4;
set $ext $5;
content_by_lua_file /usr/local/nginx/conf/lua/ImageResizer.lua;
}
}
# log file
access_log logs/attached_access.log access;
}
2.图片裁剪lua脚本
-- 生成指定尺寸
local area=ngx.var.width .. "x" .. ngx.var.height;
-- local image_sizes = {"100x100", "150x150", "200x200", "300x300", "400x400","600x600","800x800"};
local image_sizes = {"50x50","100x100", "150x150", "200x200", "300x300", "400x400","600x600","800x800"};
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
-- 生成缩略图
if table.contains(image_sizes, area) then
local command = "/usr/local/GraphicsMagick/bin/gm convert -quality 90 -auto-orient -strip " .. ngx.var.request_filepath .. " -resize " .. area .. " +profile \"*\" " .. ngx.var.request_filepath .. "_" .. area .. "." .. ngx.var.ext;
os.execute(command);
ngx.exec(ngx.var.request_uri);
else
ngx.exit(404)
end;

浙公网安备 33010602011771号