I have this Preloaded JS function.
It does not work in Mobile because I apparently don't have access to the DOM. (Cannot read property 'createElement' of null)
window.createThumbnail = async function(base64Image, maxHeight = 100) {
return new Promise((resolve, reject) => {
const img = document.createElement('img');
img.src = "data:image/jpeg;base64," + base64Image;
img.onload = () => {
const aspectRatio = img.width / img.height;
let newWidth = img.width;
let newHeight = img.height;
// Check if the height needs to be limited
if (img.height > maxHeight) {
newHeight = maxHeight;
newWidth = maxHeight * aspectRatio;
}
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
// Set the canvas size to the new dimensions
canvas.width = newWidth;
canvas.height = newHeight;
// Draw the image onto the canvas with the new dimensions
ctx.drawImage(img, 0, 0, newWidth, newHeight);
// Convert the canvas content to a base64 thumbnail
const thumbnailBase64 = canvas.toDataURL("image/jpeg");
resolve(thumbnailBase64);
};
img.onerror = (error) => {
reject(error);
};
});
};
Is this a hard limitation, or is there a way around this?