nodeca / probe-image-size

Get image size without full download. Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, PSD, ICO.
MIT License
978 stars 77 forks source link

Desn't work with NextJS `getStaticProps` #69

Closed maximousblk closed 2 years ago

maximousblk commented 2 years ago

I'm trying to use probe-image-size for getting image dimensions to use NextJS' <Image /> component. It works in dev (next dev) but throws the following error in prod (next build)

image

Any way to get around this?

puzrin commented 2 years ago

You can not use needle in browser (and can not fetch images from any domain, due securuty policy). If you need to analyze binary data - use /sync.js only.

maximousblk commented 2 years ago

@puzrin can you please give me an example of how to do that

maximousblk commented 2 years ago

nevermind, I figured it out: https://github.com/maximousblk/maximousblk.me/blob/41661f38e7403e4361237384e0cd62eab681e64c/lib/notion.ts#L144-L147

you have to download the whole image but that's the only way it works:

import probe from "probe-image-size/sync";

const img = await fetch("https://example.com/image.jpg").then((res) => res.arrayBuffer());
size = probe(new Buffer(img));
console.log(size);