tenginx使用lua脚本调用GraphicsMagick软件的gm工具对原图进行切图,用户输出指定的参数就可以显示出相应大小的图片.
1.系统必须安装jpg,png,zlib库文件
[root@node1 ~]# yum install libpng libpng-devel libjpeg libjpeg-devel zlib zlib-deve perl prel-devel freetype freetype-devel ghostscript ghostscript-devel -y
2.下载安装ImageMagick
[root@node1 ~]# tar xvf GraphicsMagick-1.3.23.tar.gz
[root@node1 ~]# cd GraphicsMagick-1.3.23
[root@node1 GraphicsMagick-1.3.23]# ./configure --prefix=/usr/local/GraphicsMagick-1.3.23 --enable-shared
ZLIB --with-zlib=yes yes --这三个扩展必须是yes,否则转换图处理会报错
JPEG v1 --with-jpeg=yes yes
PNG --with-png=yes yes
Ghostscript None gs (8.70) --必须启用,否则合并图会报错
[root@node1 GraphicsMagick-1.3.23]# make && make install
[root@node1 GraphicsMagick-1.3.23]# vim /etc/ld.so.conf.d/GraphicsMagick.conf
include /usr/local/GraphicsMagick-1.3.23/lib/ --添加一行
[root@node1 GraphicsMagick-1.3.23]# ll /usr/local/GraphicsMagick-1.3.23/bin
total 328
-rwxr-xr-x 1 root root 6498804 Nov 30 2015 gm
-rwxr-xr-x 1 root root 1332 Nov 30 2015 GraphicsMagick-config
-rwxr-xr-x 1 root root 1322 Nov 30 2015 GraphicsMagick++-config
-rwxr-xr-x 1 root root 1295 Nov 30 2015 GraphicsMagickWand-config
[root@node1 GraphicsMagick-1.3.23]#
3.下载安装tenginx [root@node1 ~]# yum install lua* --安装lua脚本 [root@node1 ~]# wget ftp://ftp.csx.cam.ac.uk/pub/soft ... re/pcre-8.38.tar.gz [root@node1 ~]# tar xvf pcre-8.38.tar.gz [root@node1 ~]# mv pcre-8.38 /usr/local/ [root@node1 ~]# wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz [root@node1 ~]# wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz [root@node1 ~]# tar xvf LuaJIT-2.0.4.tar.gz [root@node1 ~]# cd LuaJIT-2.0.4 [root@node1 ~]# make && make install [root@node1 ~]# tar xvf tengine-2.1.0.tar.gz [root@node1 ~]# cd tengine-2.1.0[root@node1 tengine-2.1.0]# --prefix=/usr/local/tenginx-2.1.0 --with-file-aio --with-poll_module --with-pcre=/usr/local/pcre-8.37 --without-http_autoindex_module --without-http_ssi_module --without-mail_pop3_module --without-mail_smtp_module --without-mail_imap_module --with-http_lua_module --with-ld-opt=-Wl,-rpath,/usr/local/lib --with-http_realip_module --with-http_concat_module --add-module=/root/ngx_cache_purge-1.6 --add-module=/root/fastdfs-nginx-module/src [root@node1 tengine-2.1.0]# make && make install [root@node1 ~]# vim /usr/local/tenginx-2.1.0/conf/nginx.conf location /lua { default_type 'text/plain'; content_by_lua 'ngx.say("hello,lua")'; } location /upload { set $image_root /usr/local/tenginx-2.1.0/html; set $file "$image_root$uri"; if (!-f $file) { rewrite_by_lua ' local index = string.find(ngx.var.uri, "([0-9]+)x([0-9]+)"); if (index == nil) then ngx.exit(404); end local originalUri = string.sub(ngx.var.uri, 0, index-2); local area = string.sub(ngx.var.uri, index); index = string.find(area, "([.])"); area = string.sub(area, 0, index-1); local image_sizes = {"160x160","400x400","100x100","200x200","300x300"}; local image_big = {"80x60^", "120x90^", "160x120^"}; function table.contains(table, element) for _, value in pairs(table) do if value == element then return true end end return false end print(table.contains(image_big, area)) if table.contains(image_big, area) then local command = "/usr/local/GraphicsMagick-1.3.23/bin/gm convert " .. ngx.var.image_root .. originalUri .. " -thumbnail " .. area .. " -background gray -gravity center -extent " .. area .. " " .. ngx.var.image_root .. ngx.var.uri os.execute(command); else if table.contains(image_sizes, area) then local command = "/usr/local/GraphicsMagick-1.3.23/bin/gm convert " .. ngx.var.image_root .. originalUri .. " -thumbnail " .. area .. " " .. ngx.var.image_root .. ngx.var.uri; os.execute(command); else ngx.exit(404); end; end; '; } } [root@node1 ~]# |
4.测试切图的效果