图片水印
2016-04-07 16:29
603 查看
Nginx
Lua
几年前之前给我们网站 安团 和 智企ID 做的水印是这么做的,后来有了更好的方法所以摈弃了,故而拿出来交流交流。
location ~* /([0-9a-z\/]+)/(.*).(jpg|jpeg|png|bmp|webp|JPG)_sy(\.|\_)(.*)$ { lua_code_cache off; default_type text/html; root /filesystem/; set $image_root /filesystem/; set $new_img_path /filesystem/sy/; set $ways $1; set $fileName $2.$3; set $file $image_root$uri; set $sy $4; set $other $5; if (!-f $file) { rewrite_by_lua_file /file_lua/sy.lua; } }
Lua
local img_path = ngx.var.image_root .. ngx.var.ways .. "/" .. ngx.var.fileName local new_img_path = ngx.var.new_img_path .. ngx.var.ways .. "/" .. ngx.var.fileName local thumbnail_path = ngx.var.file local img_dir = ngx.var.new_img_path .. ngx.var.ways local logo = ngx.var.new_img_path .. "logo.png" local gm = "/usr/bin/gm " local function mkdir_img_path(img_dir) --建文件路径 local cmd = "mkdir -p " .. img_dir os.execute(cmd) end local function get_wh() --获取原始长宽 local cmd = gm .. "identify " .. img_path local img_origin = io.popen(cmd) local img_info = img_origin:read("*all") local w_h = string.match(img_info,"%d+x%d+") local x = string.find(w_h,"x") local w = string.sub(w_h,0,(x-1)) local h = string.sub(w_h,(x+1),string.len(w_h)) return w,h end local function shuiyin(in_path,out_path) mkdir_img_path(img_dir) local w,h = get_wh() if tonumber(w) >= 400 or tonumber(h) >= 400 then cmd = gm .. "composite -gravity southeast -geometry +5+5 -dissolve 100 " .. logo .. " " .. in_path .. " " .. out_path os.execute(cmd) else cmd = "cp " .. in_path .. " " .. out_path end end local function resiz(in_path,out_path,thumbnail,quality) if not quality then quality = 100 end mkdir_img_path(img_dir) if thumbnail then local cmd = gm .."convert -quality " .. quality .. "% " .. in_path .. " -thumbnail " .. thumbnail .. " " .. out_path os.execute(cmd) end end local sy = ngx.var.sy local other = ngx.var.other local a = string.find(sy, "_") if a == 1 then thumbnail = string.match(other,"%d+x%d+") local in_path = img_path local out_path = new_img_path .. thumbnail resiz(in_path,out_path,thumbnail,quality) local in_path = new_img_path .. thumbnail local out_path = thumbnail_path shuiyin(in_path,out_path) elseif a == nil then local in_path = img_path local out_path = thumbnail_path shuiyin(in_path,out_path) end
几年前之前给我们网站 安团 和 智企ID 做的水印是这么做的,后来有了更好的方法所以摈弃了,故而拿出来交流交流。
相关文章推荐
- nginx代理指定目录
- 访问Nginx发生SSL connection error的一种情况
- Nginx+Naxsi部署专业级Web应用防火墙
- CentOS 6.2实战部署Nginx+MySQL+PHP
- nginx中http核心模块的配置指令2
- nginx中http核心模块的配置指令3
- nginx中http核心模块的配置指令4
- nginx中http的fastcgi模块的配置指令1
- Nginx 学习笔记(一)
- 网站502与504错误分析
- 用zabbix监控nginx_status状态
- 艰难完成 nginx + puma 部署 rails 4的详细记录
- 详解Lua中的表的概念及其相关操作方法
- Lua编程示例(二):面向对象、metatable对表进行扩展
- 把Lua编译进nginx步骤方法
- Lua脚本自动生成APK包
- Lua中的元表(metatable)、元方法(metamethod)详解
- Lua中的metatable介绍