Skip to content

Commit afc4bd4

Browse files
conico974Nicolas Dorseuil
andauthored
Improve image resizing logic in fetchImage function (#3553)
Co-authored-by: Nicolas Dorseuil <[email protected]>
1 parent bcfa8d8 commit afc4bd4

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

packages/gitbook/src/routes/ogimage.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,20 @@ async function fetchImage(url: string, options?: ResizeImageOptions) {
398398

399399
try {
400400
const { width, height } = imageSize(buffer);
401-
return { src, width, height };
401+
// If we provide a width and height in the options, we always want to use them
402+
// The resize in cloudflare can fail and will fallback to the original size, which could stretch the image
403+
// If the image is smaller than the requested size, it will also return the original image
404+
if (
405+
(options?.width && options.width !== width) ||
406+
(options?.height && options.height !== height)
407+
) {
408+
return {
409+
src,
410+
width: options.width,
411+
height: options.height,
412+
};
413+
}
414+
return { src, width: width, height: height };
402415
} catch {
403416
return null;
404417
}

0 commit comments

Comments
 (0)