配置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 //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 //127.0.0.1:9999;
    	}
	location ~* (.*).(JPG|jpeg|jpg|gif|png|PNG)$ {
        	proxy_pass //127.0.0.1:9999;
    	}
    	

最後訪問格式如下:

原圖://xxx.com/test.jpg

裁剪100×100://xxx.com/test_100x100.jpg

裁剪200×200兼調整質量60://xxx.com/test_200x200_60.jpg

大概就是這個意思吧。僅供參考,如有大神希望指出問題所在,我們大家一起學習一下。

也當作自己的一個記錄,怕以後又忘記了。

掃一掃不迷路

Tags: