preco21 / merge-img

Merge multiple images into a single image
MIT License
94 stars 29 forks source link

Jimp.getBufferAsync() is undefined #19

Open JaydenLiang opened 2 years ago

JaydenLiang commented 2 years ago

issue: in the latest version, built-in function getBufferAsync not found in the returned jimp instance. code example: screenshot = await mergeImg(screenshots, { direction: true }).then(result => { return result.getBufferAsync(result.MIME_PNG); });

seems like the jimp versionused in this project was out-of-date.

huksley commented 2 years ago

You don't need this library, just use

/** Merge images vertically, Copyright (C) https://github.com/PiPinecone/Image-Amalgamator/blob/main/src/index.js */
const mergeImages = async (imgs: Buffer[]) => {
  let finalWidth = 0;
  let finalHeight = 0;
  let heightArr = [0];

  // Calculate dimensions of final image and collect individual widths
  for (let i = 0; i < imgs.length; i++) {
    let image = await Jimp.read(imgs[i]);
    let height = image.bitmap.height;
    finalHeight += height;
    heightArr.push(finalHeight);

    let width = image.bitmap.width;
    if (width > finalWidth) {
      finalWidth = width;
    }
  }

  // Create a transparent image and paste images on top of it
  let back = new Jimp(finalWidth, finalHeight);
  for (let i = 0; i < imgs.length; i++) {
    back.blit(await Jimp.read(imgs[i]), 0, heightArr[i]);
  }

  return back.getBufferAsync("image/png");
};