如何在NodeJS上使用GraphicsMagick提取GIF的第一帧并将其保存为PNG? [英] How can I extract the first frame of a GIF and save it as a PNG using GraphicsMagick on NodeJS?

查看:653
本文介绍了如何在NodeJS上使用GraphicsMagick提取GIF的第一帧并将其保存为PNG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我要执行此操作的步骤:

Here are the steps in which I want to do this:


  1. 用户向服务器提供指向动画gif的链接

  2. 服务器使用request(url)来检索文件

  3. 服务器生成gif第一帧的缩略图

  4. 服务器将缩略图上传到Amazon S3

  1. The user gives the server a link to an animated gif
  2. The server uses "request(url)" to retrieve the file
  3. The server generates a thumbnail of the gif's first frame
  4. The server uploads the thumbnail to Amazon S3

步骤3中存在问题。我无法找到方法提取gif的第一帧。我更喜欢使用GraphicsMagick,但如果你知道其他方式有效,我将非常感激!

The problem exists in Step 3. I can't figure out a way to extract the gif's first frame. I would prefer to use GraphicsMagick but if there is another way you know of that works, I would be most grateful!

正在使用NodeJS。

NodeJS is being used.

推荐答案

我不知道为什么这不在文档中,而是其他人在stackoverflow上回答了这个问题

I don't know why this isn't in the documentation, but someone else answered this here on stackoverflow.

所以,你可以使用文件路径字符串或流/缓冲区,如下所示:

So, you can use this either with a file path string or a stream/buffer, like so:

对于文件:

gm('/path/to/animated.gif')
.selectFrame(0)
.write('/path/to/firstframe.png', function(err){
    if (err) print('  :(  ');
})

对于流/缓冲区:

gm(streamOrBuffer)
.selectFrame(0)
.write('/path/to/firstframe.png', function(err){
    if (err) print('  :(  ');
})

在文档中,他们说你需要将路径字符串更改为'/ path / to / animated.gif [0]'更具体的框架你想选择,但我在没有指定框架的情况下测试了该代码,并且工作正常(使用gm@1.21.1和ImageMagick 6.7.7-10 2014-03-06)。此外,他们在文档中的内容(不使用selectFrame)不起作用。它最终崩溃并在整个过程中创建了整个gif的几个png帧。

In the documentation they say you need to change the path string to be something like '/path/to/animated.gif[0]' to be more specific with the frame you want to select, but I tested that code without specifying the frame, and it worked fine (using gm@1.21.1 and ImageMagick 6.7.7-10 2014-03-06). Also, what they had in the documentation (without using selectFrame) didn't work. It ended up crashing and creating several png frames of the whole gif in the process.

为什么他们没有记录 selectFrame 但是超出了我的范围。正如他们在引用的链接中指出的那样,准确地想知道是一个未解决的问题。

Why they haven't documented selectFrame yet is beyond me. As they pointed out in the cited link, there's an open issue wondering precisely that.

这篇关于如何在NodeJS上使用GraphicsMagick提取GIF的第一帧并将其保存为PNG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆