tenginx+ImageMagick+Lua自动缩略图

简介:

      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.测试切图的效果

wKioL1fSbn-wSVOnAAB3rd3q624523.png-wh_50

wKiom1fSbdXQT-FdAAMNvzvnqws249.png-wh_50

wKiom1fSbdbB9BH6AAFdLiGt4gU438.png-wh_50

wKioL1fSbdeCAIvoAAL6GX5FC-4966.png-wh_50










本文转自 z597011036 51CTO博客,原文链接:http://blog.51cto.com/tongcheng/1717372,如需转载请自行联系原作者
目录
相关文章
|
4月前
|
小程序 PHP 数据安全/隐私保护
php图片加水印函数
这里分享下php给图片加水印的几个自定义函数 给图片加水印首先需要开启GD库。 用到的php函数是imagecopymerge () 和 imagecopy () imagecopymerge 函数可以支持两个图像叠加时,设置叠加的透明度
45 0
|
PHP 数据安全/隐私保护
PHP利用GD库给图片添加水印
利用GD库给图片添加水印
420 0
|
文字识别 计算机视觉 Python
python自动制作gif并添加文字
引言     最近租的房子快到期了,哎,因为去年是第一次找房子租,结果遇到了一个东北黑中介,押一付三,房子有啥问题,灯坏了,下水道堵了,原来签合同的时候说的客气,说是马上就会上门解决,结果实际上我每次找他,都是各种推脱,最后不了了之。
2235 0
|
前端开发 Ruby 定位技术
|
C# 图形学 C++
修改Unity中Lua文件的默认打开程序
项目中引用了XLua,而Lua文件又是以txt文件结尾的,当修改系统的扩展脚本编辑器为vs后双击lua文件(xx.txt)默认也使用vs打开了,无提示的黑白文本编辑     昨办?   ….   后来看到网上有写Unity的插件,想着应该也能判断后缀名然后调用指定的编辑器,果然可以。
2109 0

热门文章

最新文章