实验环境:CentOS 6.10

目标:1.使用http-image-filter-module进行图片变换;2.使用lua进行格式转换;

  1. 安装EPEL

    https://fedoraproject.org/wiki/EPEL

    yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
    # or RHEL/CentOS 7
    # yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
  2. 由于lua-nginx-module声称(2018-12-06)测试的最新nginx稳定版本是1.12.x(参考:https://github.com/openresty/lua-nginx-module#nginx-compatibility),所以选择安装nginx版本为1.13.6

    curl -O http://nginx.org/download/nginx-1.12.2.tar.gz
    yum install -y gcc g++ gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel
    yum install -y nginx-1.12.2
    yum install -y libjpeg-turbo-devel libpng-devel freetype-devel libtiff-devel libXpm-devel libwebp-devel fontconfig-devel
  3. libimagequant && libgd && add http-image-filter-module

    https://github.com/libgd/libgd

    https://github.com/ImageOptim/libimagequant

    cd libimagequant-2.12.2
    ./configure --prefix=/usr
    make libimagequant.so
    make install
    > install -d /usr/lib
    > install -d /usr/lib/pkgconfig
    > install -d /usr/include
    > install -m 644 libimagequant.a /usr/lib/libimagequant.a
    > install -m 644 libimagequant.so.0 /usr/lib/libimagequant.so.0
    > ln -sf libimagequant.so.0 /usr/lib/libimagequant.so
    > install -m 644 imagequant.pc /usr/lib/pkgconfig/imagequant.pc
    > install -m 644 libimagequant.h /usr/include/libimagequant.h
    #
    cd ../libgd-2.2.5
    ./configure --with-liq=/opt/src/libimagequant-2.12.2
    make
    make install
    gdlib-config --all
    > gdlib-config: warning: this script is deprecated; please use the pkg-config file instead.
    > GD library 2.2.5
    > includedir: /usr/local/include
    > cflags: -I/usr/local/include
    > ldflags:
    > libs: -lm -lz -lpng12 -lfreetype -lfontconfig -ljpeg -limagequant -fopenmp > -lXpm -lX11 -ltiff -lwebp
    > libdir: /usr/local/lib
    > features: GD_GIF GD_GIFANIM GD_OPENPOLYGON GD_ZLIB GD_PNG GD_FREETYPE GD_FONTCONFIG GD_JPEG GD_LIQ GD_XPM GD_TIFF GD_WEBP
    ln -s /usr/local/lib/libgd.so.3.0.5 /usr/lib64/libgd.so
    ln -s /usr/local/lib/libgd.so.3.0.5 /usr/lib64/libgd.so.3
    ln -s /usr/lib/libimagequant.so.0 /usr/lib64/libimagequant.so.0
    #
    cd nginx-1.12.2
    # 已经安装 yum install -y nginx-1.12.2,所以通过nginx -V获取原编译参数,再加上--with-http_image_filter_module重新编译
    nginx -V
    ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-http_image_filter_module
    > ...
    > checking for GD library ... found
    > checking for GD WebP support ... found
    > ...
    make
    cd objs
    cp /usr/sbin/nginx /usr/sbin/nginx.old.20181206
    /cp nginx /usr/sbin/nginx -f
    nginx -V
  4. 测试nginx

     location ~* ^/img/.*_(\d+)x(\d+)\.(jpg|JPG|gif|GIF|png|PNG)$ {
    set $img_width $1;
    set $img_height $2;
    rewrite ^(.*)_(\d+)x(\d+)\.(jpg|JPG|gif|GIF|png|PNG)$ $1.$4 break;
    image_filter resize $img_width $img_height;
    image_filter_buffer 10M;
    }
  5. 测试image_filter是否支持webp

    yum install -y libwebp-tools
    cwebp xxx.jpg -o xxx.webp
    curl -I http://192.168.178.130/img/288822bcfbcbdea89eaa9b84cc42f562_500x500.webp
  6. 测试image_filter + proxy_pass

    location ~* ^/oss/(.*)_(\d+)x(\d+)\.(jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG|webp|WEBP)$ {
    set $img_width $2;
    set $img_height $3;
    image_filter resize $img_width $img_height;
    image_filter_buffer 10M;
    resolver 114.114.114.114;
    proxy_pass http://image-demo.oss-cn-hangzhou.aliyuncs.com/$1.$4;
    # 借用oss的示例图片 https://help.aliyun.com/document_detail/44688.html
    }
  7. LUAJIT

    # http://luajit.org/download.html
    curl -O http://luajit.org/download/LuaJIT-2.0.5.tar.gz
    tar -zxvf LuaJIT-2.0.5.tar.gz
    cd LuaJIT-2.0.5
    make PREFIX=/usr/local/luajit
    make install PREFIX=/usr/local/luajit
    vi /etc/profile
    >> export LUAJIT_LIB=/usr/local/luajit/lib
    >> export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
    source /etc/profile
  8. ngx_devel_kit (NDK) && lua-nginx-module

    # https://github.com/openresty/lua-nginx-module/releases
    tar -zxvf lua-nginx-module-0.10.13.tar.gz
    # https://github.com/simpl/ngx_devel_kit/releases
    tar -zxvf ngx_devel_kit-0.3.0.tar.gz
  9. 编译安装nginx

    ./configure --prefix ...省略... --with-http_image_filter_module --add-module=/opt/src/ngx_devel_kit-0.3.0 --add-module=/opt/src/lua-nginx-module-0.10.13
    make
    cd objs
    cp /usr/sbin/nginx /usr/sbin/nginx.old.20181215
    /bin/cp nginx /usr/sbin/nginx -f
    ln -s /usr/local/luajit/lib/libluajit-5.1.so.2.0.5 /usr/lib64/libluajit-5.1.so.2
    service nginx restart
    # 验证Nginx是否链接了libluajit-5.1.so.2
    ldd /usr/sbin/nginx | grep lua
  10. hello,lua

    location /lua_hello {
    default_type 'text/plain';
    content_by_lua 'ngx.say('hello,lua!')';
    }
    location /lua_hello2 {
    default_type 'text/plain';
    content_by_lua_block {
    ngx.say("hello,world!");
    ngx.log(ngx.ERR, "hello");
    }
    }
  11. 用lua来做代理

    location ~* ^/lua_capture/(.*)_(\d+)x(\d+)\.(jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG|webp|WEBP)$ {
    default_type 'text/plain';
    content_by_lua_block {
    local proxy_url = "/proxy/" .. ngx.var[1] .. "." .. ngx.var[4]
    ngx.log(ngx.ERR, proxy_url)
    local resp = ngx.location.capture(proxy_url, {
    method = ngx.HTTP_GET,
    args = {q="hello"}
    })
    if not resp then
    ngx.say("request error:", err);
    return
    end
    ngx.log(ngx.ERR, tostring(resp.status)) ngx.status = resp.status for k, v in pairs(resp.header) do
    if k ~= "Transfer-Encoding" and k ~= "Connection" then
    ngx.header[k] = v
    end
    end if resp.body then
    ngx.say(resp.body)
    end
    }
    } location ~ ^/proxy/(.*)$ {
    # internal;
    resolver 114.114.114.114;
    proxy_pass http://image-demo.oss-cn-hangzhou.aliyuncs.com/$1;
    }
  12. todo: 如何实现实时转webp呢?

参考:

  1. nginx模块 ngx_http_image_filter_module
  2. ngx.location.capture 只支持相对路径,不能用绝对路径
  3. 利用 Aliyun OSS Nginx proxy module 实现OSS 图片处理回写功能
  4. 大型网站高并发解决方案分析之图片服务器分离架构
  5. WebP(整理),比较了WebP和PNG

nginx图片处理笔记(http-image-filter-module、lua)的更多相关文章

  1. nginx图片过滤处理模块http_image_filter_module安装配置笔记

    http_image_filter_module是nginx提供的集成图片处理模块,支持nginx-0.7.54以后的版本,在网站访问量不是很高磁盘有限不想生成多余的图片文件的前提下可,就可以用它实时 ...

  2. nginx图片过滤处理模块http_image_filter_module

    nginx图片过滤处理模块http_image_filter_module安装配置笔记 http_image_filter_module是nginx提供的集成图片处理模块,支持nginx-0.7.54 ...

  3. nginx图片过滤处理模块http_image_filter_module安装配置

    http_image_filter_module是nginx提供的集成图片处理模块,支持nginx-0.7.54以后的版本,在网站访问量不是很高磁盘有限不想生成多余的图片文件的前提下可,就可以用它实时 ...

  4. Windows下搭建Nginx图片服务器

    在项目最开始,上传图片的时候,服务器先保存原图再使用ImageMagick生成上传图片缩略图,这种方法有很多缺点,例如生成的缩略图的大小是固定的,不能动态请求指定大小的缩略图. 虽然有非常多的图片云存 ...

  5. nginx图片处理

    前言 不管一个系统或网站的大与小,都存在相应的图片处理,生成缩略图.为图片加水印等等,如果涉及到APP端,这个图片的处理需求变得更加重要了,因为在目前看来,客户端的屏幕大小不一,会导致以下问题: 1. ...

  6. nginx 配置rewrite 笔记

    nginx 配置rewrite笔记: 通过下面的示例来说明一下,1. 先说说location : location 表示匹配传入的url地址,其中配置符有多种,各种情况的意义不一样: location ...

  7. 搭建Nginx图片服务器

    搭建Nginx图片服务器 Part-I 安装Nginx 安装PCRE 下载 ngx_cache_purge 并解压,用来清除缓存 下载Nginx并解压 cd nginx-1.7.7 编译,--pref ...

  8. 用nginx图片缓存服务器

    图片的存储硬件 把图片存储到什么介质上? 如果有足够的资金购买专用的图片服务器硬件或者 NAS 设备,那么简单的很: 如果上述条件不具备,只想在普通的硬盘上存储,首先还是要考虑一下物理硬盘的实际处理能 ...

  9. Nginx 图片服务器

    文件服务器:后台如果是集群,每次请求都会到不同的服务器,所以每台服务器的图片文件等都要做同步处理,才能保证每次用户不管访问到哪台服务器都能获取一样的资源.这种做法开销会很大,专门使用 nginx 作为 ...

随机推荐

  1. FIREDAC(DELPHI10 or 10.1)提交数据给ORACLE数据库的一个不是BUG的BUG

    发现FIREDAC(DELPHI10 or 10.1)提交数据给ORACLE数据库的一个不是BUG的BUG,提交的表名大小写是敏感的. 只要有一个表名字母的大小写不匹配,ORACLE就会认为是一个不认 ...

  2. delphi FastReport 安装方法

    (最近记忆力真的不行了,装了很多遍,过段时间重装delphi又不记得了,又要折腾,现在先记录下来,留给下次翻) 1.下载安装包,这里提供一个百度云盘共享链接,版本为fastreport5: https ...

  3. .NET框架源码解读之启动CLR

    前面提到在SSCLI环境里运行.NET程序的时候,执行的命令类似java程序的执行过程,即通过clix程序解释执行.net程序.这个过程看起来跟在windows环境下执行.net程序表面上看起来不一样 ...

  4. osgi.net框架

    osgi.net是一个动态的模块化框架.它向用户提供了模块化与插件化.面向服务构架和模块扩展支持等功能.该平台是OSGi联盟定义的服务平台规范移植到.NET的实现. 简介 尤埃开放服务平台是一个基于. ...

  5. driver.get()和driver.navigate().to()到底有什么不同?-----Selenium快速入门(四)

    大家都知道,这两个方法都是跳转到指定的url地址,那么这两个方法有什么不同呢?遇到这种情况,第一反应就是查查官方的文档. 官方文档的说法是:Load a new web page in the cur ...

  6. C# 判断质数的2种基本方法

    质数(prime number)又称素数,有无限个. 质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数. 目前学习了判断数字n是否为质数的2种基本方法: 一.计数法 根据定义,既然质数只 ...

  7. ASP.NET基于Aspose.Words插入Word水印以及多个水印

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...

  8. AI-Info-Micron:人如其食:人工智能和人类微生物组

    ylbtech-AI-Info-Micron:人如其食:人工智能和人类微生物组 1.返回顶部 1. 人如其食:人工智能和人类微生物组 “相信你身体发出的信号”,的确是一个很好的建议.研究人员在不遗余力 ...

  9. 63. 不同路径 II leetcode JAVA

    题目 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 现在 ...

  10. HDP Spark2 HIVE3.1 的问题

    HDP 上安装了 Hive3.1 和 Spark2, 提交 Spark 作业时,报找不到 Hive 中表的问题 但是查一了下 hive 表,明明是存在这个表的.查看日志,注意到如下的一段日志. 没修改 ...