使用Tengine+Lua+GraphicsMagick实现图片自动裁剪缩放

需求:图片处理

=======互联网电商网站( PC、移动)

=======大量不同需求的图片(同一个图片需要不同尺寸的缩略图)

两种策略生成缩略图

     --------在上传图片时,就生成所需缩略图

     ---------根据请求指定尺寸的图片自动生成相应的缩略图片 Tengine+Lua+GraphicsMagick

可以查看淘宝的案例

 

Tengine 淘宝优化改良的Nginx,并集成了Lua模块   负责展示图片和调度Lua脚本

Lua 脚本语言,嵌入到应用程序中,提供灵活的扩展和定制功能 Tengine通过Lua实现对GM操作 控制裁剪、缩放规格

GraphicsMagick 强大的图片处理工具,没有UI的PS,动态的生成图片,特别适用于互联网的应用 负责图片的处理

软件列表

软件名称

 安装包

下载地址

Tengine

tengine-master.zip

https://github.com/alibaba/tengine

Lua

lua-5.3.1.tar.gz

LuaJIT-2.0.4.tar.gz(Lua依赖包)

http://www.lua.org/ftp/

http://luajit.org/download.html

GraphicsMagick

GraphicsMagick-1.3.18.tar.gz

https://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/

依赖包和类库,可yum安装 libjpeg、libjpeg-devel libpng、libpng-devel giflib、giflib-devel freetype、freetype-devel

Lua 安装依赖(readline&readline-devel) 安装Lua(源码编译安装) 安装LuaJIT (源码编译安装)

Tengine 进入Tengine源码目录,使用configure配置安装路径以及需要安装的模块 安装Tengine (源码编译安装)

GraphicsMagick 安装依赖 libjpeg & libjpeg-devel libpng & libpng-devel giflib & giflib-devel freetype & freetype-devel 进入GM源码目录,使用configure配置安装路径以及需要安装的模块 安装GM(源码编译安装)

配置 Lua脚本文件(ImageResizer.lua ) 位置:/usr/local/Tengine/lua/ImageResizer.lua 权限:可执行 Tengine配置(nginx.conf )


#user  nobody;
user  root;  # 裁剪图片需要root权限
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
#    load ngx_http_fastcgi_module.so;
#    load ngx_http_rewrite_module.so;
#}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  img.itrip.project.bdqn.cn;
        root /data/itrip/uploadimg;

        location / {
             root /data/itrip/uploadimg; # 站点根目录
             expires 1h;    # 缓存时间
             add_header Cache-Control max-age=3600; # 缓存时间
             access_log   /var/log/Tengine/host_access.log;
        }

        #如果 url 格式如:xxxx.gif_数字x数字.gif
        location ~* ^(.+\.(jpg|jpeg|gif|png))_(\d+)x(\d+)\.(jpg|jpeg|gif|png)$ {
           root /data/itrip/uploadimg;    #这里必须设置,否则根目录,即 $document_root 会是 Nginx 默认的 Nginx Root/html,在 Lua 中会得不到期望的值
           if (!-f $request_filename) { #如果文件不存在时才需要裁剪
              add_header X-Powered-By 'Lua GraphicsMagick';  #此HTTP Header无实际意义,用于测试
              add_header file-path $request_filename;  #此 HTTP Header无实际意义,用于测试
              lua_code_cache on;  #在编写外部 Lua脚本时,设置为off Nginx不会缓存 Lua,方便调试
              set $request_filepath /data/itrip/uploadimg$1;  #设置原始图片路径,如:/document_root/1.gif
              set $width $3;     # 设置裁剪/缩放的宽度
              set $height $4;    # 设置裁剪/缩放的高度
              set $ext $5;      # 图片文件格式后缀
              content_by_lua_file /usr/local/Tengine/lua/ImageResizer.lua;  #加载外部 Lua 文件
            }
        }

 


        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}

 

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值