I am trying to create a function that takes in base 64 image data with a width and height and return a resized base 64 image data with the given width and height in javascript. Essentially it is
function imageResize(img, width, height) {
var newImg;
//PLAN:transform img with new width and height.
// - change width
// - change height
// - convert back to base 64 data
return newImg;
}
I tired following this post but it didn't work out for me. I am currently doing research to find ways to transform images in js and will update post as I find new leads. Is there a function that already does what I am trying to do? If not one or more of the components in my pseudocode? Any advice is much appreciated.
Here is the current function that I've tried to implement but it returns a blank canvas without the actual img drawn on it.
function imageResize(img, width, height) {
/// create an off-screen canvas
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
/// set its dimension to target size
canvas.width = width;
canvas.height = height;
/// draw source image into the off-screen canvas:
ctx.drawImage(img, 0, 0, width, height);
/// encode image to data-uri with base64 version of compressed image
return canvas.toDataURL();
}
How do I solve this issue?