node-images 进行图片压缩
前置条件:先安装images
npm install images
编写代码
思路: 从指定文件夹遍历图片,执行压缩,压缩完成后放到指定文件夹中,并保持图片名无变化。
var images = require("images");
var fs = require("fs");
var path = "1012";
var outpath = "compress/";
function compress(path){
fs.readdir(path, function(err, files){
if(err){
console.log('error:\n' + err);
return;
}
files.forEach(function(file){
fs.stat(path + '/' + file, function(err, stat){
if(err){console.log(err); return;}
if(stat.isDirectory()){
// 如果是文件夹遍历
compress(path + '/' + file);
}else{
//遍历图片
console.log('文件名:' + path + '/' + file);
var name = path + '/' + file;
var outName = outpath+file
images(name) .save(outName, {
quality : 82 //保存图片到文件,图片质量为50
});
}
});
});
});
}
compress(path)
测试执行: node img.js

结果
经测试,在不改变图片宽高情况下,手机拍照原图进行压缩,压缩图片大小是原图的4分之1。
附注:api接口
images(file)
Load and decode image from file
从指定文件加载并解码图像
images(width, height)
Create a new transparent image
创建一个指定宽高的透明图像
images(buffer[, start[, end]])
Load and decode image from a buffer
从Buffer数据中解码图像
images(image[, x, y, width, height])
Copy from another image
从另一个图像中复制区域来创建图像
.fill(red, green, blue[, alpha])
eg:images(200, 100).fill(0xff, 0x00, 0x00, 0.5) Fill image with color
以指定颜色填充图像
.draw(image, x, y)
Draw image on the current image position( x , y )
在当前图像( x , y )上绘制 image 图像
.encode(type[, config])
eg:images("input.png").encode("jpg", {operation:50}) Encode image to buffer, config is image setting.
以指定格式编码当前图像到Buffer,config为图片设置,目前支持设置JPG图像质量
Return buffer
返回填充好的Buffer
Note:The operation will cut off the chain
注意:该操作将会切断调用链
See:.save(file[, type[, config]]) 参考:.save(file[, type[, config]])
.save(file[, type[, config]])
eg:images("input.png").encode("output.jpg", {operation:50}) Encoding and save the current image to a file, if the type is not specified, type well be automatically determined according to the file, config is image setting. eg: { operation:50 }
编码并保存当前图像到 file ,如果type未指定,则根据 file 自动判断文件类型,config为图片设置,目前支持设置JPG图像质量
.size([width[, height]])
Get size of the image or set the size of the image,if the height is not specified, then scaling based on the current width and height
获取或者设置图像宽高,如果height未指定,则根据当前宽高等比缩放
.resize(width[, height])
Set the size of the image,if the height is not specified, then scaling based on the current width and height
设置图像宽高,如果height未指定,则根据当前宽高等比缩放, 默认采用 bicubic 算法。
.width([width])
Get width for the image or set width of the image
获取或设置图像宽度
.height([height])
Get height for the image or set height of the image
获取或设置图像高度
images.setLimit(width, height)
Set the limit size of each image
设置库处理图片的大小限制,设置后对所有新的操作生效(如果超限则抛出异常)
images.setGCThreshold(value)
Set the garbage collection threshold
设置图像处理库自动gc的阈值(当新增内存使用超过该阈值时,执行垃圾回收)
images.getUsedMemory()
Get used memory (in bytes)
得到图像处理库占用的内存大小(单位为字节)
images.gc()
Forced garbage collection
强制调用V8的垃圾回收机制
node-images 进行图片压缩的更多相关文章
- node笔记——gulp-imagemin图片压缩
出处:http://blog.csdn.net/kkgege/article/details/49929983 之前用项目用gulp进行前端的构建,用到压缩图片插件gulp-imagemin, 后来发 ...
- 好久没发贴了,最近捣鼓了个基于node的图片压缩小网站解析。
看了下,距离上次发帖都是去年10月份的事,忙于工作的我很少跑博客园里面来玩了. 做这个小网站的初衷是 https://tinypng.com/ 这个网站有时候访问很慢,然后自己去研究了下图片压缩. 网 ...
- 前端构建工具之gulp(一)「图片压缩」
前端构建工具之gulp(一)「图片压缩」 已经很久没有写过博客了,现下终于事情少了,开始写博吧 今天网站要做一些优化:图片压缩,资源合并等 以前一直使用百度的FIS工具,但是FIS还没有提供图片压缩的 ...
- Gulp自动化工具之图片压缩
一.安装node https://nodejs.org/download/ 根据需要选择对应的版本 安装好了之后可以通过node -v参看一下版本 node -v 二.安装gulp npm insta ...
- Golang 编写的图片压缩程序,质量、尺寸压缩,批量、单张压缩
目录: 前序 效果图 简介 全部代码 前序: 接触 golang 不久,一直是边学边做,边总结,深深感到这门语言的魅力,等下要跟大家分享是最近项目 服务端 用到的图片压缩程序,我单独分离了出来,做成了 ...
- 三款不错的图片压缩上传插件(webuploader+localResizeIMG4+LUploader)
涉及到网页图片的交互,少不了图片的压缩上传,相关的插件有很多,相信大家都有用过,这里我就推荐三款,至于好处就仁者见仁喽: 1.名气最高的WebUploader,由Baidu FEX 团队开发,以H5为 ...
- gulp图片压缩
gulp图片压缩 网页性能优化,通常要处理图片,尤其图片量大的时候,更需要工具来批量处理,这里使用gulp,做个简单总结 image-resize压缩尺寸 var gulp = require('gu ...
- Android 图片压缩、照片选择、裁剪,上传、一整套图片解决方案
1.Android一整套图片解决方案 http://mp.weixin.qq.com/s?__biz=MzAxMTI4MTkwNQ==&mid=2650820998&idx=1& ...
- Java中图片压缩处理
原文http://cuisuqiang.iteye.com/blog/2045855 整理文档,搜刮出一个Java做图片压缩的代码,稍微整理精简一下做下分享. 首先,要压缩的图片格式不能说动态图片,你 ...
- android 图片压缩
引用:http://104zz.iteye.com/blog/1694762 第一:我们先看下质量压缩方法: private Bitmap compressImage(Bitmap image) { ...
随机推荐
- robot:当用例失败时执行关键字(发送短信)
使用场景: 当用例失败时需要通知对应人员,则需要在Teardown中,使用关键字Run Keyword If Test Failed Send Message关键字为自定义关键字,${content} ...
- .Net Core 1.1打包发布到Linux
在 .Net Core 1.0 中我们打包跨平台包是在 project.json 中配置的 但是 .Net Core 1.1 去掉了 project.json 具体咋办参考:https://blogs ...
- Oracle Spatial分区应用研究之二:按县分区与按省分区对比测试报告
1.实验目的 在上一轮的实验中,oracle 11g r2版本下,在87县市实验数据的基础上,比较了分表与分区的效率,得出了分区+全局索引效率较高的结论(见上一篇博客).不过我们尚未比较过不同的分区粒 ...
- 【转帖】编译-O 选项对性能提升作用
编译-O 选项对性能提升作用 https://www.cnblogs.com/pigerhan/p/3526889.html GCC -O 选项 这个选项控制所有的优化等级.使用优化选项会使编译过程耗 ...
- Kafka排队:Apache Kafka作为消息传递系统
1.目标 在这个Apache Kafka教程中,我们将学习Apache Kafka Queuing 的概念 .基本上,Kafka中的排队是传统消息传递的模型之一.所以,让我们首先简要介绍Kafka ...
- 题解 P3957 【跳房子】
对于这题有一个不用单调队列并且不是玄学设置区间最大值的做法 这题校内模拟考的时候打二分+枚举,结果写炸了,跑过来看题解发现为什么他们的区间最大值都是 $ 1005 $ ???特别懵,其实我的代码在dp ...
- Luogu4548 CTSC2006 歌唱王国 概率生成函数、哈希
传送门 orz ymd 考虑构造生成函数:设\(F(x) = \sum\limits_{i=0}^\infty f_ix^i\),其中\(f_i\)表示答案为\(i\)的概率:又设\(G(x) = \ ...
- [高清·非影印] Docker 容器与容器云(第2版)
------ 郑重声明 --------- 资源来自网络,纯粹共享交流, 如果喜欢,请您务必支持正版!! --------------------------------------------- 下 ...
- JSON C# Class Generator
http://www.xamasoft.com/json-class-generator/ JsonHelper.cs using System; using System.Collections.G ...
- flutter apk 打包
https://blog.csdn.net/weixin_33738578/article/details/87998565 http://www.cnblogs.com/sangwl/p/10400 ...