GraphicsMagick+lua-nginx-module+nginx=实时缩略图服务

查看评论

GraphicsMagick

A:\>gm convert 12.png -thumbnail 100x100^ -gravity center -extent 100x100 -quality50 output_4.jpg

wget http://luajit.org/download/LuaJIT-2.0.0-beta10.tar.gz
tar vxzf LuaJIT-2.0.0-beta10.tar.gz 
cd LuaJIT-2.0.0-beta10
make&&makeinstallPREFIX=/usr/local/lj2
ln-sf luajit-2.0.0-beta10 /usr/local/lj2/bin/luajit
 
#下载最新的 ngx_devel_kit (NDK) 模块cd/tmp/wget http://github.com/simpl/ngx_devel_kit/tarball/v0.2.17rc2
tar vxzf v0.2.17rc2 
ls simpl-ngx_devel_kit-bc97eea
 
#下载最新的lua-nginx-module 模块wget http://github.com/chaoslawful/lua-nginx-module/zipball/v0.5.8rc1
unzip v0.5.8rc1 
ls chaoslawful-lua-nginx-module-a624ee9
 
cd/tmp
wget'http://nginx.org/download/nginx-1.0.10.tar.gz'tar-xzvf nginx-1.0.10.tar.gz
cd nginx-1.0.10/# 或者设置 LuaJIT 环境变量exportLUAJIT_LIB=/usr/local/lj2/lib
exportLUAJIT_INC=/usr/local/lj2/include/luajit-2.0# 这里假设 Nginx 安装路径是 /usr/local/nginx/#./configure --prefix=/usr/local/nginx \--add-module=/tmp/simpl-ngx_devel_kit-bc97eea \--add-module=/tmp/chaoslawful-lua-nginx-module-a624ee9
 
#./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6
 
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module  --with-http_gzip_static_module \--add-module=/tmp/simpl-ngx_devel_kit-bc97eea \--add-module=/tmp/chaoslawful-lua-nginx-module-a624ee9
 
#2代表cpu核数make-j4makeinstall[root@ghost-rider lib]# /etc/init.d/nginx restart
Restarting nginx daemon: nginxcat: /usr/local/nginx/logs/nginx.pid: No such file or directory
kill: usage: kill[-s sigspec |-n signum | -sigspec] pid | jobspec ... or kill-l[sigspec]
nginx not running/usr/local/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
nginx already running.
 
[root@ghost-rider ~]# ldd /usr/local/nginx/sbin/nginx 
	linux-vdso.so.1 =>(0x00007fffeabff000)
	libpthread.so.0 =>/lib64/libpthread.so.0 (0x0000003a20600000)
	libcrypt.so.1 =>/lib64/libcrypt.so.1 (0x0000003a2b600000)
	libluajit-5.1.so.2 => not found
	libm.so.6 =>/lib64/libm.so.6 (0x0000003a20a00000)
	libpcre.so.0 =>/usr/local/lib/libpcre.so.0 (0x00007f4bf0971000)
	libcrypto.so.10 =>/usr/lib64/libcrypto.so.10 (0x000000301f200000)
	libz.so.1 =>/lib64/libz.so.1 (0x000000301a200000)
	libc.so.6 =>/lib64/libc.so.6 (0x0000003a20200000)/lib64/ld-linux-x86-64.so.2 (0x0000003a1fa00000)
	libfreebl3.so =>/usr/lib64/libfreebl3.so (0x0000003a2de00000)
	libdl.so.2 =>/lib64/libdl.so.2 (0x0000003a1fe00000)[root@ghost-rider ~]# /lib
lib/   lib64/[root@ghost-rider ~]# /lib
lib/   lib64/[root@ghost-rider ~]# /lib
lib/   lib64/[root@ghost-rider ~]# /lib64/lj^C[root@ghost-rider ~]# cp /usr/local/lj2/lib/l
libluajit-5.1.a         libluajit-5.1.so        libluajit-5.1.so.2      libluajit-5.1.so.2.0.0  lua/[root@ghost-rider ~]# cp /usr/local/lj2/lib/libluajit-5.1.so.2 
anaconda-ks.cfg                 core.1350                       .gconfd/                        packages/                       vhost.sh
.bash_history                   .cshrc                          .gem/                           .pki/                           weedfs/
.bash_logout                    D                               .gitconfig                      post-install                    weedfs.0.14.linux_amd64.tar.gz
.bash_profile                   .dbshell                        .gitolite/                      post-install.log                
.bashrc                         fix_volume_index                .htoprc                         .ssh/                           
?b.                           .gconf/                         lnmp                            .tcshrc                         
[root@ghost-rider ~]# cp /usr/local/lj2/lib/libluajit-5.1.so.2 /lib64/[root@ghost-rider ~]# ldd /usr/local/nginx/sbin/nginx 
	linux-vdso.so.1 =>(0x00007fff8d9ff000)
	libpthread.so.0 =>/lib64/libpthread.so.0 (0x0000003a20600000)
	libcrypt.so.1 =>/lib64/libcrypt.so.1 (0x0000003a2b600000)
	libluajit-5.1.so.2 =>/lib64/libluajit-5.1.so.2 (0x00007f0d3bbb4000)
	libm.so.6 =>/lib64/libm.so.6 (0x0000003a20a00000)
	libpcre.so.0 =>/usr/local/lib/libpcre.so.0 (0x00007f0d3b999000)
	libcrypto.so.10 =>/usr/lib64/libcrypto.so.10 (0x000000301f200000)
	libz.so.1 =>/lib64/libz.so.1 (0x000000301a200000)
	libc.so.6 =>/lib64/libc.so.6 (0x0000003a20200000)/lib64/ld-linux-x86-64.so.2 (0x0000003a1fa00000)
	libfreebl3.so =>/usr/lib64/libfreebl3.so (0x0000003a2de00000)
	libdl.so.2 =>/lib64/libdl.so.2 (0x0000003a1fe00000)
	libgcc_s.so.1 =>/lib64/libgcc_s.so.1 (0x0000003a2c200000)[root@ghost-rider nginx-1.0.10]# /usr/local/nginx/sbin/nginx -v
nginx: nginx version: nginx/1.0.10

或者使用openresty安装包,集成好了nginx各种组件(还是换这种靠谱)。

yum install readline-devel pcre-devel openssl-devel
cd/tmp
wget http://agentzh.org/misc/nginx/ngx_openresty-1.0.15.10.tar.gz
tar vxzf ngx_openresty-1.0.15.10.tar.gz 
cd ngx_openresty-1.0.15.10
./configure --user=www --group=www --prefix=/usr/local/openresty \--with-luajit \
            --without-http_redis2_module \
            --with-http_iconv_module 
#如果是多核,假设是4核make-j4makeinstall
 
ce /tmp
wget http://downloads.sourceforge.net/project/graphicsmagick/graphicsmagick/1.3.16/GraphicsMagick-1.3.16.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fgraphicsmagick%2Ffiles%2Fgraphicsmagick%2F1.3.16%2F&ts=1343298549&use_mirror=cdnetworks-kr-1tar vxzf GraphicsMagick-1.3.16.tar.gz 
cd GraphicsMagick-1.3.16
./configure
make&&makeinstall

错误处理

nginx 404 failed (2: No such file or directory)
nginx 403 forbidden,检查nginx启动用户是否有对站点目录的读和执行权限。nginx.conf第一行,可以通过设置user www www来指定执行的用户。

gm convert /home/wwwroot/st.xxx/p_w_picpaths/xxxxx.jpg.80x80.jpg -thumbnail 100x100 -background gray -gravity center -extent 100x100 output.jpg;

http://www.graphicsmagick.org/GraphicsMagick.html#details-thumbnail

nginx.con配置

   location /p_w_picpaths/{set$p_w_picpath_root/home/wwwroot/st.xxx;
 set$file"$p_w_picpath_root$uri";
 if(!-f $file){   rewrite_by_lua '                      local index = string.find(ngx.var.uri, "([0-9]+)x([0-9]+)");                      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 p_w_picpath_sizes = {"80x80", "800x600", "40x40"};                      function table.contains(table, element)                         for _, value in pairs(table) do                            if value == element then                               return true                            end                         end                         return false                      end                       if table.contains(p_w_picpath_sizes, area) then                         local command = "gm convert " .. ngx.var.p_w_picpath_root ..  originalUri  .. " -thumbnail " .. area .. " -background gray -gravity center -extent " .. area .. " " .. ngx.var.file;                         os.execute(command);                      else                         ngx.exit(404);                      end;                   ';}alias/home/wwwroot/st.xxx/p_w_picpaths/;
                expires 7d;}

上图:

后端获取文件切换成weed-fs的反向代理,本地文件存储路径再规划一下,或者本地不存,前端加一层varnish,自动处理过期静态缓存,这样整个架构基本完成了。

http://wiki.nginx.org/HttpLuaModuleZh

http://www.lua.org/

http://luajit.org/install.html

https://github.com/chaoslawful/lua-nginx-module/

http://www.iteye.com/topic/1125126?page=2

http://www.graphicsmagick.org/GraphicsMagick.html#details-thumbnail

本文来自: GraphicsMagick+lua-nginx-module+nginx=实时缩略图服务





http://gi1.md.alicdn.com/bao/uploaded/i1/T1qPN_FxRaXXXXXXXX_!!0-item_pic.jpg_460x460q90.jpg