<span>配置Nginx 扩展实现图片剪裁</span>

在此之前需要安装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

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

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

全部评论

相关推荐

LuvSran:是人我吃。老师就是学校呆久了,就业方面啥都不懂,还自以为是为了我们就业好。我学校就一破双非,计科入行率10%都没有,某老师还天天点名,说是出勤率抬头率前排率高了,华为什么的大厂就会来,我们就是不好好上课才没有厂来招。太搞笑了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务