狂神声明 : 文章均为自己的学习笔记 , 转载一定注明出处 ; 编辑不易 , 防君子不防小人~共勉 !

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

需求 : 图片处理

问题 : 

  • 互联网电商网站 ( PC, 移动)
  • 大量不同需求的图片 (同一个图片需要不同尺寸的缩略图)

解决方案(两种策略) : 

  • 在上传图片时 , 就生成所需缩略图
  • 根据请求指定尺寸的图片自动生成相应的缩略图片
    • Tengine+Lua+GraphicsMagick

Tengine+Lua+GraphicsMagick

  • Tengine

    • 淘宝优化改良的Nginx , 并集成了Lua模块
    • 负责展示图片和调度Lua脚本
  • Lua
    • 脚本语言 , 嵌入到应用程序中 , 提供灵活的扩展和定制功能
    • Tengine通过Lua实现对GM操作
    • 控制裁剪 , 缩放规格
  • GraphicsMagick
    • 强大的图片处理工具 , 没有UI的PS , 动态的生成图片 , 特别适用于互联网的应用
    • 负责图片的处理
  • 好处 :
    • 按需处理
    • 图片处理更加高效
    • GM支持高并发下的图片处理 , 有效的保证了性能

安装配置

软件列表

  • 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

安装

  • 源码安装---> ./configure   make   make install
  • yum安装--->  yum install ~
  1. Lua

    • 安装依赖 ( readline&readline-devel )
    • 安装Lua ( 源码编译安装 )
    • 安装LuaJIT ( 源码编译安装 )
  2. Tengine
    • 进入Tengine源码目录 , 使用Configure配置安装路径以及需要安装的模块
    • 安装Tengine ( 源码编译安装 )
      ./configure --prefix=/usr/local/Tengine --dso-path=/usr/local/Tengine/modules --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_concat_module --with-http_lua_module  --http-proxy-temp-path=/var/tmp/Tengine/proxy_temp --http-fastcgi-temp-path=/var/tmp/Tengine/fastcgi_temp --http-uwsgi-temp-path=/var/tmp/Tengine/uwsgi_temp --http-scgi-temp-path=/var/tmp/Tengine/cgi_temp --http-client-body-temp-path=/var/tmp/Tengine/client_body_temp --http-log-path=/var/log/Tengine/access.log --error-log-path=/var/log/Tengine/error.log
  3. GraphicsMagick
    • 安装依赖

      • libjpeg , libjpeg-devel
      • libpng , libpng-devel
      • giflib , giflib-devel
      • freetype , freetype-devel
    • 进入GM源码目录 , 使用configure配置安装路径以及需要安装的模板
    • 安装GM ( 源码编译安装 )
      ./configure --prefix=/usr/local/GraphicsMagick --enable-shared

配置

  • Lua 脚本文件  ( ImageResizer.lua )

    • 位置 : /usr/local/Tengine/lua/ImageResizer.lua
    • 权限 : 可执行 (chomd 777 /usr/local/Tengine/lua/ImageResizer.lua )
      local command = "/usr/local/GraphicsMagick/bin/gm convert " .. ngx.var.request_filepath .. " -resize " .. ngx.var.width .. "x" .. ngx.var.height .. " +profile \"*\" " .. ngx.var.request_filepath .. "_" .. ngx.var.width .. "x" .. ngx.var.height .. "." .. ngx.var.ext;
      os.execute(command);
      ngx.exec(ngx.var.request_uri);
  • Tengine 配置 ( nginx.config )
    #user  nobody;
    user  root;  # 裁剪图片需要root权限
    worker_processes  ;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    events {
        worker_connections  ;
    }
    
    # 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  ;
        keepalive_timeout  ;
    
        #gzip  on;
    
        server {
            listen       ;
            server_name  img.itrip.project.bdqn.cn;
            root /data/itrip/uploadimg;
    
            location / {
                 root /data/itrip/uploadimg; # 站点根目录
                 expires 1h;    # 缓存时间
                 add_header Cache-Control max-age=; # 缓存时间
                 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$;  #设置原始图片路径,如:/document_root/.gif
                  set $width $;     # 设置裁剪/缩放的宽度
                  set $height $;    # 设置裁剪/缩放的高度
                  set $ext $;      # 图片文件格式后缀
                  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                /.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page        /50x.html;
            location = /50x.html {
                root   html;
            }
    
            # proxy the PHP scripts to Apache listening on
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on
            #
            #location ~ \.php$ {
            #    root           html;
            #    fastcgi_pass   ;
            #    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       ;
        #    listen       somename:;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
        # HTTPS server
        #
        #server {
        #    listen        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;
        #    }
        #}
    
    }

Tengine+Lua+GraphicsMagick的更多相关文章

  1. Tengine + Lua + GraphicsMagick 实现图片自动裁剪/缩放

    http://my.oschina.net/eduosi/blog/169606

  2. tengine lua 开源一 调用内部接口高效发送文件

    tengine  lua 开源一 调用内部接口高效发送文件 开源自己封装的sendfile 模块,可以高效的通过lua发送文件 源码地址:https://github.com/weinyzhou/Lu ...

  3. tengine + lua 实现流量拷贝

    环境搭建参考地址:http://www.cnblogs.com/cp-miao/p/7505910.html cp.lua local res1, res2, action action = ngx. ...

  4. tengine+lua的安装步骤

    我是在Red Hat 5.8 的虚机上安装的. Nginx的一些模块需要其他第三方库的支持,例如gzip模块需要zlib库,rewrite模块需要pcre库,ssl功能需要openssl库等.建议把这 ...

  5. 【技术干货】听阿里云CDN安防技术专家金九讲tengine+lua开发

    一.介绍 二.安装 三.运行 四.开发 1.介绍 Tengine:轻量级.高性能.高并发.配置化.模块化.可扩展.可移植的Web和反向代理 服务器,Tengine是nginx超集,但做了很多优化,包含 ...

  6. OpenResty+lua+GraphicsMagick生成缩略图

    1.安装GraphicsMagick 下载地址:http://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.19/G ...

  7. OpenResty(Nginx)+Lua+GraphicsMagick实现缩略图功能

    http://www.hopesoft.org/blog/?p=1188 http://www.imagemagick.org/download/ 2.用法 原始图片是input.jpg,尺寸:160 ...

  8. 如何安装nginx_lua_module模块,升级nginx,nginx-lua-fastdfs-GraphicsMagick动态生成缩略图,实现图片自动裁剪缩放

    如何安装nginx_lua_module模块,升级nginx,nginx-lua-fastdfs-GraphicsMagick动态生成缩略图,实现图片自动裁剪缩放 参考网站:nginx-lua-fas ...

  9. nginx lua处理图片

    user apache apache; worker_processes 4; worker_rlimit_nofile 100000; #error_log logs/error.log; #err ...

随机推荐

  1. 求逆序对常用的两种算法 ----归并排 & 树状数组

    网上看了一些归并排求逆序对的文章,又看了一些树状数组的,觉得自己也写一篇试试看吧,然后本文大体也就讲个思路(没有例题),但是还是会有个程序框架的 好了下面是正文 归并排求逆序对 树状数组求逆序对 一. ...

  2. 学习pano2vr制作html5全景笔记

    demo截图: demo下载: 百度网盘:http://pan.baidu.com/s/1o8yBwIA 密码:nf62(启服务端查看); 我制作是全屏定点360的全景页面,使用pano2vr软件制作 ...

  3. Python 爬虫 某迅漫画 selemiun+plantomJS

    目标站点需求分析 爬取某迅漫画到本地 涉及的库 selenium, PhantomJS time,urllib.request,os,random 模拟滑动窗口,获取完整网页 保存到文件中 获取本地h ...

  4. JS高级

    一.函数高级 1.函数回调 函数回调的本质:在一个函数中,满足特定条件下,调用另一个函数 // 回调的函数 function callback(data) {} // 逻辑函数 function fu ...

  5. IntelliJ IDEA 使提示不区分大小写

    File ==> Settings ==> Editor ==> General ==> Code Completion 第一行 Match case 将默认勾选去掉

  6. 解决 for xml path encode 的问题

    select stuff( (select ', <' + name + '>' from sys.databases where database_id > 4 order by ...

  7. 图书管理系统 (c语言实现) (全部代码)

    直接上代码不解释 #include <stdio.h> #include <stdlib.h> #include <string.h> #define AVAILA ...

  8. leetcode刷题第二天<两数相加>

    题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...

  9. Do-Now—团队Scrum 冲刺博客五

    各个成员今日完成的任务 侯泽洋: 每日任务页面编写,任务修改功能 周亚杰:完成个人中心页面设计 王志伟:完成个人中心页面设计 唐才铭:启动动画及引导页与项目合并 项目燃尽图 站立式会议照片 各个成员遇 ...

  10. 关于ajax 返回值验证问题

    如果后台返回布尔值true时 前端 if(data==true){    //true 不能加引号  否则就变成了字符串了 alert(data+'操作成功!'+status); }