|
| 1 | +# nginx-lua-GraphicsMagick |
| 2 | +用Lua脚本实现的图片处理模块,目前实现了缩略图功能 |
| 3 | + |
| 4 | +## 说明 |
| 5 | +目前主要实现图片缩略图功能,可对不同目录配置缩略图尺寸。 |
| 6 | +主要将缩放的图片放在独立的thumb目录,并保持原有的图片目录结构. |
| 7 | + |
| 8 | +#### 文件夹规划 |
| 9 | +```bash |
| 10 | +img.xxx.com |
| 11 | +|-- img1 |
| 12 | +| `-- 001 |
| 13 | +| `-- 001.jpg |
| 14 | +|-- img2 |
| 15 | +| `-- notfound.jpg |
| 16 | +|-- img3 |
| 17 | +| `-- 001 |
| 18 | +| `-- 001.jpg |
| 19 | +`-- thumb |
| 20 | + `-- img1 |
| 21 | + `-- 001 |
| 22 | + |-- 001_100x100.jpg |
| 23 | + |-- 001_200x160.jpg |
| 24 | +``` |
| 25 | + |
| 26 | +其中img.xxx.com为图片站点根目录,img1,img2...目录是原图目录,可根据目录设置不同的缩略图尺寸,thumb文件夹用来存放缩略图,可定时清理。 |
| 27 | + |
| 28 | +#### 链接地址对应关系 |
| 29 | +* 原图访问地址:```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 | + |
| 33 | +#### 访问流程 |
| 34 | +* 首先判断缩略图是否存在,如存在则直接显示缩略图; |
| 35 | +* 缩略图不存在,则判断原图是否存在,如原图存在则拼接graphicsmagick命令,生成并显示缩略图,否则返回404 |
| 36 | + |
| 37 | +## 配置 |
| 38 | + |
| 39 | + |
| 40 | +## 依赖 |
| 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 | +* GraphicsMagick(1.3.18) |
| 43 | + * libjpeg-6b |
| 44 | + * libpng-1.2.49 |
| 45 | + * freetype-2.4.10 |
| 46 | +* inotify(可选) |
| 47 | + |
| 48 | +## 安装 |
| 49 | + #/thumb目录下的图片请求不经过缩略图模块 |
| 50 | + location ^~ /thumb/ { |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + # 所有符合规则的图片/xx/xx/xx.jpg_100x100.jpg |
| 55 | + location ~* ^(.+\.(jpg|jpeg|gif|png))_(\d+)+x(\d+)+\.(jpg|jpeg|gif|png)$ { |
| 56 | + root $root_path; # 这里必须设置,否则根目录,即 $document_root 会是 Nginx 默认的 Nginx Root/html,在 Lua 中会得不到期望的值 |
| 57 | + set $thumbnail_root $root_path/thumb; |
| 58 | + set $file $thumbnail_root$uri; #如果缩略图文件存在,直接返回 |
| 59 | + if (-f $file) { |
| 60 | + rewrite ^/(.*)$ /thumb/$1 last; |
| 61 | + } |
| 62 | + |
| 63 | + if (!-f $file) { # 如果文件不存在时才需要裁剪 |
| 64 | + add_header X-Powered-By 'Lua GraphicsMagick'; # 此 HTTP Header 无实际意义,用于测试 |
| 65 | + add_header file-path $request_filename; # 此 HTTP Header 无实际意义,用于测试 |
| 66 | + set $request_filepath $root_path$1; # 设置原始图片路径,如:/document_root/1.gif |
| 67 | + set $img_width $3; # 设置裁剪/缩放的宽度 |
| 68 | + set $img_height $4; # 设置裁剪/缩放的高度 |
| 69 | + set $img_ext $5; # 图片文件格式后缀 |
| 70 | + content_by_lua_file /etc/nginx/lua/img.lua; # 加载外部 Lua 文件 |
| 71 | + } |
| 72 | + } |
0 commit comments