function convertImageResolution(img) {
var canvas = document.createElement("canvas");
if (img.width * img.height < 921600) { // Less than 480p
canvas.width = 1920;
canvas.height = 1080;
} else if (img.width * img.height > 2073600) { // Greater than 1080p
canvas.width = img.width;
canvas.height = img.height;
}
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
var dataURL = canvas.toDataURL("image/jpg");
return dataURL.replace(/^data:image\/(jpg);base64,/, "");
}
I need to adjust the resolution of an image before uploading it. If the image is less than 480p, I want to increase its size to 480p on canvas. If the image is greater than 1080p, I want to reduce it to 1080p on canvas.
Is there a way to set minimum and maximum width and height for the canvas, or any alternative method to modify the image's resolution before uploading?