【问题标题】:NodeJS Animated gif resizingNodeJS 动画 gif 调整大小
【发布时间】:2018-04-18 17:20:53
【问题描述】:

我正在尝试使用 NodeJS 调整动画 gif 的大小。 我需要它在调整大小后保持动画。

我的应用目前正在使用Sharp 调整其他图像的大小,但它似乎不支持导出动画 gif。

如果可能的话,我想找到一个不依赖其他外部软件(如 *Magick)的 npm 包。 如果它也支持流,那就太棒了。

【问题讨论】:

    标签: node.js image-resizing animated-gif


    【解决方案1】:

    我不知道任何原生 npm 包,但你可以使用 gifsicle

    Gifsicle 是一个命令行工具,用于创建、编辑和获取有关 GIF 图像和动画的信息。

    这是一篇关于使用 gifsicle 调整动画 gif 大小的好文章: https://davidwalsh.name/resize-animated-gif

    示例1:我使用gifsicle模块安装gifsicle的二进制文件:

    const { execFile } = require('child_process');
    const gifsicle = require('gifsicle');
    
    console.time('execute time');
    
    // You can use these options for resizing:
    // --scale 0.5
    // --resize-fit-width 300
    // --resize-fit-height 200
    // --resize 300x200
    execFile(gifsicle, ['--resize-fit-width', '300', '-o', 'output.gif', 'input.gif'], err => {
      console.timeEnd('execute time');
    
      if (err) {
        throw err;
      }
    
      console.log('image resized!');
    });
    

    示例2:使用spawn方法获取可读流:

    const fs = require('fs');
    const { spawn } = require('child_process');
    const gifsicle = require('gifsicle');
    
    console.time('execute time');
    
    const stream = spawn(gifsicle, ['--resize-fit-width', '300', 'input.gif']);
    
    stream.on('close', () => {
      console.timeEnd('execute time');
    
      console.log('image resized!');
    });
    
    stream.stdout.pipe(fs.createWriteStream('output.gif'));
    

    【讨论】:

      猜你喜欢
      • 2011-11-01
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      • 2012-04-16
      • 2013-08-28
      • 2011-08-09
      • 2011-08-31
      相关资源
      最近更新 更多