配置Nginx 扩展实现图片剪裁

在此之前需要安装ngx_http_image_filter_module,如果是采用的Docker的话可以看看我历史文章。

然后修改配置文件,增加几个location模块,配置如下,仅供参考👇

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location ~* (.*)_(\d+)x(\d+)\.(JPG|jpeg|jpg|gif|png|PNG)$ {
        	set $img_width $2;
        	set $img_height $3;
        	image_filter resize $img_width $img_height;
        	image_filter_buffer 10M;
        	rewrite ^(.*)_(\d+)x(\d+).(JPG|jpeg|jpg|gif|png|PNG)$ /$1.$4 break;
        	proxy_pass http://127.0.0.1:9999;
    	}
    	location ~* (.*)_(\d+)x(\d+)_(\d+)\.(JPG|jpeg|jpg|gif|png|PNG)$ {
        	set $img_width $2;
        	set $img_height $3;
        	set $img_quality $4;
        	rewrite ^(.*)_(\d+)x(\d+)_(\d+).(JPG|jpeg|jpg|gif|png|PNG)$ /$1.$5 break;
        	image_filter resize $img_width $img_height;
        	image_filter_buffer 10M;
        	image_filter_jpeg_quality $img_quality;
        	proxy_pass http://127.0.0.1:9999;
    	}
	location ~* (.*).(JPG|jpeg|jpg|gif|png|PNG)$ {
        	proxy_pass http://127.0.0.1:9999;
    	}
    	

最后访问格式如下:

原图:http://xxx.com/test.jpg

裁剪100x100:http://xxx.com/test_100x100.jpg

裁剪200x200兼调整质量60:http://xxx.com/test_200x200_60.jpg

大概就是这个意思吧。仅供参考,如有大神希望指出问题所在,我们大家一起学习一下。

也当作自己的一个记录,怕以后又忘记了。

扫一扫不迷路

posted @ 2020-11-28 22:30  自在拉基  阅读(89)  评论(0编辑  收藏