OpenResty+lua+GraphicsMagick生成缩略图

1、安装GraphicsMagick

下载地址:http://www.graphicsmagick.org/

安装支持包:下载地址:ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/delegates/

直接上传

libjpeg-6b.tar.gz

libpng-1.6.16.tar.gz

到/usr/local/software下。

tar zxvf libjpeg-6b.tar.gz

cd cd libjpeg-6b

./configure

make

make install

tar zxvf libpng-1.6.16.tar.gz

./configure

make

make install

tar zxvf GraphicsMagick-1.3.20.tar.gz

cd GraphicsMagick-1.3.20

./configure

make

make install

安装依赖包Ghostscript,不安装的话加水印会找不到字体

yum install -y ghostscript

为目标加权限

chmod 777 /usr/local/openresty/nginx/html/down/PersonImg -R

2、配置nginx.conf

location /down/PersonImg

{

set $image_root /usr/local/openresty/nginx/html;

set $file "$image_root$uri";

#判断目标文件是不是存在?

if (!-f $file)

{

rewrite_by_lua

'

ngx.header.content_type = "text/plain;charset=utf-8"

local index = string.find(ngx.var.uri, "([0-9]+)w_([0-9]+)h_");

if(index==nil) then return

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);

index = string.find(area, "w_");

local w=string.sub(area,0,index-1);

local index_h = string.find(area, "h_");

local h=string.sub(area,index+2,index_h-1);

area=tostring(w).."x"..tostring(h);

local image_sizes = {"140x140", "800x800", "90x90"};

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 = "/usr/local/bin/gm convert " .. ngx.var.image_root .. originalUri .. " -thumbnail " .. area .. " -gravity center -extent " .. area .. " " .. ngx.var.file;

os.execute(command);

else

ngx.exit(401)

end

';

}

alias /usr/local/openresty/nginx/html/down/PersonImg;

}

3、

放文件:

/usr/local/openresty/nginx/html/down/PersonImg/3AFAE457-FDC8-27D3-E2B5-6C10A145A3A6.jpg

访问

http://10.10.3.178/down/PersonImg/3AFAE457-FDC8-27D3-E2B5-6C10A145A3A6.jpg@140w_140h_100q_1x.jpg

4、要加水印的话命令如下

/usr/local/GraphicsMagick/bin/gm convert /usr/local/openresty/nginx/html/down/PersonImg/3AFAE457-FDC8-27D3-E2B5-6C10A145A3A6.jpg -font ArialBold -pointsize 45 -fill red -draw "text 10,10 dsideal" /usr/local/openresty/nginx/html/down/PersonImg/3AFAE457-FDC8-27D3-E2B5-6C10A145A3A61111.jpg