Repository navigation
Expand file tree
/
Copy pathencode.ts
More file actions
44 lines (41 loc) · 1.42 KB
/
Copy pathencode.ts
File metadata and controls
44 lines (41 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import sharp from "sharp";
import type { Encoder } from "../src/reencode.ts";
/**
* JPEG at quality 88 for an opaque image, WebP for one that is see-through,
* turned upright by its EXIF orientation and fitted inside `edge` pixels.
*/
export const encode: Encoder = async (bytes, edge) => {
const { isOpaque } = await sharp(bytes, { limitInputPixels: false }).stats();
const image = sharp(bytes, { limitInputPixels: false }).rotate().resize({
width: edge,
height: edge,
fit: "inside",
withoutEnlargement: true,
});
const { data, info } = await (
isOpaque
? image.jpeg({ quality: 88, mozjpeg: true })
: image.webp({ quality: 88, alphaQuality: 100 })
).toBuffer({ resolveWithObject: true });
return {
bytes: new Uint8Array(data),
format: isOpaque ? "jpeg" : "webp",
width: info.width,
height: info.height,
};
};
/** A placeholder's longest side, in pixels: enough for a blur-up (as web/src/sync/bindings.ts makes them). */
const placeholderEdge = 16;
/** A tiny JPEG of the image as a data URL, for `images.placeholder`. */
export const placeholder = async (bytes: Uint8Array): Promise<string> => {
const data = await sharp(bytes, { limitInputPixels: false })
.rotate()
.resize({
width: placeholderEdge,
height: placeholderEdge,
fit: "inside",
})
.jpeg({ quality: 50 })
.toBuffer();
return `data:image/jpeg;base64,${data.toString("base64")}`;
};