GraphicsMagick+NGINX生成缩略图

1.安装GraphicsMagick

wget http://ftp.icm.edu.pl/pub/unix/graphics/wget http://ftp.icm.edu.pl/pub/unix/graphics/GraphicsMagick/1.3/GraphicsMagick-1.3.18.tar.gz

tar  -xvf   GraphicsMagick-1.3.18.tar.gz
cd GraphicsMagick-1.3.18

./configure      -enable-shared -enable-lzw -without-perl -with-modules
make 
make install

gm version 出现下面的信息
GraphicsMagick 1.3.18 2013-03-10 Q8 http://www.GraphicsMagick.org/
Copyright (C) 2002-2013 GraphicsMagick Group.
Additional copyrights and licenses apply to this software.
See http://www.GraphicsMagick.org/www/Copyright.html for details.

Feature Support:
  Thread Safe              yes
  Large Files (> 32 bit)   yes
  Large Memory (> 32 bit)  yes
  BZIP                     no
  DPS                      no
  FlashPix                 no
  FreeType                 no
  Ghostscript (Library)    no
  JBIG                     no
  JPEG-2000                no
  JPEG                     no
  Little CMS               no
  Loadable Modules         no
  OpenMP                   yes (201107)
  PNG                      no
  TIFF                     no
  TRIO                     no
  UMEM                     no
  WMF                      no
  X11                      no
  XML                      no
  ZLIB                     yes

Host type: x86_64-unknown-linux-gnu

Configured using the command:
  ./configure 

Final Build Parameters:
  CC       = gcc -std=gnu99
  CFLAGS   = -fopenmp -g -O2 -Wall -pthread
  CPPFLAGS = 
  CXX      = g++
  CXXFLAGS = -pthread
  LDFLAGS  = 
  LIBS     = -lz -lm -lgomp -lpthread

如果 png jpg 都是no 需要添加支持

yum install -y libpng-devel libpng
yum install -y  libjpeg-devel libjpeg
#freetype 支持
yum -y install libjpeg libjpeg-devel libpng libpng-devel giflib giflib-devel freetype freetype-devel

./configure     -enable-shared -enable-lzw -without-perl -with-modules
make 
make install

2.安装LuaJIT

安装LuaJIT
下载  
wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz  
tar -xvf   LuaJIT-2.0.4.tar.gz  
cd  LuaJIT-2.0.4
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0

3.分别下载和解压 lua-nginx-module-0.8.10.tar.gz 和 ngx_devel_kit-0.2.18.tar.gz

 wget -O lua-nginx-module-0.8.10.tar.gz https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.10.tar.gz 
 wget -O ngx_devel_kit-0.2.18.tar.gz https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz 
 tar -xvf  lua-nginx-module-0.8.10.tar.gz
 tar -xvf  ngx_devel_kit-0.2.18.tar.gz 

4.下载安装 nginx

 wget http://nginx.org/download/nginx-1.4.2.tar.gz
 tar -xvf  nginx-1.4.2.tar.gz
 cd  nginx-1.4.2
 ./configure  --add-module=/root/fastdfs/fastdfs-nginx-module-1.20/src --add-module=/root/fastdfs/lua-nginx-module-0.8.10 --add-module=/root/fastdfs/ngx_devel_kit-0.2.18 
 
ln -s /usr/local/lib/libluajit-5.1.so.2 /usr/lib/libluajit-5.1.so.2
cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
echo “/usr/local/lib” >> /etc/ld.so.conf
ldconfig

/usr/local/nginx/sbin/nginx -V

vim /usr/local/nginx/conf/nginx.conf

     server {
        listen       8888;
        server_name  localhost;
        #location ~/group[0-9]/ {
        #    ngx_fastdfs_module;
        #}
        location / {
             root /home/fastdfs/data/00/00;
        }
        location /test {
            default_type text/html;
            content_by_lua '
            ngx.say("hello world")
            ngx.log(ngx.ERR,"err err")
            ';
        }
         # fastdfs 缩略图生成
        location ~/group[0-9]/M00 {
                alias /home/fastdfs/data;

                set $image_root "/home/fastdfs/data";
                if ($uri ~ "/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/(.*)") {
                  set $image_dir "$image_root/$3/$4/";
                  set $image_name "$5";
                  set $file "$image_dir$image_name";
                }

                if (!-f $file) {
                  # 关闭lua代码缓存,方便调试lua脚本
                  #lua_code_cache off;
                  content_by_lua_file  conf/img_crop.lua;
                }
                ngx_fastdfs_module;
        }

    }


img_crop.lua 脚本

-- 写入文件
local function writefile(filename, info)
    local wfile=io.open(filename, "w") --写入文件(w覆盖)
    assert(wfile)  --打开时验证是否出错		
    wfile:write(info)  --写入传入的内容
    wfile:close()  --调用结束后记得关闭
end

-- 检测路径是否目录
local function is_dir(sPath)
    if type(sPath) ~= "string" then return false end

    local response = os.execute( "cd " .. sPath )
    if response == 0 then
        return true
    end
    return false
end

-- 检测文件是否存在
local file_exists = function(name)
    local f=io.open(name,"r")
    if f~=nil then io.close(f) return true else return false end
end

local area = nil
local originalUri = ngx.var.uri;
local originalFile = ngx.var.file;
local index = string.find(ngx.var.uri, "([0-9]+)x([0-9]+)");  
if index then 
    originalUri = string.sub(ngx.var.uri, 0, index-2);  
    area = string.sub(ngx.var.uri, index);  
    index = string.find(area, "([.])");  
    area = string.sub(area, 0, index-1);  

    local index = string.find(originalFile, "([0-9]+)x([0-9]+)");  
    originalFile = string.sub(originalFile, 0, index-2)
end

-- check original file
if not file_exists(originalFile) then
    local fileid = string.sub(originalUri, 2);
    -- main
    local fastdfs = require('restyfastdfs')
    local fdfs = fastdfs:new()
    fdfs:set_tracker("192.168.211.136", 22122)
    fdfs:set_timeout(1000)
    fdfs:set_tracker_keepalive(0, 100)
    fdfs:set_storage_keepalive(0, 100)
    local data = fdfs:do_download(fileid)
    if data then
       -- check image dir
        if not is_dir(ngx.var.image_dir) then
            os.execute("mkdir -p " .. ngx.var.image_dir)
        end
        writefile(originalFile, data)
    end
end

-- 创建缩略图
local image_sizes = {"80x80", "800x600", "40x40", "60x60"};  
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(image_sizes, area) then  
    local command = "gm convert " .. originalFile  .. " -thumbnail " .. area .. " -background gray -gravity center -extent " .. area .. " " .. ngx.var.file;  
    os.execute(command);  
end;

if file_exists(ngx.var.file) then
    --ngx.req.set_uri(ngx.var.uri, true);  
    ngx.exec(ngx.var.uri)
else
    ngx.exit(404)
end

改变目录的权限
chmod -R 777 /home/fastdfs/data/

启动nginx 测试

http://192.168.211.136:8888/test
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/fastdfs/1.png
将返回的路径使用 nginx 访问 我的访问路径是 
http://192.168.211.136:8888/group1/M00/00/00/wKjTiF8HDKSANWomAACGZa9JdFo395.png
http://192.168.211.136:8888/wKjTiF8HDKSANWomAACGZa9JdFo395.png
http://192.168.211.136:8888/wKjTiF8HDKSANWomAACGZa9JdFo395.png_300x200.png

猜你喜欢

转载自blog.csdn.net/ko0491/article/details/108991087