Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/core/image.ts
3557 views
1
/*
2
* image.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import { existsSync } from "../deno_ral/fs.ts";
8
import { extname } from "../deno_ral/path.ts";
9
import PngImage from "./png.ts";
10
11
export function imageSize(path: string) {
12
if (path !== undefined) {
13
if (path.endsWith(".png")) {
14
if (existsSync(path)) {
15
const imageData = Deno.readFileSync(path);
16
try {
17
const png = new PngImage(imageData);
18
return {
19
height: png.height,
20
width: png.width,
21
};
22
} catch (error) {
23
if (!(error instanceof Error)) throw error;
24
throw new Error(`Error reading file ${path}\n${error.message}`);
25
}
26
}
27
}
28
}
29
}
30
31
export function imageContentType(path: string) {
32
const ext = extname(path);
33
return kimageTypes[ext];
34
}
35
36
// From
37
// https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types
38
const kimageTypes: Record<string, string> = {
39
".apng": "image/apng",
40
".avif": "image/avif",
41
".gif": "image/gif",
42
".jpg": "image/jpeg",
43
".jpeg": "image/jpeg",
44
".jfif": "image/jpeg",
45
".pjpeg": "image/jpeg",
46
".pjp": "image/jpeg",
47
".png": "image/png",
48
".svg": "image/svg+xml",
49
".webp": "image/webp",
50
};
51
52