Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error cropping a base64 format image using jimp package in Nodejs

var base64str="data:image/jpeg;base64,***"//base64 format of the image
var buf = Buffer.from(base64str, 'base64');
  
jimp.read(buf, (err, image) => {
  if (err) throw err;
  else {
    image.crop(140, 50, 200, 280)
      .quality(100)
      .getBase64(jimp.MIME_JPEG, function(err, src) {
        console.log("rb is \n")
        console.log(src);
      })
  }
})

I am trying to use the jimp package from npm to crop the base64 format of an image but I am getting an error as follows:

Error: Could not find MIME for Buffer <null>
    at Jimp.parseBitmap (D:\Node\image-crop\node_modules\@jimp\core\dist\utils\image-bitmap.js:108:15)
    at new Jimp (D:\Node\image-crop\node_modules\@jimp\core\dist\index.js:425:32)
    at _construct (D:\Node\image-crop\node_modules\@jimp\core\dist\index.js:100:393)
    at D:\Node\image-crop\node_modules\@jimp\core\dist\index.js:932:5
    at new Promise (<anonymous>)
    at Function.Jimp.read (D:\Node\image-crop\node_modules\@jimp\core\dist\index.js:931:10)
    at Object.<anonymous> (D:\Node\image-crop\index.js:46:6)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)

Is there any way to crop an image which is in base64 format without converting it into an image in Nodejs?

like image 405
Veda Vyas Avatar asked Sep 19 '18 07:09

Veda Vyas


People also ask

What is Jimp in Node JS?

In a Node.js application, Javascript Image Manipulation Program (or JIMP) makes it easier to manipulate images to achieve whatever design we want. JIMP provides the functionality to crop, resize, blur, and add effects to images.

Can Jimp create a base 64 string from an image?

One of the JIMP library's many features is the ability to create a Base 64 string from an image. This has various uses (some nefarious...) but in this post I will demonstrate embedding a Base 64 encoded graphic as an image source in an HTML document.

How to crop an image in base64 format without converting it?

Is there any way to crop an image which is in base64 format without converting it into an image in Nodejs? The problem is from the base64 string. To make it work, just drop the prefix data:image/jpeg;base64,, and just leave the data.

How do I crop an image in Node JS?

NodeJS – Crop () is an inbuilt function that is used to crop the images. We can use crop to select/crop an image within specified coordinates and dimnesions. x – It will hold the value of x co-ordinate of cropping.


1 Answers

The problem is from the base64 string. To make it work, just drop the prefix data:image/jpeg;base64,, and just leave the data.

Example :

const base64str = "R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs="//base64 of a 1x1 black pixel
const buf = Buffer.from(base64str, 'base64');

jimp.read(buf, (err, image) => {
  if (err) throw err;
  else {
    image.crop(140, 50, 200, 280)
      .quality(100)
      .getBase64(jimp.MIME_JPEG, function (err, src) {
        console.log("rb is \n")
        console.log(src);
      })
  }
})

Output is :

rb is

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA[...]

like image 162
Seblor Avatar answered Sep 24 '22 20:09

Seblor