Nginx+Lua+GraphicsMagick,实现自定义图片尺寸功能,支持两种模式[固定高宽模式,定高或定宽模式],支持FastDFS文件存储
github地址: https://github.com/yanue/nginx-lua-GraphicsMagick
img.xxx.com(如/var/www/img) |-- img1 | `-- 001 | `-- 001.jpg |-- img2 | `-- notfound.jpg |-- img3 | `-- 001 | `-- 001.jpg
thumb(如/tmp/thumb,可在conf文件里面更改) `-- img1 `-- 001 |-- 001_200x160.jpg 固定高和宽 |-- 001_-100.jpg 定高 |-- 001_200-.jpg 定宽
http://img.xxx.com/xx/001/001.jpg
http://img.xxx.com/xx/001/001.jpg_100x100.jpg
即为宽100,高100 http://img.xxx.com/xx/001/001.jpg_-100.jpg
用"-"表示自动,即为高100,宽自动 http://img.xxx.com/xx/001/001.jpg_100-.jpg
用"-"表示自动,即为宽100,高自动 CentOS6 安装过程见 nginx+lua+GraphicsMagick安装
bash ./configure --prefix=/usr/local/nginx / --user=www / --group=www / --sbin-path=/usr/sbin/nginx / --conf-path=/etc/nginx/nginx.conf / --pid-path=/var/run/nginx.pid / --lock-path=/var/run/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-http_spdy_module / --with-pcre / --with-zlib=../zlib-1.2.8 / --add-module=../nginx-http-concat / --add-module=../lua-nginx-module / --add-module=../ngx_devel_kit /
nginx 配置文件 /etc/nginx
vhost 为站点配置
server{ listen 80 # set var for thumb pic set $upload_path /opt/uploads; set $img_original_root $upload_path;# original root; set $img_thumbnail_root $upload_path/cache/thumb; set $img_file $img_thumbnail_root$uri; # like:/xx/xx/xx.jpg_100-.jpg or /xx/xx/xx.jpg_-100.jpg location ~* ^(.+/.(jpg|jpeg|gif|png))_((/d+/-)|(/-/d+))/.(jpg|jpeg|gif|png)$ { root $img_thumbnail_root; # root path for croped img set $img_size $3; if (!-f $img_file) { # if file not exists add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue'; # header for test add_header file-path $request_filename; # header for test set $request_filepath $img_original_root$1; # origin_img full path:/document_root/1.gif set $img_size $3; # img width or height size depends on uri set $img_ext $2; # file ext content_by_lua_file /etc/nginx/lua/autoSize.lua; # load lua } } # like: /xx/xx/xx.jpg_100x100.jpg location ~* ^(.+/.(jpg|jpeg|gif|png))_(/d+)+x(/d+)+/.(jpg|jpeg|gif|png)$ { root $img_thumbnail_root; # root path for croped img if (!-f $img_file) { # if file not exists add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue'; # header for test add_header file-path $request_filename; # header for test set $request_filepath $img_original_root$1; # origin_img file path set $img_width $3; # img width set $img_height $4; # height set $img_ext $5; # file ext content_by_lua_file /etc/nginx/lua/cropSize.lua; # load lua } } location = /favicon.ico { log_not_found off; access_log off; } }
server{ listen 80; server_name xxx.com; set $img_thumbnail_root /opt/fastdfs/thumb; #set thumb path set $img_file $img_thumbnail_root$uri; #thumb file # like:/pic/M00/xx/xx/xx.jpg_100-.jpg or /pic/M00/xx/xx/xx.jpg_-100.jpg location ~* ^(//(/w+)(//M00)(.+/.(jpg|jpeg|gif|png)))_((/d+/-)|(/-/d+))/.(jpg|jpeg|gif|png)$ { root $img_thumbnail_root; # root path for croped img set $fdfs_group_root /opt/fastdfs/$2/store0/data; #set fastdfs group path $2 if (!-f $img_file) { # if thumb file not exists add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue'; # header for test add_header file-path $request_filename; # header for test set $request_filepath $fdfs_group_root$4; # origin_img full path:/document_root/1.gif set $img_size $6; # img width or height size depends on uri : img size like "-100" or "100-", "-" means auto size set $img_ext $5; # file ext content_by_lua_file /etc/nginx/lua/autoSize.lua; # load auto width or height crop Lua file } } # like:/pic/M00/xx/xx/xx.jpg_200x100.jpg location ~* ^(//(/w+)(//M00)(.+/.(jpg|jpeg|gif|png))_(/d+)+x(/d+)+/.(jpg|jpeg|gif|png))$ { root $img_thumbnail_root; # root path for croped img set $fdfs_group_root /opt/fastdfs/$2/store0/data; #set fastdfs group path $2 if (!-f $img_file) { # if thumb file not exists add_header X-Powered-By 'Nginx+Lua+GraphicsMagick By Yanue'; # header for test add_header file-path $request_filename; # header for test set $request_filepath $fdfs_group_root$4; # real file path set $img_width $6; # img width set $img_height $7; # img height set $img_ext $5; # file ext content_by_lua_file /etc/nginx/lua/cropSize.lua; # load crop Lua file } } location /pic/M00 { alias /opt/fastdfs/pic/store0/data; ngx_fastdfs_module; } location /chat/M00 { alias /opt/fastdfs/chat/store0/data; ngx_fastdfs_module; } location = /favicon.ico { log_not_found off; access_log off; } }
参考: https://github.com/hopesoft/nginx-lua-image-module