一般商品主图大小为800800(淘宝)或者750750(京东),即便以72*72的分辨率去做图,图片的大小也有几百k,对于移动端而已,图片过大,加载过慢,于是研究了下用Nginx和Lua进行图片webp压缩处理。以下我主要参考的几篇文章:
通过 Nginx-Lua 自动转换图片为 WebP 格式
nginx 之前端图片webp
centos yum 安装nginx 后增加模块
centos-nginx添加模块(无需重新编译)

下面进入正题:

安装libwebp

wget "http://downloads.webmproject.org/releases/webp/libwebp-0.6.0-linux-x86-64.tar.gz"
tar --strip-components 1 -xzvf libwebp*.gz -C /usr/local

测试是否安装成功

cwebp -q 75 test.png -o test.png.webp

为nginx添加lua模块

由于我本来就已经安装好了nginx,也不便停用,故只能为nginx添加lua模块。如果是全新安装nginx的同学,建议直接安装OpenResty,里面集成了nginx和lua。以下开始安装:

cd /rootwget https://github.com/openresty/lua-nginx-module/archive/v0.10.11.zipunzip v0.10.11.zipwget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.zipunzip v0.3.0.zipwget http://luajit.org/download/LuaJIT-2.0.5.zipunzip LuaJIT-2.0.5.zipcd LuaJIT-2.0.5makemake install PREFIX=/usr/local/luajitcat > /etc/ld.so.conf.d/luajit.conf<<EOF
/usr/local/luajit/lib
EOFldconfigexport LUAJIT_LIB=/usr/local/luajit/libexport LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

由于我是用yum安装的nginx,查看现在的nginx版本,然后再重新编译。

nginx -Vnginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

于是下载1.14.0版本:

$ wget http://nginx.org/download/nginx-1.14.0.tar.gz
$ tar xvzf nginx-1.14.0.tar.gz

然后重新编译

cd nginx-1.14.0
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'  --add-module=/root/lua-nginx-module-0.10.11 --add-module=/root/ngx_devel_kit-0.3.0$ make #千万别make install,否则就覆盖安装了#备份旧的nginx程序$ cp /usr/sbin/nginx /usr/sbin/nginx.bak#把新的nginx程序覆盖旧的$ cp objs/nginx /usr/sbin/nginx#重启nginx
$ sudo systemctl restart nginx.service

图片压缩配置

新建webp.lua文件

function file_exists(name)local f=io.open(name,"r")if f~=nil then io.close(f) return true else return false end
endlocal newFile = ngx.var.request_filename;
local originalFile = newFile:sub(1, #newFile - 5); -- 去掉 .webp 的后缀
if not file_exists(originalFile) then -- 原文件不存在ngx.exit(404);return;
endlocal cmdstr = "cwebp -q 20 " .. originalFile .. " -o " .. newFile; -- 转换原图片到 webp 格式,这里的质量是 20, 压缩比例为90%
cmdstr = string.gsub(cmdstr,"%$","\\$"); -- 处理字符串中含有dollar符号的
os.execute(cmdstr);if file_exists(newFile) then -- 如果新文件存在(转换成功)ngx.exec(ngx.var.uri) -- Internal Redirect
elsengx.exit(404)
end

如果文件名中有$符号

修改img.conf配置,在server中添加

   # webp 压缩location ~* ^(.+\.(jpg|jpeg|gif|png))\.webp$ {set     $webp_root /mnt/www/img;root    $webp_root;set $filename $webp_root$uri;if (!-f $filename) {set $request_filepath $webp_root$1;content_by_lua_file "/etc/nginx/conf.d/webp.lua";}}

重启nginx即可访问了。

后续

经过上面的一波操作,我们终于可以使用webp压缩了。 不过,可惜的webp的google的标准,由于我们的图片要使用在微信小程序上,而iphone的微信小程序内置的浏览器不支持webp格式。于是只能放弃webp,下面使用的是GraphicsMagick压缩方式来替代。

安装GraphicsMagick

wget https://nchc.dl.sourceforge.net/project/graphicsmagick/graphicsmagick/1.3.31/GraphicsMagick-1.3.31.tar.xz
xz -d GraphicsMagick-1.3.31.tar.xz
tar -xvf GraphicsMagick-1.3.31.tar
cd GraphicsMagick-1.3.31
./configure
make
make install

修改lua文件

function file_exists(name)local f=io.open(name,"r")if f~=nil then io.close(f) return true else return false end
end
local gmReqFile = ngx.var.request_filename; -- 原来访问地址
local originalFile = string.gsub(gmReqFile, "gm/",""); -- 去掉 gm 文件夹
local gmDir = string.match(originalFile, "(.+)/[^/]*%.%w+$") .. "/gm"; -- gm目录
--local imgName = string.match(originalFile, ".+/([^/]*%.%w+)$"); -- 文件名带原本的后缀
local imgName = string.match(originalFile, ".+/([^/]*)%.%w+$"); -- 文件名,无后缀
local gmFile = gmDir .. "/" .. imgName .. ".jpg";  -- 最终访问文件, 全部转为jpg
if file_exists(gmFile) then -- 如果压缩图已经存在ngx.exec(string.gsub(ngx.var.uri, "png","jpg"))--ngx.exec(ngx.var.uri)
end
if not file_exists(originalFile) then -- 原文件不存在ngx.exit(404);return;
end
local response = os.execute("cd " .. gmDir) -- 如果gm文件夹不存在
if response ~= 0 thenos.execute("mkdir -p " .. gmDir);
end
local cmdstr = "gm convert -quality 75% " .. originalFile .. " " .. gmFile; -- 转换质量 75 % ,由于不同图片色彩不一样,压缩比例会不同,30%到90%
cmdstr = string.gsub(cmdstr,"%$","\\$"); -- 处理字符串中含有dollar符号的
os.execute(cmdstr);
if file_exists(gmFile) then -- 如果新文件存在(转换成功)ngx.exec(string.gsub(ngx.var.uri, "png","jpg"))--ngx.exec(ngx.var.uri)
elsengx.exit(404)
end

使用Nginx和Lua进行图片webp压缩处理相关推荐

  1. 配置nginx的Gzip功能实现网页的压缩和图片的压缩

    1.为什么要用GZIP实现网页和图片的压缩?nginx怎么实现压缩的? 使用Gzip压缩可以提高CPU 使用率,可以减少服务器发送的字节数量.这就使人们觉得页速度加快了,并且还减少了带宽的用量. 根据 ...

  2. Nginx利用lua剪辑FastDFS图片

    Nginx利用lua剪辑FastDFS中的图片 我们经常用FastDFS来做图片服务器,通过nginx来上传或者获取图片.本文要实现的功能是,当客户端要获取不同尺寸的图片是,lua根据url中的尺寸大 ...

  3. Nginx实战(二) Rewrite + 图片的压缩过滤

    文章目录 Nginx实战(二) Rewrite + 图片的压缩过滤 1.图片的压缩过滤 2.HTTPS加密认证 3.Nginx的rewrite rewite规则 网页重写的具体实现 如何实现两个域名之 ...

  4. 【Android 内存优化】图片文件压缩 ( Android 原生 API 提供的图片压缩功能能 | 图片质量压缩 | 图片尺寸压缩 )

    文章目录 一. 图片压缩 二. 图片文件压缩类型 三. Android 原生 API 提供的质量压缩 四. Android 原生 API 提供的尺寸压缩 一. 图片压缩 图片压缩 : ① 文件压缩 : ...

  5. webp怎么转png?图片webp格式怎么转换?

    webp是一种动态格式的图片,那么怎么把它改变成其他格式呢?使用专业的工具来处理是最方便的方法了,如何使用webp在线转换工具把webp转png(在线图片格式转换器(jpg.png.gif.webp. ...

  6. Centos 通过 Nginx 和 vsftpd 构建图片服务器

    1.Nginx 简介 nginx_百度百科 Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由伊戈尔·赛索耶夫为俄罗斯 ...

  7. 如何把webp压缩大小?在线webp压缩的工具推荐

    webp是一种同时提供了有损压缩与无损压缩的图片文件格式,而且还拥有动态与静态两种模式,现在日常用到webp图片的人也越来越多了,如何实现webp压缩(webp压缩 webp图片压缩 在线webp无损 ...

  8. COS数据处理WebP压缩 | 减少70%图像大小

    当前网络中,图片仍是占用流量较大的一部分,在网站的视觉效果和加载速度之间,我们始终面临着两难选择. 一个网站的内容,不仅仅只有文字,图片.动图.视频等众多元素都在帮助用户从我们的网站获取更多的信息,当 ...

  9. nginx静态资源缓存和gzip压缩

    其实这章主要就是优化访问时间和减少宽带问题 优化访问时间,一般css,js,woff还有图片都是静态文件,是一般都不用更改得,如果每次请求都要走tomcat,都是要耗时一些的,那么可以第一次访问的时候 ...

最新文章

  1. Kali Linux常用服务配置教程DHCP服务原理
  2. table的分页打印
  3. Java函数式接口看这一篇就够了
  4. python登录网站 爬虫_Python爬虫如何使用Cookies登录网站
  5. JavaFX技巧23:节省内存! 属性的阴影场
  6. ffplay分析(从启动到读取数据线程插入到字幕、音频、视频解码前的队列操作)
  7. 【OpenGL】游戏编程常用TGA图像格式详解以及加载纹理编程实现
  8. 分离了sa默认的数据库 , 用sa登录不了的解决方法
  9. 用Python做童年回忆的游戏 贪吃蛇
  10. 苹果如何分屏_玩转mac—苹果电脑操作教程
  11. linux 内核 介绍,Linux内核详细介绍
  12. FLUENT 流体计算应用教程
  13. js—封装原生AJAX
  14. fastfds文件服务器搭建
  15. php制作入库单,教你如何利用vba制作自动登记数据的仓库入库单
  16. Concatenated Multiples(思维,数学)
  17. task9-文件与文件系统
  18. 一步步实现windows版ijkplayer系列文章之三——Ijkplayer播放器源码分析之音视频输出——音频篇
  19. Ventory制作多系统启动u盘 和 安装 windows10+ubuntu双系统
  20. 3D打印机的故障检测及排除

热门文章

  1. 《Android源代码设计模式解析与实战》读书笔记(十四)
  2. linux 接收接盘输入的方式
  3. 微软输入法正则bug
  4. 努比亚红魔6spro线刷9008救砖教程
  5. html5视频播放av,7月AHA急救课程报名中!掌握埃里克森心脏骤停的获救技能!!...
  6. GAN(生成对抗网络)和IQA(图像质量评价能擦出什么样的火花呢?)简单聊一些近来published的论文
  7. 计算机基础是公共必修课,计算机公共必修课《大学计算机基础》课程教学大纲.doc...
  8. 贝尔宾九种团队角色理论总结(转)
  9. 互联网架构概述 互联网架构演变过程
  10. 每天不知道吃什么,于是我做了个随机选择的小程序