2017-11-06 9 views
0

NodeJS를 사용하여 애니메이션 GIF의 크기를 조정하려고합니다. 크기를 조정 한 후에도 애니메이션을 유지해야합니다.NodeJS 애니메이션 크기 조정

내 응용 프로그램은 현재 Sharp을 사용하여 다른 이미지의 크기를 조정하지만 애니메이션 GIF 내보내기는 지원하지 않는 것으로 보입니다.

가능한 경우 * Magick과 같은 다른 외부 소프트웨어에 의존하지 않는 npm 패키지를 찾고 싶습니다. 스트림을 지원하는 경우 정말 멋지게 될 것입니다.

답변

3

기본 npm 패키지에 대해 모르겠으나 gifsicle을 사용할 수 있습니다.

Gifsicle은 GIF 이미지와 애니메이션에 대한 정보를 만들고 편집하고 가져 오는 명령 줄 도구입니다. https://davidwalsh.name/resize-animated-gif

예 1 : 나는 gifsicle의 바이너리 파일 설치 gifsicle 모듈을 사용 :

이 gifsicle 사용하여 애니메이션 GIF의 크기를 조정에 대한 좋은 기사입니다

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'));