Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example of using jimp and gifwrap to resize and crop gif to gif #13

Open
wobeng opened this issue Oct 3, 2018 · 1 comment
Open

example of using jimp and gifwrap to resize and crop gif to gif #13

wobeng opened this issue Oct 3, 2018 · 1 comment

Comments

@wobeng
Copy link

wobeng commented Oct 3, 2018

Hi,

Does anyone have an example of using jimp and gifwrap to resize and crop gif to gif.

Thanks in advance

@jeffbseeking
Copy link

jeffbseeking commented May 31, 2020

This isn't exactly what you are looking for, but these (typescript) functions can replace Jimp.read and Jimp.getBufferAsync to allow you to convert the first frame of a gif into a Jimp image and back to a buffer.

We use it like:


readImage(imageUrl)
.then((image) => image.cover(size.width, size.height))
.then((image) => getBuffer(image))
.then((imageBuffer) => {
  // now you can write the image somewhere
});

Note: will only work with the first frame....


/* This is a way of handing Jimp.read that will also take gif format.
   Ideally, we wouldn't need this, but Jimp cant handle gifs natively */
const readImage = (imageUrl: string): Promise<Jimp> => {
  var promise = new Promise<Jimp>((resolve, reject) => {
    Jimp.read(imageUrl)
      .then((jimp) => {
        resolve(jimp);
      })
      .catch((err) => {
        if (err.toString() === 'Unsupported MIME type: image/gif') {
          GifUtil.read(imageUrl)
            .then((gif) => {
              let image = GifUtil.copyAsJimp(Jimp, gif.frames[0]);
              resolve(image);
            })
            .catch((giferror) => {
              reject(giferror);
            });
        } else {
          reject(err);
        }
      });
  });
  return promise;
};

/* This is a way of handing Jimp.getBufferAsync that will also handle gif format.
   Ideally, we wouldn't need this, but Jimp cant handle gifs natively */
const getBuffer = (image: Jimp): Promise<Buffer> => {
  var promise = new Promise<Buffer>((resolve, reject) => {
    image
      .getBufferAsync(image._originalMime)
      .then((buffer) => {
        resolve(buffer);
      })
      .catch((err) => {
        if (err.toString() === 'Unsupported MIME type: image/gif') {
          let bitmap = new BitmapImage(image.bitmap);
          GifUtil.quantizeDekker(bitmap, 256);
          let newFrame = new GifFrame(bitmap);
          let gifCodec = new GifCodec();
          gifCodec
            .encodeGif([newFrame], {})
            .then((gif) => {
              resolve(gif.buffer);
            })
            .catch((giferror) => {
              reject(giferror);
            });
        } else {
          reject(err);
        }
      });
  });
  return promise;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants