11# nginx-lua-GraphicsMagick
2- 用Lua脚本实现的图片处理模块,目前实现了缩略图功能
2+ Nginx+Lua+GraphicsMagick,实现自定义图片尺寸功能,支持FastDFS文件存储
33
44## 说明
5- 目前主要实现图片缩略图功能,可对不同目录配置缩略图尺寸 。
6- 主要将缩放的图片放在独立的thumb目录 ,并保持原有的图片目录结构.
5+ 类似淘宝图片,实现自定义图片尺寸功能,可根据图片加后缀_100x100.jpg形式实现自定义输出图片大小 。
6+ 主要将自定义尺寸的图片放在完全独立的thumb目录(自定义目录) ,并保持原有的图片目录结构。
77
88#### 文件夹规划
99``` bash
10- img.xxx.com
10+ img.xxx.com(如/var/www/img)
1111| -- img1
1212| ` -- 001
1313| ` -- 001.jpg
@@ -16,19 +16,20 @@ img.xxx.com
1616| -- img3
1717| ` -- 001
1818| ` -- 001.jpg
19- ` -- thumb
19+ ` ` `
20+ # ### 自定义尺寸后的路径
21+ ` ` ` bash
22+ thumb(如/tmp/thumb,可在conf文件里面更改)
2023 ` -- img1
2124 ` -- 001
2225 | -- 001_100x100.jpg
2326 | -- 001_200x160.jpg
2427` ` `
25-
2628其中img.xxx.com为图片站点根目录,img1,img2...目录是原图目录,可根据目录设置不同的缩略图尺寸,thumb文件夹用来存放缩略图,可定时清理。
2729
2830# ### 链接地址对应关系
2931* 原图访问地址:` ` ` http://img.xxx.com/xx/001/001.jpg` ` `
30- * 缩略图访问地址:``` http://img.xxx.com/xx/001/001.jpg_100x100.jpg ``` (请勿加thumb)
31- * 实际缩略图地址:``` http://img.xxx.com/thumb/xx/001/001.jpg_100x100.jpg ``` (请勿加thumb)
32+ * 缩略图访问地址:` ` ` http://img.xxx.com/xx/001/001.jpg_100x100.jpg` ` `
3233
3334# ### 访问流程
3435* 首先判断缩略图是否存在,如存在则直接显示缩略图;
@@ -38,68 +39,99 @@ img.xxx.com
3839
3940
4041# # 依赖
41- * Nginx(configure arguments: --prefix=/usr/local/nginx --user=www --group=www --pid-path=/opt/logs/nginx/nginx.pid --lock-path=/opt/logs/nginx/nginx.lock --error-log-path=/opt/logs/nginx/error.log --http-log-path=/opt/logs/nginx/access.log --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_flv_module --with-http_dav_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_addition_module --with-zlib=../zlib-1.2.8 --with-pcre=../pcre-8.36 --add-module=../nginx-http-concat --add-module=../lua-nginx-module/ --add-module=../echo-nginx-module/ --add-module=../ngx_devel_kit/)
42+ * Nginx
43+ ` ` ` bash
44+ ./configure --prefix=/usr/local/nginx --user=www --group=www --pid-path=/opt/logs/nginx/nginx.pid --lock-path=/opt/logs/nginx/nginx.lock --error-log-path=/opt/logs/nginx/error.log --http-log-path=/opt/logs/nginx/access.log --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_flv_module --with-http_dav_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_addition_module --with-zlib=../zlib-1.2.8 --with-pcre=../pcre-8.36 --add-module=../nginx-http-concat --add-module=../lua-nginx-module/ --add-module=../echo-nginx-module/ --add-module=../ngx_devel_kit/
45+ ` ` `
4246* GraphicsMagick(1.3.18)
43- * libjpeg-6b
44- * libpng-1.2.49
45- * freetype-2.4.10
47+ * libjpeg
48+ * libpng
4649* inotify(可选)
4750
4851# # 安装
4952
50- #### nginx vhost 配置
53+ # ### nginx vhost default配置
5154` ` ` bash
5255server {
5356 listen 80;
54-
5557 index index.php index.html index.htm;
58+
5659 set $root_path ' /var/www' ;
5760 root $root_path ;
5861
5962
6063 location / {
61- index index.html index.htm;
64+ index index.html index.htm;
6265 }
6366
64-
6567 location /lua {
6668 default_type ' text/plain' ;
67- content_by_lua ' ngx.say("hello, ttlsa lua")' ;
69+ content_by_lua ' ngx.say("hello, ttlsa lua")' ;
6870 }
6971
70- # /thumb目录下的图片请求不经过缩略图模块
71- location ^~ /thumb/ {
72+ location ~ * ^(.+\. (jpg| jpeg| gif| png))_(\d +)+x(\d +)+\. (jpg| jpeg| gif| png)$ {
73+ root /tmp/thumb; # 这里必须设置,否则根目录,即 $document_root 会是 Nginx 默认的 Nginx Root/html,在 Lua 中会得不到期望的值
74+ set $thumbnail_root /tmp/thumb;
75+ set $img_original_root $root_path ;
76+ set $file $thumbnail_root$uri ; # 如果缩略图文件存在,直接返回
77+
78+ if (! -f $file ) { # 如果文件不存在时才需要裁剪
79+ set $request_filepath $img_original_root $1 ; # 设置原始图片路径,如:/document_root/1.gif
80+ set $img_width $3 ; # 设置裁剪/缩放的宽度
81+ set $img_height $4 ; # 设置裁剪/缩放的高度
82+ set $img_ext $2 ; # 图片文件格式后缀
83+ content_by_lua_file /etc/nginx/lua/img.lua; # 加载外部 Lua 文件
84+ }
85+ }
7286
87+ location ~ /\. ht {
88+ deny all;
7389 }
90+ }
91+ ` ` `
7492
75- # 所有符合规则的图片/xx/xx/xx.jpg_100x100.jpg
76- location ~ * ^(.+\. (jpg| jpeg| gif| png))_(\d +)+x(\d +)+\. (jpg| jpeg| gif| png)$ {
77- root $root_path ; # 这里必须设置,否则根目录,即 $document_root 会是 Nginx 默认的 Nginx Root/html,在 Lua 中会得不到期望的值
78- set $thumbnail_root $root_path /thumb;
79- set $file $thumbnail_root$uri ; # 如果缩略图文件存在,直接返回
80- if (-f $file ) {
81- rewrite ^/(.* )$ /thumb/$1 last;
82- }
93+ # ### nginx fastdfs配置
94+ ` ` ` bash
95+ server{
96+ listen 80;
97+ server_name static.saleasy.net static.isaleasy.com static.estt.com.cn;
98+
99+ # 缩放图片链接
100+ location ~ * ^(\/ pic\/ M00(.+\. (jpg| jpeg| gif| png))_(\d +)+x(\d +)+\. (jpg| jpeg| gif| png))$ {
101+ root /opt/fastdfs/thumb; # 这里必须设置,否则根目录,即 $document_root 会是 Nginx 默认的 Nginx Root/html,在 Lua 中会得不到期望的值
102+ set $thumbnail_root /opt/fastdfs/thumb;
103+ set $fdfs_group_root /opt/fastdfs/pic/store0/data;
104+ set $file $thumbnail_root$uri ;
83105
84106 if (! -f $file ) { # 如果文件不存在时才需要裁剪
85- add_header X-Powered-By ' Lua GraphicsMagick' ; # 此 HTTP Header 无实际意义,用于测试
86- add_header file-path $request_filename ; # 此 HTTP Header 无实际意义,用于测试
87- set $request_filepath $root_path $1 ; # 设置原始图片路径,如:/document_root/1.gif
88- set $img_width $3 ; # 设置裁剪/缩放的宽度
89- set $img_height $4 ; # 设置裁剪/缩放的高度
90- set $img_ext $5 ; # 图片文件格式后缀
107+ set $request_filepath $fdfs_group_root $2 ; # 设置原始图片路径,如:/document_root/1.gif
108+ set $img_width $4 ; # 设置裁剪/缩放的宽度
109+ set $img_height $5 ; # 设置裁剪/缩放的高度
110+ set $img_ext $3 ; # 图片文件格式后缀
91111 content_by_lua_file /etc/nginx/lua/img.lua; # 加载外部 Lua 文件
92112 }
93113 }
94114
95- location ~ /\. ht {
96- deny all;
115+ # 默认图片
116+ location /pic/M00 {
117+ alias /opt/fastdfs/pic/store0/data;
118+ ngx_fastdfs_module;
119+ }
120+
121+ location = /favicon.ico {
122+ log_not_found off;
123+ access_log off;
97124 }
98125}
99126` ` `
100127
101- # ### lua文件
128+ # ### img. lua文件
102129` ` ` bash
130+ -- nginx lua thumbnail module
131+ -- created by yanue
132+ -- last update : 2014/11/3
133+ -- version : 0.5.1
134+
103135-- 检测路径是否目录
104136local function is_dir(sPath)
105137 if type(sPath) ~ = " string" then return false end
@@ -144,18 +176,22 @@ end
144176
145177-- 开始执行
146178-- ngx.log(ngx.ERR, getFileDir(ngx.var.file));
179+ -- ngx.log(ngx.ERR,ngx.var.file);
180+ -- ngx.log(ngx.ERR,ngx.var.request_filepath);
147181
148- local gm_path = '/usr/local/bin/ gm'
182+ local gm_path = 'gm'
149183
150184-- check image dir
151185if not is_dir(getFileDir(ngx.var.file)) then
152186 os.execute(" mkdir -p " .. getFileDir(ngx.var.file))
153187end
154188
155- -- 如果原始文件存在,则缩放(以最小边,不变形)
189+ -- 裁剪后保证等比缩图 (缺点:裁剪了图片的一部分)
190+ -- 命令:gm convert input.jpg -thumbnail " 100x100^" -gravity center -extent 100x100 output_3.jpg
156191if (file_exists(ngx.var.request_filepath)) then
157192 local cmd = gm_path .. ' convert ' .. ngx.var.request_filepath
158- cmd = cmd .. " -resize " .. ngx.var.img_width .. " x" .. ngx.var.img_height
193+ cmd = cmd .. " -thumbnail " .. ngx.var.img_width .. " x" .. ngx.var.img_height .. " ^"
194+ cmd = cmd .. " -gravity center -extent " .. ngx.var.img_width .. " x" .. ngx.var.img_height
159195 cmd = cmd .. " +profile \" * \" " .. ngx.var.file;
160196 ngx.log(ngx.ERR, cmd);
161197 os.execute(cmd);
0 commit comments