I have many images in the cloud and need to get their dimensions. For this purpose, I decided to use sharp#metadata.
What minimum valuable bites do I need to fetch of a file to get this information? Or is it possible?
Types of files that I have: .jpeg, .png, .webp, .gif.
The code I'm using:
import sharp from 'sharp';
import axios from 'axios';
async function getDimensions(src: string, mime: string): Promise<{ width: number; height: number }> {
// TODO: get max range by mime type
const maxRange = 1024 * 1024;
// END TODO
const headers = {
Range: `bytes=${0}-${maxRange}`,
};
const response = await axios.get(src, {
headers,
responseType: 'arraybuffer',
});
const data = await sharp(response.data).metadata();
return {
width: data.width,
height: data.height,
};
}