Files
brandly/src/utils/mediaDimensions.ts
T

33 lines
1.0 KiB
TypeScript

export async function detectMediaDimensionsAndAspect(
url: string,
type: 'video' | 'image'
): Promise<{ width: number; height: number; format: 'video' | 'image'; aspect: string }> {
return new Promise((resolve, reject) => {
if (type === 'image') {
const img = new Image();
img.onload = () => {
resolve({
width: img.width,
height: img.height,
format: 'image',
aspect: `${img.width}:${img.height}`,
});
};
img.onerror = () => reject(new Error('Failed to load image to detect dimensions.'));
img.src = url;
} else {
const video = document.createElement('video');
video.onloadedmetadata = () => {
resolve({
width: video.videoWidth,
height: video.videoHeight,
format: 'video',
aspect: `${video.videoWidth}:${video.videoHeight}`,
});
};
video.onerror = () => reject(new Error('Failed to load video to detect dimensions.'));
video.src = url;
}
});
}