mirror of
https://github.com/yanue/nginx-lua-GraphicsMagick.git
synced 2025-11-26 23:35:33 +08:00
set config crop size
This commit is contained in:
156
lua/autoSize.lua
156
lua/autoSize.lua
@@ -1,93 +1,119 @@
|
||||
--
|
||||
-- 根据输入长或宽的尺寸自动裁切图片大小
|
||||
-- User: yanue
|
||||
-- Date: 04/10/2016
|
||||
-- Time: 14:42
|
||||
--
|
||||
|
||||
-- 检测路径是否目录
|
||||
local function is_dir(sPath)
|
||||
if type(sPath) ~= "string" then return false end
|
||||
-- 基础方法
|
||||
local base = require("base")
|
||||
|
||||
local response = os.execute("cd " .. sPath)
|
||||
if response == 0 then
|
||||
return true
|
||||
end
|
||||
return false
|
||||
-- 配置信息
|
||||
local cfg = require("config")
|
||||
|
||||
-- 检查缩略图目录是否存在
|
||||
if not base.is_dir(base.get_file_dir(ngx.var.img_file)) then
|
||||
os.execute("mkdir -p " .. base.get_file_dir(ngx.var.img_file))
|
||||
end
|
||||
|
||||
-- 文件是否存在
|
||||
function file_exists(name)
|
||||
local f = io.open(name, "r")
|
||||
if f ~= nil then io.close(f) return true else return false end
|
||||
end
|
||||
|
||||
-- 获取文件路径
|
||||
function getFileDir(filename)
|
||||
return string.match(filename, "(.+)/[^/]*%.%w+$") --*nix system
|
||||
end
|
||||
function get_img_width_height()
|
||||
-- 获取高宽 img_size 如: 100-或-100模式
|
||||
local uri = ngx.var.img_size
|
||||
local width = string.sub(uri, 1, 1)
|
||||
local height = 0
|
||||
|
||||
-- 获取文件名
|
||||
function strippath(filename)
|
||||
return string.match(filename, ".+/([^/]*%.%w+)$") -- *nix system
|
||||
end
|
||||
-- 宽为"-"时,高固定,取高的值
|
||||
if width == "-" then
|
||||
local prev_height = 0;
|
||||
local has_found = 0
|
||||
width = 0
|
||||
-- 截取高,如: -100
|
||||
height = string.sub(uri, 2, string.len(uri))
|
||||
|
||||
--去除扩展名
|
||||
function stripextension(filename)
|
||||
local idx = filename:match(".+()%.%w+$")
|
||||
if (idx) then
|
||||
return filename:sub(1, idx - 1)
|
||||
-- 正序排序
|
||||
table.sort(cfg.allow_auto_height_sizes);
|
||||
|
||||
-- 查找匹配规格列表
|
||||
for _, value in pairs(all_list) do
|
||||
if (height <= value and height > prev_height) then
|
||||
height = value
|
||||
has_found = 1
|
||||
break
|
||||
end
|
||||
prev_height = value
|
||||
end
|
||||
|
||||
-- 如果不在列表
|
||||
if (has_found == 0) then
|
||||
height = cfg.allow_max_height
|
||||
end
|
||||
else
|
||||
return filename
|
||||
-- 高为"-"时,宽固定,取宽的值
|
||||
local prev_width = 0;
|
||||
|
||||
-- 截取宽,如: 100-
|
||||
width = string.sub(uri, 1, string.len(uri) - 1)
|
||||
height = 0
|
||||
|
||||
-- 正序排序
|
||||
table.sort(cfg.allow_auto_width_sizes);
|
||||
|
||||
-- 查找匹配规格列表
|
||||
for _, value in pairs(cfg.allow_auto_width_sizes) do
|
||||
if (width <= value and width > prev_width) then
|
||||
width = value
|
||||
break
|
||||
end
|
||||
prev_width = value
|
||||
end
|
||||
|
||||
-- 如果不在列表
|
||||
if (has_found == 0) then
|
||||
width = cfg.allow_max_width
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
width:width,
|
||||
height:width,
|
||||
}
|
||||
end
|
||||
|
||||
--获取扩展名
|
||||
function getExtension(filename)
|
||||
return filename:match(".+%.(%w+)$")
|
||||
end
|
||||
|
||||
function getImgSize(img)
|
||||
|
||||
end
|
||||
|
||||
-- 开始执行
|
||||
-- ngx.log(ngx.ERR, getFileDir(ngx.var.img_file));
|
||||
|
||||
local gm_path = 'gm'
|
||||
|
||||
-- check image dir
|
||||
if not is_dir(getFileDir(ngx.var.img_file)) then
|
||||
os.execute("mkdir -p " .. getFileDir(ngx.var.img_file))
|
||||
end
|
||||
|
||||
-- 获取高宽 100!或!100模式
|
||||
local uri = ngx.var.img_size
|
||||
local width = string.sub(uri,1,1)
|
||||
local width = 0
|
||||
local height = 0
|
||||
|
||||
if width == "-" then
|
||||
width = 0
|
||||
height = string.sub(uri,2,string.len(uri))
|
||||
else
|
||||
width = string.sub(uri,1,string.len(uri)-1)
|
||||
height = 0
|
||||
end
|
||||
-- ngx.log(ngx.ERR,uri)
|
||||
-- ngx.log(ngx.ERR,width)
|
||||
-- ngx.log(ngx.ERR,height)
|
||||
-- ngx.log(ngx.ERR,img_size)
|
||||
-- ngx.log(ngx.ERR,ngx.var.img_file);
|
||||
-- ngx.log(ngx.ERR,ngx.var.request_filepath);
|
||||
-- 裁剪后保证等比缩图 (缺点:裁剪了图片的一部分)
|
||||
-- 如: gm convert autoSize.jpg -resize x200 -quality 100 +profile "*" autoSize.jpg_-200.jpg
|
||||
if (file_exists(ngx.var.request_filepath)) then
|
||||
local cmd = gm_path .. ' convert ' .. ngx.var.request_filepath
|
||||
|
||||
--[[
|
||||
裁剪后保证等比缩图
|
||||
转换图片命令如: gm convert autoSize.jpg -resize x200 -quality 100 +profile "*" autoSize.jpg_-200.jpg
|
||||
]]
|
||||
if (base.file_exists(ngx.var.request_filepath)) then
|
||||
-- 拼接 gm 命令
|
||||
local cmd = cfg.gm_path .. ' convert ' .. ngx.var.request_filepath
|
||||
if height == 0 then
|
||||
cmd = cmd .. " -resize " .. width .. "x" .. ""
|
||||
cmd = cmd .. " -resize " .. width .. "x" .. ""
|
||||
else
|
||||
cmd = cmd .. " -resize " .. "x" .. height .. ""
|
||||
end
|
||||
|
||||
-- 由于压缩后比较模糊,默认图片质量为100,请根据自己情况修改quality
|
||||
-- 由于压缩后比较模糊,默认图片质量为100,请根据自己情况修改quality
|
||||
cmd = cmd .. " -quality 100"
|
||||
|
||||
-- 不存储exif信息,以减小图片体积
|
||||
cmd = cmd .. " +profile \"*\" " .. ngx.var.img_file;
|
||||
ngx.log(ngx.ERR, cmd);
|
||||
|
||||
-- 打印gm转换命令
|
||||
base.lua_log(cmd, ngx.ERR);
|
||||
|
||||
-- 执行转换
|
||||
os.execute(cmd);
|
||||
|
||||
-- 重新渲染 nginx 地址
|
||||
ngx.exec(ngx.var.uri);
|
||||
else
|
||||
ngx.exit(ngx.HTTP_NOT_FOUND);
|
||||
|
||||
72
lua/base.lua
Normal file
72
lua/base.lua
Normal file
@@ -0,0 +1,72 @@
|
||||
-- 基础方法模块
|
||||
-- User: yanue
|
||||
-- Date: 04/10/2016
|
||||
-- Time: 14:42
|
||||
--
|
||||
|
||||
-- 引用配置
|
||||
local c = require 'config'
|
||||
|
||||
-- 定义 base 模块
|
||||
base = {}
|
||||
|
||||
-- 检测路径是否目录
|
||||
function base.is_dir(sPath)
|
||||
if type(sPath) ~= "string" then return false end
|
||||
|
||||
local response = os.execute("cd " .. sPath)
|
||||
if response == 0 then
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- 文件是否存在
|
||||
function base.file_exists(name)
|
||||
local f = io.open(name, "r")
|
||||
if f ~= nil then io.close(f) return true else return false end
|
||||
end
|
||||
|
||||
-- 获取文件路径
|
||||
function base.get_file_dir(filename)
|
||||
return string.match(filename, "(.+)/[^/]*%.%w+$") --*nix system
|
||||
end
|
||||
|
||||
-- 获取文件名
|
||||
function base.strip_path(filename)
|
||||
return string.match(filename, ".+/([^/]*%.%w+)$") -- *nix system
|
||||
end
|
||||
|
||||
-- 去除扩展名
|
||||
function base.strip_extension(filename)
|
||||
local idx = filename:match(".+()%.%w+$")
|
||||
if (idx) then
|
||||
return filename:sub(1, idx - 1)
|
||||
else
|
||||
return filename
|
||||
end
|
||||
end
|
||||
|
||||
-- 获取扩展名
|
||||
function base.get_extension(filename)
|
||||
return filename:match(".+%.(%w+)$")
|
||||
end
|
||||
|
||||
-- 获取图片尺寸
|
||||
function base.get_img_size(img)
|
||||
end
|
||||
|
||||
--[[
|
||||
打印 nginx 日志
|
||||
log_level: 默认为ngx.NOTICE
|
||||
取值范围:ngx.STDERR , ngx.EMERG , ngx.ALERT , ngx.CRIT , ngx.ERR , ngx.WARN , ngx.NOTICE , ngx.INFO , ngx.DEBUG
|
||||
请配合nginx.conf中error_log的日志级别使用
|
||||
]]
|
||||
function lua_log(msg, log_level)
|
||||
log_level = log_level or c.lua_log_level
|
||||
if (c.enabled_log) then
|
||||
ngx.log(log_level, msg)
|
||||
end
|
||||
end
|
||||
|
||||
return base
|
||||
106
lua/config.lua
Normal file
106
lua/config.lua
Normal file
@@ -0,0 +1,106 @@
|
||||
-- 基础配置
|
||||
-- User: yanue
|
||||
-- Date: 04/10/2016
|
||||
-- Time: 14:42
|
||||
|
||||
module(..., package.seeall)
|
||||
|
||||
--[[
|
||||
enabled_log: 是否打开日志
|
||||
lua_log_level: 日志记录级别
|
||||
gm_path: graphicsmagick安装目录
|
||||
img_background_color: 填充背景色
|
||||
enabled_default_img: 是否显示默认图片
|
||||
default_img_uri: 默认图片链接
|
||||
default_uri_reg: 缩略图正则匹配模式,可自定义
|
||||
_[0-9]+x[0-9] 对应:001_100x100.jpg
|
||||
_[0-9]+x[0-9]+[.jpg|.png|.gif]+ 对应:001.jpg_100x100.jpg
|
||||
]]
|
||||
|
||||
enabled_log = true
|
||||
lua_log_level = ngx.NOTICE
|
||||
|
||||
-- gm命令路径
|
||||
gm_path = 'gm'
|
||||
crop_mode = ''
|
||||
|
||||
allow_max_width = 1980
|
||||
allow_max_height = 1980
|
||||
|
||||
--[[
|
||||
固定宽高模式[如: /xx/xx/xx.jpg_100x100.jpg]
|
||||
允许的宽度列表,请根据自己情况修改(填写数字)
|
||||
]]
|
||||
all_crop_area_sizes = {
|
||||
30,
|
||||
50,
|
||||
80,
|
||||
100,
|
||||
120,
|
||||
160,
|
||||
200,
|
||||
240,
|
||||
300,
|
||||
360,
|
||||
400,
|
||||
500,
|
||||
600,
|
||||
640,
|
||||
800,
|
||||
800,
|
||||
900,
|
||||
1000,
|
||||
1200
|
||||
}
|
||||
|
||||
--[[
|
||||
宽固定模式[如: /xx/xx/xx.jpg_100-.jpg]
|
||||
允许的宽度列表,请根据自己情况修改(填写数字)
|
||||
]]
|
||||
allow_auto_height_sizes = {
|
||||
30,
|
||||
50,
|
||||
80,
|
||||
100,
|
||||
120,
|
||||
160,
|
||||
200,
|
||||
240,
|
||||
300,
|
||||
360,
|
||||
400,
|
||||
500,
|
||||
600,
|
||||
640,
|
||||
800,
|
||||
800,
|
||||
900,
|
||||
1000,
|
||||
1200
|
||||
}
|
||||
|
||||
--[[
|
||||
高固定模式[如: /xx/xx/xx.jpg_-100.jpg]
|
||||
允许的宽度列表,请根据自己情况修改(填写数字)
|
||||
]]
|
||||
allow_auto_width_sizes = {
|
||||
30,
|
||||
50,
|
||||
80,
|
||||
100,
|
||||
120,
|
||||
160,
|
||||
200,
|
||||
240,
|
||||
300,
|
||||
360,
|
||||
400,
|
||||
500,
|
||||
600,
|
||||
640,
|
||||
800,
|
||||
800,
|
||||
900,
|
||||
1000
|
||||
}
|
||||
|
||||
115
lua/cropSize.lua
115
lua/cropSize.lua
@@ -1,73 +1,80 @@
|
||||
-- 根据输入长和宽的尺寸裁切图片
|
||||
-- User: yanue
|
||||
-- Date: 04/10/2016
|
||||
-- Time: 14:42
|
||||
|
||||
-- 检测路径是否目录
|
||||
local function is_dir(sPath)
|
||||
if type(sPath) ~= "string" then return false end
|
||||
-- 基础方法
|
||||
local base = require("base")
|
||||
|
||||
local response = os.execute("cd " .. sPath)
|
||||
if response == 0 then
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- 文件是否存在
|
||||
function file_exists(name)
|
||||
local f = io.open(name, "r")
|
||||
if f ~= nil then io.close(f) return true else return false end
|
||||
end
|
||||
|
||||
-- 获取文件路径
|
||||
function getFileDir(filename)
|
||||
return string.match(filename, "(.+)/[^/]*%.%w+$") --*nix system
|
||||
end
|
||||
|
||||
-- 获取文件名
|
||||
function strippath(filename)
|
||||
return string.match(filename, ".+/([^/]*%.%w+)$") -- *nix system
|
||||
end
|
||||
|
||||
--去除扩展名
|
||||
function stripextension(filename)
|
||||
local idx = filename:match(".+()%.%w+$")
|
||||
if (idx) then
|
||||
return filename:sub(1, idx - 1)
|
||||
else
|
||||
return filename
|
||||
end
|
||||
end
|
||||
|
||||
--获取扩展名
|
||||
function getExtension(filename)
|
||||
return filename:match(".+%.(%w+)$")
|
||||
end
|
||||
|
||||
-- 开始执行
|
||||
-- ngx.log(ngx.ERR, getFileDir(ngx.var.img_file));
|
||||
|
||||
local gm_path = 'gm'
|
||||
-- 配置信息
|
||||
local cfg = require("config")
|
||||
|
||||
-- check image dir
|
||||
if not is_dir(getFileDir(ngx.var.img_file)) then
|
||||
os.execute("mkdir -p " .. getFileDir(ngx.var.img_file))
|
||||
if not base.is_dir(base.get_file_dir(ngx.var.img_file)) then
|
||||
os.execute("mkdir -p " .. base.get_file_dir(ngx.var.img_file))
|
||||
end
|
||||
|
||||
-- ngx.log(ngx.ERR,ngx.var.img_file);
|
||||
-- ngx.log(ngx.ERR,ngx.var.request_filepath);
|
||||
function get_img_width_height()
|
||||
-- 获取高宽 img_size 如: 100-或-100模式
|
||||
local width = ngx.var.img_width
|
||||
local height = ngx.var.img_height
|
||||
local has_found = 0
|
||||
local prev_size = cfg.allow_max_width
|
||||
local list = cfg.all_crop_area_sizes
|
||||
-- 倒序排序
|
||||
table.sort(list, function(a, b) return a[1] > b[1] end);
|
||||
-- 查找匹配规格列表
|
||||
-- todo
|
||||
for _, value in pairs(all_list) do
|
||||
if ((height <= value and height > prev_size) or (width <= value and width > prev_size)) then
|
||||
height = value
|
||||
has_found = 1
|
||||
break
|
||||
end
|
||||
prev_size = value
|
||||
end
|
||||
|
||||
-- 裁剪后保证等比缩图 (缺点:裁剪了图片的一部分)
|
||||
-- gm convert cropSize.jpg -thumbnail 300x300^ -gravity center -extent 300x300 -quality 100 +profile "*" cropSize.jpg_300x300.jpg
|
||||
if (file_exists(ngx.var.request_filepath)) then
|
||||
local cmd = gm_path .. ' convert ' .. ngx.var.request_filepath
|
||||
if (has_found == 0) then
|
||||
|
||||
end
|
||||
|
||||
return {
|
||||
width:width,
|
||||
height:width,
|
||||
}
|
||||
end
|
||||
|
||||
-- ngx.log(ngx.ERR,uri)
|
||||
-- ngx.log(ngx.ERR,width)
|
||||
-- ngx.log(ngx.ERR,height)
|
||||
-- ngx.log(ngx.ERR,ngx.var.img_file);
|
||||
-- ngx.log(ngx.ERR,ngx.var.request_filepath);
|
||||
|
||||
--[[
|
||||
裁剪后保证等比缩图 (缺点:裁剪了图片的一部分)
|
||||
转换图片命令如: gm convert cropSize.jpg -thumbnail 300x300^ -gravity center -extent 300x300 -quality 100 +profile "*" cropSize.jpg_300x300.jpg
|
||||
]]
|
||||
if (base.file_exists(ngx.var.request_filepath)) then
|
||||
-- 拼接 gm 命令
|
||||
local cmd = cfg.gm_path .. ' convert ' .. ngx.var.request_filepath
|
||||
cmd = cmd .. " -thumbnail " .. ngx.var.img_width .. "x" .. ngx.var.img_height .. "^"
|
||||
cmd = cmd .. " -gravity center -extent " .. ngx.var.img_width .. "x" .. ngx.var.img_height
|
||||
|
||||
-- 由于压缩后比较模糊,默认图片质量为100,请根据自己情况修改quality
|
||||
cmd = cmd .. " -quality 100"
|
||||
|
||||
-- 不存储exif信息,以减小图片体积
|
||||
cmd = cmd .. " +profile \"*\" " .. ngx.var.img_file;
|
||||
-- ngx.log(ngx.ERR, cmd);
|
||||
|
||||
-- 打印gm转换命令
|
||||
base.lua_log(cmd, ngx.ERR);
|
||||
|
||||
-- 执行转换
|
||||
os.execute(cmd);
|
||||
|
||||
-- 重新渲染 nginx 地址
|
||||
ngx.exec(ngx.var.uri);
|
||||
else
|
||||
-- 404
|
||||
ngx.exit(ngx.HTTP_NOT_FOUND);
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user