当前位置:主页 > 聚焦 >

nodejs怎么实现对图片进行批量裁剪?

时间:2020-12-09 23:55:27

  nodejs怎么实现对图片进行批量裁剪?下面本篇文章给大家介绍一下nodejs实现批量裁剪图片功能的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

  相关:《nodejs 教程》

  1、初始化

  首先新建一个 tailor-img 文件夹,接着执行 npm init -y 初始化一个package.json

  2、安装相关插件archiver 压缩文件canvas 裁剪图片glob 批量获取路径npm i archiver canvas glob --save3、app.jsconst fs = require('fs') const { basename } = require('path') // 压缩文件 const archiver = require('archiver') // canvas库,用于裁剪图片 const { createCanvas, loadImage } = require('canvas') // 批量获取路径 const glob = require('glob') const config = require('./config') // 根据宽高获取配置 function getOptions(options, config) { const [sourceWidth, sourceHeight] = options const { width, height, isWidth, isHeight, scale } = config const haveWidth = [width, (sourceHeight * width * scale) / sourceWidth] const haveHeight = [(sourceWidth * height * scale) / sourceHeight, height] if (width === 0 height === 0) { return [0, 0] } if (width && height) { if (isWidth) { return haveWidth } if (isHeight) { return haveHeight } return [width / scale, height / scale] } if (width && !height) { return haveWidth } if (height && !width) { return haveHeight } return options.map((item) => item / scale) } !(async () => { const paths = glob.sync('./images/*') // 压缩成zip const archive = archiver('zip', { zlib: { level: 9, }, }) // 输出到当前文件夹下的 image-resize.zip const output = fs.createWriteStream(__dirname + '/image-resize.zip') archive.pipe(output) for (let i = 0; i < paths.length; i++) { const path = paths[i] const image = await loadImage(path) const { width, height } = image // 由于使用了扩展运算符展开对象,这里需要为对象定义迭代器ex < objArr.length - 1) { let key = objArr[this.index] this.index++ return { value: obj[key] } } else { return { done: true } } }, index: 0, } } // 默认缩放2倍 // const options = [width, height].map((item) => item / 2) const options = getOptions(obj, config) const canvas = createCanvas(...options) const ctx = canvas.getContext('2d') ctx.drawImage(image, 0, 0, ...options) archive.append(canvas.toBuffer(), { name: `${basename(path)}` }) } })()4、config.js用于修改宽高等配置module.exports = { width: 300, height: '', // 根据宽度等比缩放,优先级更高 isWidth: true, // 根据高度等比缩放 isHeight: false, // 宽高整体缩放 scale: 1, }

  编程视频课程!!

热点推荐
1 麻吉黄立成:ETH多单增持900枚,持仓规模

消息,麻吉黄立成在Hyperliquid平台上增持ETH多单900枚,约合2079840美元,当前持仓规模为2979384...

2 德副总理:德国不需要特朗普的“指点”

德国副总理兼财政部长克林拜尔在5月1日于北威州贝格卡门市举行的活动上表示,德国不需要特...

3 Coinglass:以太坊面临约8.74亿美元多头清算

消息,据数据显示,以太坊多头在价格跌破约2206美元时面临约874万美元的清算风险,而空头在...

4 比特币突破78000美元,短期内清算超3000万

消息,最近比特币价格突破78000美元,过去一小时内清算超过3000万美元的空头仓位。...

5 Riot Platforms:第一季度数据中心收入达3

消息,Riot Platforms报告称,第一季度数据中心收入为3320万美元,占其总收入1.672亿美元的约20...

6 美联储卡什卡利:海峡长期关闭可能危及

消息,美联储官员卡什卡利表示,海峡长期关闭带来的价格冲击可能会危及通胀预期,因此需...

7 美国防部与7家AI公司达成协议

消息,美国国防部今天发声明称,已与7家领先的人工智能公司达成协议,分别是SpaceX、OpenAI、...

8 ZEC最大空头:CL空单增持10174.50枚

消息,ZEC最大空头CL空单增持10,174.50枚,约合1,086,247.65美元,持仓规模达到15,179,643.08美元,均...

9 Riot Platforms公布2026年Q1营收1.67亿美元,首

消息,加密矿企Riot Platforms公布2026年第一季度营收为1.67亿美元,其中数据中心业务收入3320万美...

10 KuCoin支持PHIL代币交换并更名为AKITA

消息,KuCoin宣布将支持PHIL代币的交换,并更名为AKITA。PHIL到AKITA的代币交换将自动完成,具体...

成都来彰科技 蜀ICP备2025134723号-1

资讯来源互联网,如有版权问题请联系管理员删除。