很强大,图片转ico库,也可以转换各种图像格式

大家好我是辉子,遇到有用的东西就记录下来,也希望和您成为朋友。关注 公众号: 【罗米笔记】,有更好的笔记会及时更新,

昨天想找下直接在js转ico的库,以后用到项目中,就发现了这个,记录下来留着使用

Sharp库是一个用于处理图像的Node.js库,它提供了一组功能强大且易于使用的API,可以用于读取、写入和转换各种图像格式。Sharp库支持多种常见的图像格式,包括JPEG、PNG、GIF、BMP等,并且可以在不损失质量的情况下对图像进行缩放、旋转、裁剪等操作。此外,Sharp库还支持流式处理,可以在处理大型图像时节省内存。总之,Sharp库是一个非常实用的Node.js图像处理库,可以帮助开发者轻松地完成各种图像处理任务。

图片

是在nodejs环境中使用,主要用在服务器端,可以使用nest进行测试

用法:

npm install sharp

// 转换图片格式并设置大小, 可以转换成多种图片格式 。const inputImagePath = '/Users/wanghui/Desktop/test/images/1.png';const outputIcoPath = './1.jpg';const sharpImage = sharp(inputImagePath);const resizeImage = sharpImage.resize(200, 200);await resizeImage.toFile(outputIcoPath);
// 通过这样的方式进行转化 const transformer = sharp()  .resize(200)  .on('error', function (err) {    console.log(err);  });const readableStream = fs.createReadStream(inputImagePath);
const writableStream = fs.createWriteStream(outputIcoPath);readableStream.pipe(transformer).pipe(writableStream);
// 输入生成的图片,buffer格式,可以转成base64输出await sharp({  // Create a 300x200 RGBA image with a red background  create: {    width: 300,    height: 200,    channels: 4,    background: { r: 255, g: 0, b: 0, alpha: 0.5 },  },})  // Use PNG format  .png()  // Convert to a buffer  .toBuffer();
// 转化gif为webpawait sharp('./2.gif', { animated: true }).toFile('out.webp');
// 生成图片const input = Uint8Array.from([255, 255, 255, 0, 0, 0, 255, 255, 0]); // or Uint8ClampedArrayconst image = sharp(input, {  // because the input does not contain its dimensions or how many channels it has  // we need to specify it in the constructor options  raw: {    width: 3,    height: 1,    channels: 3,  },});await image.toFile('./my-two-pixels.png');
// 生成文字图片await sharp({  text: {    text: `<span foreground="red">Red!</span><span background="cyan">blue</span>`,    font: 'sans',    rgba: true,    dpi: 300,  },}).toFile('text_rgba.png');

当然还有很多的用法及参数。可以参照官网。这个适合做工具类的图片转换,当然也可以用到其他的场景中,就看是怎么使用了

github: https://github.com/lovell/sharp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗米笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值