I am using a JS cropper library. Once the cropping is done, the cropper prodives me a canvas so that I can get the data and upload the cropped picture to my server.
Currently it works fine but when converting the canvas to a PNG data url, as I want my output picture to be very large (it's a banner), the upload actually takes a lot of time so I prefer to go with JPEG which seems to compress better large pictures. But JPEG has no alpha channel so it seems I have to replace it with a background (I choose my color: white).
So, can someone tell me how to convert that canvas to a data url in JPEG when the original image is like this:
It is a cropped picture of a warning icon in PNG. The background is alpha and it has some black drawings.
I have done some attempts but it seems that when I draw a rectangle on the canvas it draws on top of my initial picture. I can't draw the rectangle before the picture gets added because the cropper lib provides me with the canvas already constructed.
canvasToDataUrl(cropper) {
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
return canvas.toDataURL("image/jpeg",1);
},
When I do this I only get a white picture at the end.
If I don't draw a rectangle and I just try to convert to JPEG then I get a black picture.