配置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

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

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

掃一掃不迷路


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM