Kalpesh Kashyap
Kalpesh Kashyap

Reputation: 824

transparent image background change to black with Jimp

I am uploading image to aws s3 and before image upload I am resizing image using node jimp, all functionality is working but when I upload transparent image and then Jimp resize it and change background to black and want original image with resized.

here is code for jimp

Jimp.read(buf, (err, image) => {
            if (err)
                reject(err);
            else {
                image.resize(118, 66)
                    .quality(100)
                    .getBase64(Jimp.MIME_JPEG, (err, src) => {
                        if (err)
                            reject(err);
                        else
                            resolve(src);
                    });
            }
        });

I have tried by this options but still not working

                    .rgba(false)
                    .background(0x00ff001C)

does anyone have any solution for this Thanks in advance

Upvotes: 2

Views: 4775

Answers (2)

Naomi Messing
Naomi Messing

Reputation: 668

You should change background-color.

it affects only transparent background.

you do it this way jimpImage = await jimpImage.background(0xFFFFFFFF)

Upvotes: 3

Gaurav Mall
Gaurav Mall

Reputation: 502

Its because you are using JPEG as Mimetype for the buffer.

Simply change Jimp.MIME_JPEG to Jimp.MIME_PNG and you are sorted.

As shown below.

Jimp.read(buf, (err, image) => {
            if (err)
                reject(err);
            else {
                image.resize(118, 66)
                    .quality(100)
                    .getBase64(Jimp.MIME_PNG, (err, src) => {
                        if (err)
                            reject(err);
                        else
                            resolve(src);
                    });
            }
        });

Upvotes: 2

Related Questions