1 Star 0 Fork 49

留云 / nginx-lua-GraphicsMagick

forked from yanue / nginx-lua-GraphicsMagick
暂停
 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

nginx-lua-GraphicsMagick

Nginx+Lua+GraphicsMagick,实现自定义图片尺寸功能,支持两种模式[固定高宽模式,定高或定宽模式],支持FastDFS文件存储
github地址:https://github.com/yanue/nginx-lua-GraphicsMagick

说明

  • 类似淘宝图片,实现自定义图片尺寸功能,可根据图片加后缀_100x100.jpg(固定高宽),_-100.jpg(定高),_100-.jpg(定宽)形式实现自定义输出图片大小。
  • 主要将自定义尺寸的图片放在完全独立的thumb目录(自定义目录),并保持原有的图片目录结构。

2016-01-14更新说明

  • 新增定高或定宽裁切模式 左右结构,用"-"号区分未知高或未知宽("-"号不会被浏览器url转义),如 如: xx.jpg_100-.jpg 宽100,高自动 如: xx.jpg_-100.jpg 高100,宽自动
  • 新增 php 动态获取图片尺寸的类文件

文件夹规划

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 定宽
  • 其中img.xxx.com为图片站点根目录,img1,img2...目录是原图目录
  • 缩略图目录根据保持原有结构,并单独设置目录,可定时清理。

链接地址对应关系

  • 原图访问地址: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,高自动

访问流程

  • 首先判断缩略图是否存在,如存在则直接显示缩略图;
  • 缩略图不存在,则判断原图是否存在,如原图存在则拼接graphicsmagick(gm)命令,生成并显示缩略图,否则返回404

安装

CentOS6 安装过程见 nginx+lua+GraphicsMagick安装

配置

依赖

  • Nginx
./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 \
  • GraphicsMagick(1.3.18)
    • libjpeg
    • libpng
    • inotify(可选)

配置文件说明

nginx 配置文件 /etc/nginx
vhost 为站点配置
  • demo.conf 为 普通站点 配置文件,包含固定高宽和定高,定宽两种模式配置
  • fdfs.conf 为 fastdfs 配置文件,包含固定高宽和定高,定宽两种模式配置
lua 为裁切图片处理目录
  • autoSize.lua 定高或定宽模式裁切图片处理lua脚本
  • cropSize.lua 固定高宽模式裁切图片处理lua脚本

nginx vhost demo配置

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;
    }
}

nginx fastdfs配置

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;
    }
}

最后说明

  • lua 脚本处理并未做任何图片尺寸限制,这样很容易被恶意改变宽和高参数而随意生成大量文件,浪费资源和空间,请根据直接情况自行处理

参考:https://github.com/hopesoft/nginx-lua-image-module

空文件

简介

类似淘宝图片,根据参数获取图片自定义尺寸, 动态生成缩略图,支持两种模式[固定高宽模式,定高或定宽模式],支持FastDFS文件存储,github地址:https://github.com/yanue/nginx-lua-GraphicsMagick 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/xhf2006/nginx-lua-GraphicsMagick.git
git@gitee.com:xhf2006/nginx-lua-GraphicsMagick.git
xhf2006
nginx-lua-GraphicsMagick
nginx-lua-GraphicsMagick
master

搜索帮助

14c37bed 8189591 565d56ea 8189591