From e2a26141235458dc99a0138a26099d2c2383924d Mon Sep 17 00:00:00 2001 From: nzambello Date: Mon, 10 Mar 2025 09:24:20 +0200 Subject: [PATCH] feat: add fitting querystring parameter --- README.md | 3 +++ bun.lockb | Bin 15964 -> 15956 bytes index.ts | 22 +++++++++++++++------- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 648130d..da08b03 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,9 @@ Then call the API as, for example: You can also specify a format using the querystring `?format=` and indicating one of the following: `avif`, `gif`, `heif`, `jpeg`, `jpg`, `pdf`, `png`, `svg`, `tiff`, `webp`. (Note: Experimental!) +Another querystring parameter is `fit` which can be `cover`, `contain`, `fill`, `inside`, `outside` (Ref: [sharp](https://sharp.pixelplumbing.com/api-resize#resize)). Default is `inside`. + + ## Docker To build the Docker image: diff --git a/bun.lockb b/bun.lockb index bd4cd38d7eac8e2abf6e7e8ee62ced239253af9b..7abc05e180bead98d77df7ca8de431716be80133 100755 GIT binary patch delta 481 zcmcapbERg2ALEgY{%!n>jFYeNhXYAH0T9^+ChvhsGeM9@ADDawChdekBGbU+J22@c z3=)|KCclA6KM|0~GBEiMOooYqMAm^xHZc%84oq$XlYHVJku)&54@`h5_0x=s9>jJSJ5K95E8W5`k zu?i3y0I@U>YXh++5UT>Q77z!3I6xqxH91d1X>x?-5l#<~I1o5(R@G8vRiB^_;a-5! zTsFn2xn-$Edae~EsYQ8-Il(1GnR)4x^Yqmx&(L?^1X~5v(z5xX{%&5b1GW(DM{L0s IoUok&0JMy38~^|S delta 494 zcmcaobEjs4ALE&g{%!n>43n?%hXYAH0T9^+ChvhsGeM9@ADDawChdekBGbU+J22@c z3=)|KCclA6KM|0~GBEiMOooYqMAm^xHZc%84oq$XlYHVJku)&54@`__`m-U0Ai~H@dT(c9nHylno5&9G(Then call the API as, for example:

/api/imgresize/200/200/https%3A%2F%2Fmemori.ai%2Flogo.png

You can also specify a format using the querystring ?format= and indicating one of the following: avif, gif, heif, jpeg, jpg, jp2, pdf, png, svg, tiff, webp. Note: Experimental!

+

Another querystring parameter is ?fit= which can be one of the following: cover, contain, fill, inside, outside (Ref: sharp docs). Default is inside.

@@ -128,7 +129,18 @@ app.post("/api/preview", async (c) => { app.get("/api/imgresize/:width/:height/:url", async (c) => { const { width, height, url } = c.req.param(); - const format = c.req.query("format"); + const fmtParam = c.req.query("format"); + const fitParam = c.req.query("fit"); + + const format = + fmtParam && + sharp.format[fmtParam as keyof typeof sharp.format] !== undefined + ? sharp.format[fmtParam as keyof typeof sharp.format] + : sharp.format.jpeg; + const fit = + fitParam && sharp.fit[fitParam as keyof typeof sharp.fit] !== undefined + ? sharp.fit[fitParam as keyof typeof sharp.fit] + : sharp.fit.inside; c.header("Cache-Control", "s-maxage=31536000, stale-while-revalidate"); c.header("Content-Type", `image/jpeg`); @@ -148,14 +160,10 @@ app.get("/api/imgresize/:width/:height/:url", async (c) => { .resize({ width: w, height: h, - fit: sharp.fit.inside, + fit: fit, withoutEnlargement: true, }) - .toFormat( - format && sharp.format[format as keyof typeof sharp.format] !== undefined - ? sharp.format[format as keyof typeof sharp.format] - : sharp.format.jpeg - ) + .toFormat(format) .toBuffer(); c.status(200);