Hi! I’m new around here in the forum but have been tweaking a website project of mine in Wix Editor for a while.
I’m trying to externally import multiple images to my Wix Media and then add them to a Media Gallery field in one of my collections.
With the code below I get errors because the uploadResponse doesn’t have width nor height properties (shouldn’t it?), but if I substitute those parts for something like 100, then I successfully import all images and add them to my collection with a 100x100 size. But I still need the original dimensions of the images and I’m seeing no way of getting them…
upload.jsw
import { mediaManager } from 'wix-media-backend';
export async function uploadImageToWix(imageUrl, gameId) {
try {
const folderPath = `${gameId}`;
// Import image from external URL
const uploadResponse = await mediaManager.importFile(
folderPath,
imageUrl
);
const wixFileUrl = `wix:image://v1/${uploadResponse.fileName}/${uploadResponse.fileName}#originWidth=${uploadResponse.width}&originHeight=${uploadResponse.height}`; // PROBLEM HERE
return [wixFileUrl, uploadResponse.width, uploadResponse.height]; // PROBLEM HERE
} catch (error) {
console.error(`Error uploading image ${imageUrl}:`, error);
return null;
}
}
updateGames.jsw
import wixData from 'wix-data';
import { uploadImageToWix } from 'backend/upload';
async function uploadImages(imageUrls, gameId) {
let wixGalleryImages = [];
for (let i = 0; i < imageUrls.length; i++) {
const uploadedImage = await uploadImageToWix(imageUrls[i], gameId);
if (uploadedImage) {
wixGalleryImages.push(
{
description: "",
slug: "",
alt: "",
src: uploadedImage[0],
title: "",
type: "image",
settings: {
"width":uploadedImage[1], // PROBLEM HERE
"height":uploadedImage[2], // PROBLEM HERE
"focalPoint":[0.5,0.5]
}
}
);
}
}
return wixGalleryImages;
}
Can somebody point me in the right direction? How can I get the original dimensions of the images?
Thank you in advance for any help!