nginx-image-filter配置实现缩略图

查看模块是否安装

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

表示已安装

nginx配置

server {
	listen 80;
	server_name 127.0.0.1; #域名或IP
	root /root/data; #图片目录
	#实现图片缩略图
	location ~ (.+)\.(jpg|gif|png|JPG|JPEG|PNG|GIF)@(\d+)w_(\d+)h_(\d+)Q_([rc])$ {		 
		set $w $3; #宽
		set $h $4; #高
		set $q $5; #图片质量
		set $type $6;
		set $image_path  $1.$2; #真实图片地址
		set $cache_path  $1_$3w_$4h_$5Q_$6.$2;  #临时文件地址
		
		if ($type = 'r') {
			set $type 'image-resize';
		}
		if ($type = 'c') {
			set $type 'image-crop';
		}
		set $image_uri /$type$image_path?w=$w&h=$h&q=$q;	
		if (-f $cache_path) {
			rewrite (.+)\.(jpg|gif|png|JPG|JPEG|PNG|GIF)@(\d+)w_(\d+)h_(\d+)Q_([rc])$ $1_$3w_$4h_$5Q_$6.$2;
			break;
		}
		if (!-f $cache_path) {	
			proxy_pass http://127.0.0.1$image_uri;
			break;
		}
		proxy_store $cache_path;
		proxy_store_access user:rw group:rw all:rw;
		proxy_temp_path  /tmp/image;
		proxy_set_header Host $host;
		expires  30d; # 设置图片过期时间30天
	}
	#图片裁剪
	location ~ /image-resize(.+)\.(jpg|gif|png|JPG|JPEG|PNG|GIF) {
		rewrite /image-resize(.+)\.(jpg|gif|png|JPG|JPEG|PNG|GIF)$ $1.$2 break;
		image_filter resize $arg_w $arg_h;
		image_filter_jpeg_quality $arg_q;
		image_filter_buffer 50M;
		try_files $1.$2 /img/notfound.jpg;
	}
	#图片缩放
	location ~ /image-crop(.+)\.(jpg|gif|png|JPG|JPEG|PNG|GIF) {
		rewrite /image-crop(.+)\.(jpg|gif|png|JPG|JPEG|PNG|GIF)$ $1.$2 break;
		image_filter crop $arg_w $arg_h;
		image_filter_jpeg_quality $arg_q;
		image_filter_buffer 50M;
		try_files $1.$2 /img/notfound.jpg;
	}
	#正常图片
	location /image { 
	   expires 30d;
    }
}
访问方式
http://127.0.0.1/image/xxx.jpg
http://127.0.0.1/xxx.jpg@100w_100h_100Q_r 
http://127.0.0.1/xxx.jpg@100w_100h_100Q_c
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值