feat: add fitting querystring parameter
All checks were successful
Docker CI / release (push) Successful in 1m26s
All checks were successful
Docker CI / release (push) Successful in 1m26s
This commit is contained in:
parent
654b8b5e64
commit
e2a2614123
|
|
@ -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:
|
||||
|
|
|
|||
22
index.ts
22
index.ts
|
|
@ -89,6 +89,7 @@ const homepage = html`
|
|||
<p>Then call the API as, for example:</p>
|
||||
<p><pre>/api/imgresize/200/200/https%3A%2F%2Fmemori.ai%2Flogo.png</pre></p>
|
||||
<p>You can also specify a format using the querystring <code>?format=</code> and indicating one of the following: avif, gif, heif, jpeg, jpg, jp2, pdf, png, svg, tiff, webp. Note: Experimental!</p>
|
||||
<p>Another querystring parameter is <code>?fit=</code> which can be one of the following: cover, contain, fill, inside, outside (Ref: <a href="https://sharp.pixelplumbing.com/api-resize#resize" rel="noopener noreferrer" target="_blank">sharp docs</a>). Default is <code>inside</code>.</p>
|
||||
</article>
|
||||
</main>
|
||||
</body>
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue