Node.js-开发实践:图片处理
【前言】
使用nodejs的过程中会遇到一些处理图片的场景,
比如上传图片时进行压缩,
或者nodejs开发客户端本处理图片等,
本文介绍下nodejs常见的图片处理操作。
【常见图片处理库】
nodejs常见的图片处理库如下,
可以看到sharp从各方面都遥遥领先
npm包
github地址
github stars
github issues
最后更新时间
npm下载量
sharp
https://github.com/lovell/sharp
26.4k
109
2023年09月19日
2831k
gm
https://github.com/aheckmann/gm
6.9k
321
2022年09月21日
225k
jimp
https://github.com/jimp-dev/jimp
13k
173
2023年07月27日
923k
lwip
https://github.com/EyalAr/lwip
2.4k
114
2016年07月19日
408
【sharp】
官网: https://sharp.pixelplumbing.com/
github: https://github.com/lovell/sharp#sharp
npm: https://www.npmjs.com/package/sharp
功能介绍:
1.将大图压缩为小图,并转换为常见的图片格式,包括jpg,png,webp,gif等
2.修改图片尺寸,比几个竞品快4-5倍
3.一些常见的图片操作,包括缩放,旋转等
4.多设备支持,包括windows,linux,mac等
支持的格式:
1.读取:jpeg,png,webp,gif,avif,tiff,svg
2.输出:jpeg,png,webp,gif,avif,tiff
【安装】
需要nodejs大于14.15.0
npm install sharp
安装过程会下载一个对应平台的bin文件,大约7M左右,
默认npm会自定下载对应平台的bin文件,例如macos下载对应的macos文件,
如果要下载不同平台文件,详见: https://sharp.pixelplumbing.com/install#cross-platform
官方贴心的给出了中国镜像,
本地尝试不加中国镜像确实会timeout,
npm_config_sharp_binary_host="https://npmmirror.com/mirrors/sharp" \
npm_config_sharp_libvips_binary_host="https://npmmirror.com/mirrors/sharp-libvips" \
npm install sharp
【获取image】
操作图片前需要先通过sharp方法获取一个image,
其中input可以是文件路径也可以是buffer等,
详见: https://sharp.pixelplumbing.com/api-constructor
// sharp
import sharp from "sharp";
// image
const image = sharp(input);
【获取图片信息】
使用metadata方法获取图片信息,
详见: https://sharp.pixelplumbing.com/api-input#metadata
// sharp
import sharp from 'sharp';
// image
const image = sharp(input);
// info
const info = await image.metadata();
// info
{
channels: 4,
density: 72,
depth: 'uchar',
exif: Buffer @Uint8Array [],
format: 'png',
hasAlpha: true,
hasProfile: true,
height: 260,
icc: Buffer @Uint8Array [],
isProgressive: false,
space: 'srgb',
width: 506,
}
【图片转换】
简单图片转换
支持将常见图片格式jpg,png,webp,gif等相互转换
// sharp
import sharp from "sharp";
// image
const image = sharp(input);
// convert
const newImage = await image.toFile(output);
复杂图片转换
上面toFile方法不支持高级的一些处理,
每种图片转换sharp提供了对应的方法,
转jpg: https://sharp.pixelplumbing.com/api-output#jpeg
转png: https://sharp.pixelplumbing.com/api-output#png
转webp: https://sharp.pixelplumbing.com/api-output#webp
转gif: https://sharp.pixelplumbing.com/api-output#gif
// sharp
import sharp from "sharp";
// image
const image = sharp(input);
// jpg
const newJpg = await image.jpeg(options).toFile(output);
// png
const newPng = await image.png(options).toFile(output);
// webp
const newWebp = await image.webp(options).toFile(output);
// gif
const newGif = await image.gif(options).toFile(output);
【图片缩放】
resize也是最常见的操作,
options详见: https://sharp.pixelplumbing.com/api-resize
// sharp
import sharp from "sharp";
// image
const image = sharp(input);
// new image
const newImage = await image.resize(options).toFile(output);
【其他】
图片合成: https://sharp.pixelplumbing.com/api-composite
图片操作: https://sharp.pixelplumbing.com/api-operation
图片颜色: https://sharp.pixelplumbing.com/api-colour
【qiao-img】
封装一个npm包,欢迎使用, https://www.npmjs.com/package/qiao-img
|— meta
const info = await meta(input);
|—file
const info = await file(input, output, meta);
|—convert
const info = await convert(input, output, meta, convertType, convertOptions);
|—resize
const info = await resize(input, output, options);
也支持cli使用, https://www.npmjs.com/package/qiao-img-cli
# 全局安装
npm i -g qiao-img-cli
# 帮助
qimg
qimg -h
# resize
qimg resize src width height fit
# convert
qimg convert src format
# meta
qimg meta src
【总结】
1.图片处理技术选型:sharp
2.sharp-介绍
3.sharp-安装
4.sharp-获取image
5.sharp-获取图片信息
6.sharp-图片转换
7.sharp-图片缩放
8.qiao-img, https://www.npmjs.com/package/qiao-img
9.qiao-img-cli, https://www.npmjs.com/package/qiao-img-cli
相关推荐
Node.js-WebServer开发实践:使用autocannon进行接口压测
【前言】 AutoCannon是基于Node.js的接口压测工具, https://www.npmjs.com/package/autocannon 【安装】 【cli使用】 AutoCannon可以通过cli的方式使用, 其中各参数的含义可以直接输入autocannon查看, 例如10个并发连接,
Node.js-开发实践:下载文件
【前言】 下载文件是Node.js中最常见的功能, 但实际开发中下载文件也会隐藏各种各样的坑。 【原始代码】 如果在网络搜索Node.js下载文件代码, 大概会搜到类似下面的代码片段, 本文从这里开始,陆续优化下载文件这个功能。 上面的代码片段可以看到: 1.兼容了Node.js原生的http和ht
Node.js-开发实践:使用健壮的FS
【前言】 fs模块是nodejs中最常见的模块, 可是fs的使用经常会有各种意想不到的坑。 【高性能FS】 其中之一是没有使用高性能的fs, 导致在electron应用中造成卡顿, fs模块有3种使用方式, callback方式 1\. 书写会导致回调地狱 2\. 体现nodejs事件驱动,非阻塞i
Node.js-开发实践:高性能FS
【前言】 nodejs的fs模块相信大家都不陌生, 本文对比一下fs模块的三种使用方式。 【fs的三种使用方式】 nodejs官方提供了fs的三种使用方式, https://nodejs.org/dist/latestv18.x/docs/api/fs.htmlpromiseexample call
Node.js-WebServer开发实践:使用PM2-Cluster模式提升接口QPS
【前言】 pm2是nodejs进程管理工具, https://pm2.keymetrics.io/ 介绍详见之前的一篇文章: https://blog.csdn.net/uikoo9/article/details/79018750 , 本文介绍下pm2的cluster模式, 并使用pm2的clus
Node.js-WebServer开发实践:定时任务
【前言】 定时任务是服务端开发中的必备能力, 在nodejs web server的开发过程中, 可以使用cron实现定时任务能力, 【qiaotimer】 cron的使用可以查看官网文档, 这里封装了一个npm包,欢迎使用:https://code.insistime.com//qiaotimer
Node.js-WebServer开发实践:获取公网IP
【前言】 在nodejs server开发实践中,在一些场景下需要获取公网ip, 而nodejs中默认的获取ip的方法,只能获取本地的ip, 而无法获取公网ip,本文介绍如何获取公网ip 【服务端获取公网ip】 在服务端获取公网ip比较简单, nodejs下可以通过下述方法获取, 但是如果有使用ng
Node.js-WebServer开发实践:本地日志
【前言】 本地日志是服务端开发中必备的能力, 在nodejs web server的开发过程中, 可以使用log4js实现本地日志能力, 【qiaolog】 log4js的使用可以查看官网文档, 这里封装了一个npm包,欢迎使用: https://www.npmjs.com/package/qiao
Node.js-WebServer开发实践:上传文件
【前言】 文件上传是服务端开发中的必备能力, 在nodejs web server的开发过程中, 可以使用formidable实现文件上传能力, 【qiaozupload】 formidable的使用可以查看官网文档, 这里封装了一个npm包,欢迎使用: https://code.insistime
一篇文章开发Node.js-WebServer
【前言】 Node.js的服务端框架很多,耳熟能详的有express,koa等, 本文从零到一开发一个Node.js的web server。 https://nodejs.org/en/ https://expressjs.com/ https://koa.bootcss.com/ 【http】 要