1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Nginx-Lua-FastDFS-GraphicsMagick动态图片缩略图

Nginx-Lua-FastDFS-GraphicsMagick动态图片缩略图

时间:2020-12-27 11:12:04

相关推荐

Nginx-Lua-FastDFS-GraphicsMagick动态图片缩略图

Nginx-Lua-FastDFS-GraphicsMagick动态图片缩略图

公司使用FastDFS来当作图片服务器,客户端通过Nginx请求图片。某些情况下,客户端对请求的图片有尺寸要求,

如http://10.100.1.145/group1/M00/00/18/CmQBkVsSjbiARQKhAAAHLpECb3c407.jpg_400x400.jpg。

这时我们可以使用GraphicsMagick工具动态的修改图片以满足客户端的需求。

这里使用lua脚本,调用GraphicsMagick的gm命令动态处理图片。

FastDFS+Nginx的安装配置步骤请见:/danielzhou888/article/details/80563355

安装包来源请自行百度,谢谢。

整体思路:

1、首先服务器需要有lua环境

2、Nginx扩展支持lua,可调用lua脚本

3、lua脚本中定义gm命令及参数

安装配置:

1、安装lua环境

tar zxvf LuaJIT-2.0.2.tar.gz

cd LuaJIT-2.0.2

make && make install

2、配置lua环境变量

export LUAJIT_LIB=/usr/local/lib

export LUAJIT_INC=/usr/local/include/luajit-2.0

3、安装GraphicsMagick

tar zxvf GraphicsMagick-1.3.21.tar.gz

cd GraphicsMagick

./configure –prefix=/data/local/GraphicsMagick –enable-shared

make && make install

查看GraphicsMagick 支持的文件类型:

/data/local/GraphicsMagick/bin/gm -version

Feature Support:

Native Thread Safe yes

Large Files (> 32 bit) yes

Large Memory (> 32 bit) yes

BZIP yes

DPS no

FlashPix no

FreeType no

Ghostscript (Library) no

JBIG no

JPEG-2000 no

JPEG yes

Little CMS no

Loadable Modules no

OpenMP yes (07)

PNG yes

TIFF no

TRIO no

UMEM no

WebP no

WMF no

X11 no

XML no

ZLIB yes

Host type: x86_64-unknown-linux-gnu

若PNG、JPEG等不支持,可以在编译GraphicsMagick的时候,使用 –with-png=yes

4、准备lua模块

ngx_devel_kit-0.2.18.tar.gz

v0.8.6.tar.gz(lua-nginx-module-0.8.6

5、安装Nginx

tar zxvf nginx-1.4.2.tar.gz

cd nginx-1.4.2

./configure —prefix=/usr/local/nginx —add-module=lua-nginx-module-0.8.6 —add-module=ngx_devel_kit-0.2.18

make && make install

测试nginx扩展lua是否正常:

location /test {

default_type text/html;

content_by_lua ’

ngx.say(“hello world”)

ngx.log(ngx.ERR,”err err”)

‘;

}

若返回hello world即说明正常

6、启动Nginx

若出错,如/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

请按如下操作:

find / -name libluajit-5.1.so.2

ln -s /usr/local/lib/libluajit-5.1.so.2 /usr/lib64/libluajit-5.1.so.2

7、lua脚本ImageResizer.lua

local command = “/data/local/GraphicsMagick/bin/gm convert ” .. ngx.var.request_filepath .. ” -resize ” .. ngx.var.width .. “x” .. ngx.var.height .. ” +profile \”*\” ” .. ngx.var.request_filepath .. “_” .. ngx.var.width .. “x” .. ngx.var.height .. “.” ..

ngx.var.ext;os.execute(command);

ngx.exec(ngx.var.request_uri);

Nginx配置请见nginx.conf

user root;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;}http {include mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopushon;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;lua_package_path "/usr/local/nginx/conf/lua/?.lua;;";server {listen 80;server_name localhost;location /group1/M00 {root /home/fastdfs/fastdfs_storage_data/data;if ($uri ~* ^/group1/M00(.+\.(jpg|jpeg|gif|png))_(\d+)x(\d+)\.(jpg|jpeg|gif|png)) { add_header X-Powered-By 'Lua GraphicsMagick';add_header file-path $request_filename;lua_code_cache on;set $request_filepath /home/fastdfs/fastdfs_storage_data/data$1;set $width $3;set $height $4;set $ext $5;}if (!-f $request_filename) {content_by_lua_file conf/lua/ImageResizer.lua;}}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificatecert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}

lua配置请见ImageResizer.lua

if(ngx.var.height ~= '' and ngx.var.width ~= '') thenheight = tonumber(ngx.var.height);width = tonumber(ngx.var.width);if(height > 1000) thenheight = 1000endif(width > 1000) thenheight = 1000endlocal command = "/usr/local/GraphicsMagick/bin/gm convert " .. ngx.var.request_filepath .. " -resize " .. width .. "x" .. height .. " +profile \"*\" " .. ngx.var.request_filepath .. "_" .. ngx.var.width .. "x" .. ngx.var.height .. "." .. ngx.var.ext;os.execute(command);ngx.exec(ngx.var.request_uri);elsengx.exit(ngx.HTTP_NOT_FOUND);end

欢迎加入Java猿社区

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。