首页 > 学院 > 开发设计 > 正文

OpenResty(Nginx)+Lua+GraphicsMagick实现图片截图与缓存 二

2019-11-06 09:01:32
字体:
来源:转载
供稿:网友

安装openresty(自动官网下载安装)

安装GraphicsMagick

sudo apt-get install GraphicsMagick

配置openresty下的nginx配置文件(/usr/local/openresty/nginx/config/nginx.conf)

worker_PRocesses1;# 日志级别调试时可设为notice,生产环境请设为errorerror_log  /usr/local/openresty/nginx/logs/error.log notice;events   {       use epoll;       worker_connections51200;   }http   {       lua_package_path'/usr/local/openresty/nginx/lua/?.lua;;';              server{               listen      8055;               server_name  localhost;               root  /usr/local/nginx/html;                              #/thumbnail目录下的图片请求不经过缩略图模块               location^~/imgcache/{                      }                              #对类似_100x100.gif/jpg/png/jpeg进行缩略图处理               location~*_([0-9]+)x([0-9]+)/.(gif|jpg|png|jpeg)${                 #匹配文件名规则                       set$image_root/usr/local/nginx/html;                 #图片目录                       set$thumbnail_root/usr/local/nginx/html/imgcache;   #缩略图存放目录                       #如果缩略图文件存在,直接返回                       set$file $thumbnail_root$uri;                       if(-f $file){                               rewrite^/(.*)$/imgcache/$1 last;                       }                       #如果缩略图文件不存在,则应用缩略图模块处理                       if(!-f $file){                               rewrite_by_lua_file lua/thumbnail.lua;                       }               }       }}

 

配置lua配置文件(/usr/local/openresty/nginx/lua/config.lua)

-- nginx thumbnail module -- last update : 2014/8/21-- version     : 0.4.1module(...,package.seeall)--[[	enabled_log:			是否打开日志	lua_log_level:			日志记录级别	gm_path:				graphicsmagick安装目录	img_background_color:	填充背景色	enabled_default_img:	是否显示默认图片	default_img_uri:		默认图片链接		default_uri_reg:		缩略图正则匹配模式,可自定义		_[0-9]+x[0-9]						对应:001_100x100.jpg		_[0-9]+x[0-9]+[.jpg|.png|.gif]+ 	对应:001.jpg_100x100.jpg]]enabled_log 		 = truelua_log_level        	 = ngx.NOTICEgm_path		 = '/usr/bin/gm'img_background_color   = 'white'enabled_default_img     = truedefault_img_uri 	 = '/default/notfound.jpg' default_uri_reg      	 = '_[0-9]+x[0-9]+[.jpg|.png|.gif]+' quality			= 80  -- 质量direction	             =  'southeast' --位置warter_dissolve	= 50  --百分比 warter_img_uri              = '/usr/local/nginx/html/default/water.png'--[[ 	配置项,对目录、缩略图尺寸、裁剪类型进行配置,匹配后才进行缩略图处理	1.sizes={'350x350'} 填充后保证等比缩图	2.sizes={'300x300_'}等比缩放	3.sizes={'50x50^'}裁剪等比缩放 (缺点:裁剪了图片的一部分)		dir="/"       对应根目录,请放在default之前	dir="default" 对应默认图片尺寸,当原图不存在时,请求该尺寸会以默认图片生成缩略图]]cfg = {		{			dir      = '/',			sizes    = {'50x50^','100x100^','250x250^','300x300^','350x350^','80x80^','120x120^'},		},		{	dir   = 'default',			sizes = {'50x50^','100x100^','250x250^','300x300^','350x350^','80x80^'},		}	}	

 

lua脚本(/usr/local/openresty/nginx/lua/thumbnail.lua

访问测试路径:http://aidemofang.com/test/3.jpg_300x300.jpg

重新加载nginx命令 nginx -s reload

    参考:

  http://www.hopesoft.org/blog/?p=1188

  http://fromwiz.com/share/s/1hfxw32Jy17v2YBkmI2eNjeH30ZPIb1ajAQd2rq5h51svjuD

 

来源: http://hihubs.com/article/269


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表