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 bd4cd38..7abc05e 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/index.ts b/index.ts index c3c58dc..e326a22 100644 --- a/index.ts +++ b/index.ts @@ -89,6 +89,7 @@ const homepage = html`

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);