Skip to content

Commit a0b228a

Browse files
author
root
committed
modified: README.md
modified: lua/img.lua modified: vhost/demo.conf modified: vhost/fastdfs.conf
1 parent f42efa6 commit a0b228a

File tree

4 files changed

+98
-74
lines changed

4 files changed

+98
-74
lines changed

README.md

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
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
5255
server {
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
-- 检测路径是否目录
104136
local 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
151185
if not is_dir(getFileDir(ngx.var.file)) then
152186
os.execute("mkdir -p " .. getFileDir(ngx.var.file))
153187
end
154188
155-
-- 如果原始文件存在,则缩放(以最小边,不变形)
189+
-- 裁剪后保证等比缩图 (缺点:裁剪了图片的一部分)
190+
-- 命令:gm convert input.jpg -thumbnail "100x100^" -gravity center -extent 100x100 output_3.jpg
156191
if (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);

lua/img.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- nginx lua thumbnail module
2+
-- created by yanue
23
-- last update : 2014/11/3
3-
-- version : 0.5.0
4-
-- created by yanne
4+
-- version : 0.5.1
55

66
-- 检测路径是否目录
77
local function is_dir(sPath)
@@ -47,6 +47,8 @@ end
4747

4848
-- 开始执行
4949
-- ngx.log(ngx.ERR, getFileDir(ngx.var.file));
50+
-- ngx.log(ngx.ERR,ngx.var.file);
51+
-- ngx.log(ngx.ERR,ngx.var.request_filepath);
5052

5153
local gm_path = 'gm'
5254

@@ -56,7 +58,7 @@ if not is_dir(getFileDir(ngx.var.file)) then
5658
end
5759

5860
-- 裁剪后保证等比缩图 (缺点:裁剪了图片的一部分)
59-
-- gm convert input.jpg -thumbnail "100x100^" -gravity center -extent 100x100 output_3.jpg
61+
-- 命令:gm convert input.jpg -thumbnail "100x100^" -gravity center -extent 100x100 output_3.jpg
6062
if (file_exists(ngx.var.request_filepath)) then
6163
local cmd = gm_path .. ' convert ' .. ngx.var.request_filepath
6264
cmd = cmd .. " -thumbnail " .. ngx.var.img_width .. "x" .. ngx.var.img_height .. "^"

vhost/demo.conf

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,33 @@
11
server {
22
listen 80;
3-
43
index index.php index.html index.htm;
4+
55
set $root_path '/var/www';
66
root $root_path;
77

88

99
location / {
10-
index index.html index.htm;
10+
index index.html index.htm;
1111
}
1212

13-
1413
location /lua {
1514
default_type 'text/plain';
16-
content_by_lua 'ngx.say("hello, ttlsa lua")';
15+
content_by_lua 'ngx.say("hello, ttlsa lua")';
1716
}
1817

19-
#/thumb目录下的图片请求不经过缩略图模块
20-
location ^~ /thumb/ {
21-
22-
}
23-
24-
# 所有符合规则的图片/xx/xx/xx.jpg_100x100.jpg
2518
location ~* ^(.+\.(jpg|jpeg|gif|png))_(\d+)+x(\d+)+\.(jpg|jpeg|gif|png)$ {
26-
root $root_path; # 这里必须设置,否则根目录,即 $document_root 会是 Nginx 默认的 Nginx Root/html,在 Lua 中会得不到期望的值
27-
set $thumbnail_root $root_path/thumb;
28-
set $file $thumbnail_root$uri; #如果缩略图文件存在,直接返回
29-
if (-f $file) {
30-
rewrite ^/(.*)$ /thumb/$1 last;
31-
}
32-
33-
if (!-f $file) { # 如果文件不存在时才需要裁剪
34-
add_header X-Powered-By 'Lua GraphicsMagick'; # 此 HTTP Header 无实际意义,用于测试
35-
add_header file-path $request_filename; # 此 HTTP Header 无实际意义,用于测试
36-
set $request_filepath $root_path$1; # 设置原始图片路径,如:/document_root/1.gif
37-
set $img_width $3; # 设置裁剪/缩放的宽度
38-
set $img_height $4; # 设置裁剪/缩放的高度
39-
set $img_ext $5; # 图片文件格式后缀
40-
content_by_lua_file /etc/nginx/lua/img.lua; # 加载外部 Lua 文件
41-
}
19+
root /tmp/thumb; # 这里必须设置,否则根目录,即 $document_root 会是 Nginx 默认的 Nginx Root/html,在 Lua 中会得不到期望的值
20+
set $thumbnail_root /tmp/thumb;
21+
set $img_original_root $root_path;
22+
set $file $thumbnail_root$uri; #如果缩略图文件存在,直接返回
23+
24+
if (!-f $file) { # 如果文件不存在时才需要裁剪
25+
set $request_filepath $img_original_root$1; # 设置原始图片路径,如:/document_root/1.gif
26+
set $img_width $3; # 设置裁剪/缩放的宽度
27+
set $img_height $4; # 设置裁剪/缩放的高度
28+
set $img_ext $2; # 图片文件格式后缀
29+
content_by_lua_file /etc/nginx/lua/img.lua; # 加载外部 Lua 文件
30+
}
4231
}
4332

4433
location ~ /\.ht {

vhost/fastdfs.conf

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
server{
22
listen 80;
33
server_name static.saleasy.net static.isaleasy.com static.estt.com.cn;
4-
4+
5+
# 缩放图片链接
56
location ~* ^(\/pic\/M00(.+\.(jpg|jpeg|gif|png))_(\d+)+x(\d+)+\.(jpg|jpeg|gif|png))$ {
67
root /opt/fastdfs/thumb; # 这里必须设置,否则根目录,即 $document_root 会是 Nginx 默认的 Nginx Root/html,在 Lua 中会得不到期望的值
78
set $thumbnail_root /opt/fastdfs/thumb;
@@ -17,16 +18,12 @@ server{
1718
}
1819
}
1920

21+
# 默认图片
2022
location /pic/M00 {
2123
alias /opt/fastdfs/pic/store0/data;
2224
ngx_fastdfs_module;
2325
}
2426

25-
location /media/M00 {
26-
alias /opt/fastdfs/media/store0/data;
27-
ngx_fastdfs_module;
28-
}
29-
3027
location = /favicon.ico {
3128
log_not_found off;
3229
access_log off;

0 commit comments

Comments
 (0)