Nginx实战案例--图片的过滤压缩

不管一个系统或网站的大与小,都存在相应的图片处理,生成缩略图、为图片加水印等等,如果涉及到APP端,这个图片的处理需求变得更加重要了,因为在目前看来,客户端的屏幕大小不一,会导致以下问题:

1、图片过大导致APP加载图片速度慢;
2、消耗用户过多流量。

注意:若要实现图片的压缩和过滤,nginx必须编译了 --with-http_image_filter_module=dynamic模块,而此模块编译时需要gd-devel包,这个包下载地址:https://centos.pkgs.org/7/centos-x86_64/gd-devel-2.0.35-26.el7.x86_64.rpm.html,下载后 yum install gd-devel-2.0.35-26.el7.x86_64.rpm -y,注意版本不能错

1.配置nginx服务器

nginx服务器的ip地址为172.25.63.1

[root@server1 modules]# vim /usr/local/nginx/conf/nginx.conf


  1 load_module modules/ngx_http_image_filter_module.so;
......
#编辑一个location
 57         location /download {
 58            image_filter resize 150 100;			#重新调整图片的尺寸(像素)
 59         }
......

之后重新加载nginx:

[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload

2.加入测试文件

[root@server1 conf]# cd /usr/local/nginx/html/download/
[root@server1 download]# ls
1.jpg

3.测试

使用客户端访问http://172.25.63.1/download/1.jpg
在这里插入图片描述可以看出图片很小为1.64kb,原图片的大小:

[root@server1 download]# du -sh 1.jpg 
8.0K	1.jpg

成功实现图片的压缩。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值