@prunellejs/images 0.7.2 → 0.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/llms.txt +2 -2
- package/package.json +2 -2
- package/src/card.ts +59 -10
- package/src/dimensions.ts +351 -27
- package/src/metadata.ts +75 -15
- package/src/pipeline.ts +2 -2
- package/src/plugin.ts +19 -6
package/llms.txt
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
- `widths`: responsive widths in px (default `[320, 640, 960, 1280, 1920]`).
|
|
13
13
|
- `formats`: `('avif' | 'webp')[]` (default `['avif', 'webp']`; uses ImageMagick `magick` on `PATH`, falls back to original file with parsed dimensions when unavailable). Each image is decoded once per format, one `magick` run writing all its widths; runs share one budget for the whole build (half the cores, at most 8 `magick` at once), each limited to one thread and 512 MiB and killed after 2 min; a failed run warns with the file, the widths and `magick`'s error and leaves those variants out until the next build retries them (the original ships when none succeeds).
|
|
14
|
-
- Every image the build ships, original included, loses its EXIF (GPS position, camera serial), XMP, IPTC and text metadata;
|
|
14
|
+
- Every image the build ships, original included, loses its EXIF (GPS position, camera serial), XMP, IPTC and text metadata; JPEG, PNG and WebP keep only their Orientation, and every variant keeps its ICC colour profile (`magick ... +profile "!icc,*"`, not `-strip`). `prunelle check` warns about each JPEG, PNG or WebP under `public/` still carrying EXIF, XMP or IPTC, since `public/` is copied as is.
|
|
15
15
|
- `quality` (WebP, default `80`), `avifQuality` (default `55`), `cacheDir` (default `'.prunelle-cache/images'`), `pruneCache` (default `true`: after a build that prerendered pages, `og-` and `md-` cache files no page registered are deleted, together with the imported-image cache the build did not load). The runtime `/og/` and `/img/` stores are LRUs of twice the files the build registered, at least 256 and 4096.
|
|
16
16
|
- `card`: `ShareCardDefaults` (`{ site?, publicDir?, cacheDir?, background?, color?, accent?, mutedColor?, fontFamily?, width? (1200), height? (630) }`).
|
|
17
17
|
- `markdownSizes`: default `sizes` attribute for relative images inside `@prunellejs/content` Markdown (auto-wired via `CONTENT_IMAGE_RESOLVER_SERVICE`).
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
- `image(asset, { sizes?, priority? })`: returns `ImgSpreadProps` (`src`, `srcset`, `sizes`, `width`, `height`, `loading`, `decoding`, `fetchpriority`) for `<img {...image(hero)} alt="..." />`.
|
|
23
23
|
- `shareCard({ title, subtitle?, badge?, image?, background?, color?, accent? })`: renders and caches a PNG card under `public/og/*.png` (via `rsvg-convert` or `magick`) and returns its URL (prefixed with `site` when configured).
|
|
24
24
|
- `optimizeImageFile(absPath, options?)`: synchronously optimizes a local file into `dist/public/img/`.
|
|
25
|
-
- `readImageDimensions(bytes)`: `{ width, height }` from a PNG, GIF, WebP, JPEG or
|
|
25
|
+
- `readImageDimensions(bytes)`: `{ width, height }` from a PNG, GIF, WebP, JPEG, AVIF, BMP, ICO, TIFF or SVG header, with no process, as the image is displayed: rotated JPEG/PNG/WebP/TIFF (EXIF Orientation 5–8) and AVIF (`irot` 90°/270°) swap width and height to match their displayed orientation and encoded variants.
|
|
26
26
|
|
|
27
27
|
## Snippets
|
|
28
28
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunellejs/images",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.4",
|
|
4
4
|
"description": "Build-time responsive AVIF/WebP image optimization plugin for Prunelle",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -22,6 +22,6 @@
|
|
|
22
22
|
"directory": "packages/plugin-images"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"prunelle": "^0.7.
|
|
25
|
+
"prunelle": "^0.7.4"
|
|
26
26
|
}
|
|
27
27
|
}
|
package/src/card.ts
CHANGED
|
@@ -69,24 +69,73 @@ function getCardConfig(): ShareCardDefaults {
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
|
|
72
|
-
function
|
|
72
|
+
function splitTokens(text: string, maxChars: number): string[] {
|
|
73
73
|
const words = text.trim().split(/\s+/).filter(Boolean)
|
|
74
|
+
const tokens: string[] = []
|
|
75
|
+
for (const w of words) {
|
|
76
|
+
const chars = Array.from(w)
|
|
77
|
+
if (chars.length <= maxChars) {
|
|
78
|
+
tokens.push(w)
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
for (let i = 0; i < chars.length; i += maxChars)
|
|
82
|
+
tokens.push(chars.slice(i, i + maxChars).join(''))
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return tokens
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function addEllipsis(line: string, maxChars: number): string {
|
|
89
|
+
const chars = Array.from(line)
|
|
90
|
+
if (line.endsWith('…') && chars.length <= maxChars)
|
|
91
|
+
return line
|
|
92
|
+
if (chars.length < maxChars)
|
|
93
|
+
return `${line}…`
|
|
94
|
+
if (maxChars <= 1)
|
|
95
|
+
return '…'
|
|
96
|
+
const lastSpace = line.lastIndexOf(' ')
|
|
97
|
+
if (lastSpace > 0 && Array.from(line.slice(0, lastSpace)).length < maxChars)
|
|
98
|
+
return `${line.slice(0, lastSpace).trimEnd()}…`
|
|
99
|
+
return `${chars.slice(0, maxChars - 1).join('').trimEnd()}…`
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function wrapLines(text: string, maxChars: number, maxLines: number): string[] {
|
|
103
|
+
if (maxChars < 1 || maxLines < 1)
|
|
104
|
+
return []
|
|
105
|
+
const tokens = splitTokens(text, maxChars)
|
|
106
|
+
if (tokens.length === 0)
|
|
107
|
+
return []
|
|
108
|
+
|
|
74
109
|
const lines: string[] = []
|
|
75
110
|
let cur = ''
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
111
|
+
let curLen = 0
|
|
112
|
+
let truncated = false
|
|
113
|
+
|
|
114
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
115
|
+
const token = tokens[i]!
|
|
116
|
+
const tokenLen = Array.from(token).length
|
|
117
|
+
if (curLen === 0) {
|
|
118
|
+
cur = token
|
|
119
|
+
curLen = tokenLen
|
|
120
|
+
}
|
|
121
|
+
else if (curLen + 1 + tokenLen <= maxChars) {
|
|
122
|
+
cur = `${cur} ${token}`
|
|
123
|
+
curLen += 1 + tokenLen
|
|
124
|
+
}
|
|
125
|
+
else if (lines.length < maxLines - 1) {
|
|
79
126
|
lines.push(cur)
|
|
80
|
-
cur =
|
|
81
|
-
|
|
82
|
-
break
|
|
127
|
+
cur = token
|
|
128
|
+
curLen = tokenLen
|
|
83
129
|
}
|
|
84
130
|
else {
|
|
85
|
-
|
|
131
|
+
truncated = true
|
|
132
|
+
break
|
|
86
133
|
}
|
|
87
134
|
}
|
|
88
|
-
|
|
89
|
-
|
|
135
|
+
|
|
136
|
+
if (cur)
|
|
137
|
+
lines.push(truncated ? addEllipsis(cur, maxChars) : cur)
|
|
138
|
+
|
|
90
139
|
return lines
|
|
91
140
|
}
|
|
92
141
|
|
package/src/dimensions.ts
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
// Image sizes read from the file header, with no decoder and no process.
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const SVG_OPEN_TAG_RE = /<svg\b((?:[^<>"']|"[^"]*"|'[^']*')*)\/?>/i
|
|
4
|
+
const SVG_ATTR_RE = /\b(width|height|viewBox)\s*=\s*(?:"([^"]*)"|'([^']*)')/gi
|
|
5
|
+
const SVG_PX_RE = /^\s*(\d+(?:\.\d+)?)(?:px)?\s*$/i
|
|
6
|
+
const SVG_BOX_SEP_RE = /[\s,]+/
|
|
7
|
+
|
|
8
|
+
/** Pure-JS binary header parser for PNG, GIF, WebP, JPEG, AVIF/HEIF, BMP, ICO, TIFF, and SVG dimensions (zero external dependencies). */
|
|
4
9
|
export function readImageDimensions(bytes: Uint8Array): { width: number, height: number } | undefined {
|
|
5
10
|
const len = bytes.byteLength
|
|
6
|
-
if (len <
|
|
11
|
+
if (len < 6)
|
|
7
12
|
return undefined
|
|
8
13
|
const view = new DataView(bytes.buffer, bytes.byteOffset, len)
|
|
9
14
|
|
|
10
|
-
// PNG: 89 50 4E 47 0D 0A 1A 0A, IHDR width@16, height@20 (uint32BE)
|
|
11
|
-
if (len >= 24 && view.getUint32(0) === 0x89504E47 && view.getUint32(4) === 0x0D0A1A0A)
|
|
12
|
-
|
|
13
|
-
const height = view.getUint32(20)
|
|
14
|
-
return width > 0 && height > 0 ? { width, height } : undefined
|
|
15
|
-
}
|
|
15
|
+
// PNG: 89 50 4E 47 0D 0A 1A 0A, IHDR width@16, height@20 (uint32BE) + optional eXIf/orNT orientation
|
|
16
|
+
if (len >= 24 && view.getUint32(0) === 0x89504E47 && view.getUint32(4) === 0x0D0A1A0A)
|
|
17
|
+
return pngDimensions(bytes, view)
|
|
16
18
|
|
|
17
19
|
// GIF87a / GIF89a: width@6, height@8 (uint16LE)
|
|
18
|
-
if (bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x38 && (bytes[4] === 0x37 || bytes[4] === 0x39) && bytes[5] === 0x61) {
|
|
20
|
+
if (len >= 10 && bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x38 && (bytes[4] === 0x37 || bytes[4] === 0x39) && bytes[5] === 0x61) {
|
|
19
21
|
const width = view.getUint16(6, true)
|
|
20
22
|
const height = view.getUint16(8, true)
|
|
21
23
|
return width > 0 && height > 0 ? { width, height } : undefined
|
|
@@ -27,10 +29,59 @@ export function readImageDimensions(bytes: Uint8Array): { width: number, height:
|
|
|
27
29
|
return jpegDimensions(bytes, view)
|
|
28
30
|
if (len >= 24 && view.getUint32(4) === 0x66747970)
|
|
29
31
|
return isobmffDimensions(bytes, view)
|
|
30
|
-
|
|
32
|
+
if (len >= 26 && bytes[0] === 0x42 && bytes[1] === 0x4D)
|
|
33
|
+
return bmpDimensions(view)
|
|
34
|
+
if (len >= 22 && view.getUint32(0) === 0x00000100)
|
|
35
|
+
return icoDimensions(bytes, view)
|
|
36
|
+
if (len >= 8 && ((bytes[0] === 0x49 && bytes[1] === 0x49 && bytes[2] === 0x2A && bytes[3] === 0x00) || (bytes[0] === 0x4D && bytes[1] === 0x4D && bytes[2] === 0x00 && bytes[3] === 0x2A)))
|
|
37
|
+
return tiffDimensions(view, len)
|
|
38
|
+
return svgDimensions(bytes)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function applyOrientation(width: number, height: number, orientation: number): { width: number, height: number } {
|
|
42
|
+
return orientation >= 5 && orientation <= 8 ? { width: height, height: width } : { width, height }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Reads EXIF orientation from a payload that holds either raw TIFF bytes (`II`/`MM`) or an `Exif\0\0` prefix followed by TIFF. */
|
|
46
|
+
function rawOrPrefixedExifOrientation(view: DataView, start: number, end: number): number {
|
|
47
|
+
if (end - start >= 14 && view.getUint32(start) === 0x45786966 && view.getUint16(start + 4) === 0)
|
|
48
|
+
return tiffIfdInfo(view, start + 6, end).orientation
|
|
49
|
+
return tiffIfdInfo(view, start, end).orientation
|
|
31
50
|
}
|
|
32
51
|
|
|
33
|
-
/**
|
|
52
|
+
/** PNG: IHDR width/height, swapped when an eXIf or ImageMagick orNT chunk carries orientation 5..8. */
|
|
53
|
+
function pngDimensions(bytes: Uint8Array, view: DataView): { width: number, height: number } | undefined {
|
|
54
|
+
const width = view.getUint32(16)
|
|
55
|
+
const height = view.getUint32(20)
|
|
56
|
+
if (width === 0 || height === 0)
|
|
57
|
+
return undefined
|
|
58
|
+
const len = bytes.byteLength
|
|
59
|
+
let orientation = 1
|
|
60
|
+
let at = 8
|
|
61
|
+
while (at + 12 <= len) {
|
|
62
|
+
const size = view.getUint32(at)
|
|
63
|
+
const dataStart = at + 8
|
|
64
|
+
const dataEnd = dataStart + size
|
|
65
|
+
if (size > len - at - 12)
|
|
66
|
+
break
|
|
67
|
+
const type = view.getUint32(at + 4)
|
|
68
|
+
if (type === 0x65584966) {
|
|
69
|
+
// Standard PNG eXIf chunk (raw TIFF, or Exif\0\0 + TIFF).
|
|
70
|
+
orientation = rawOrPrefixedExifOrientation(view, dataStart, dataEnd)
|
|
71
|
+
}
|
|
72
|
+
else if (type === 0x6F724E54 && size >= 1 && orientation === 1) {
|
|
73
|
+
// ImageMagick orNT chunk: single-byte EXIF orientation (1..8).
|
|
74
|
+
orientation = bytes[dataStart]!
|
|
75
|
+
}
|
|
76
|
+
else if (type === 0x49454E44) {
|
|
77
|
+
break
|
|
78
|
+
}
|
|
79
|
+
at = dataEnd + 4
|
|
80
|
+
}
|
|
81
|
+
return applyOrientation(width, height, orientation)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** WebP, RIFF....WEBP: the VP8X, VP8L or VP8 chunk's size, swapped when a VP8X EXIF chunk carries orientation 5..8. */
|
|
34
85
|
function webpDimensions(bytes: Uint8Array, view: DataView): { width: number, height: number } | undefined {
|
|
35
86
|
const len = bytes.byteLength
|
|
36
87
|
const chunk = view.getUint32(12)
|
|
@@ -38,7 +89,19 @@ function webpDimensions(bytes: Uint8Array, view: DataView): { width: number, hei
|
|
|
38
89
|
if (chunk === 0x56503858) {
|
|
39
90
|
const width = 1 + (bytes[24]! | (bytes[25]! << 8) | (bytes[26]! << 16))
|
|
40
91
|
const height = 1 + (bytes[27]! | (bytes[28]! << 8) | (bytes[29]! << 16))
|
|
41
|
-
|
|
92
|
+
let orientation = 1
|
|
93
|
+
let at = 12
|
|
94
|
+
while (at + 8 <= len) {
|
|
95
|
+
const size = view.getUint32(at + 4, true)
|
|
96
|
+
if (size > len - at - 8)
|
|
97
|
+
break
|
|
98
|
+
if (view.getUint32(at) === 0x45584946) {
|
|
99
|
+
orientation = rawOrPrefixedExifOrientation(view, at + 8, at + 8 + size)
|
|
100
|
+
break
|
|
101
|
+
}
|
|
102
|
+
at += 8 + size + (size & 1)
|
|
103
|
+
}
|
|
104
|
+
return applyOrientation(width, height, orientation)
|
|
42
105
|
}
|
|
43
106
|
// VP8L lossless
|
|
44
107
|
if (chunk === 0x5650384C && len >= 25 && bytes[20] === 0x2F) {
|
|
@@ -54,9 +117,166 @@ function webpDimensions(bytes: Uint8Array, view: DataView): { width: number, hei
|
|
|
54
117
|
return undefined
|
|
55
118
|
}
|
|
56
119
|
|
|
57
|
-
|
|
120
|
+
interface IsoBox {
|
|
121
|
+
type: number
|
|
122
|
+
payloadStart: number
|
|
123
|
+
end: number
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function readIsoBox(view: DataView, at: number, limit: number): IsoBox | undefined {
|
|
127
|
+
if (at + 8 > limit)
|
|
128
|
+
return undefined
|
|
129
|
+
let size = view.getUint32(at)
|
|
130
|
+
const type = view.getUint32(at + 4)
|
|
131
|
+
let headerLen = 8
|
|
132
|
+
if (size === 0) {
|
|
133
|
+
size = limit - at
|
|
134
|
+
}
|
|
135
|
+
else if (size === 1) {
|
|
136
|
+
if (at + 16 > limit)
|
|
137
|
+
return undefined
|
|
138
|
+
const hi = view.getUint32(at + 8)
|
|
139
|
+
const lo = view.getUint32(at + 12)
|
|
140
|
+
if (hi > 0x1FFFFF)
|
|
141
|
+
return undefined
|
|
142
|
+
size = hi * 0x100000000 + lo
|
|
143
|
+
headerLen = 16
|
|
144
|
+
}
|
|
145
|
+
if (size < headerLen || at + size > limit)
|
|
146
|
+
return undefined
|
|
147
|
+
return { type, payloadStart: at + headerLen, end: at + size }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function parseIpmaBox(bytes: Uint8Array, view: DataView, start: number, end: number, assocs: Map<number, number[]>): void {
|
|
151
|
+
if (start + 8 > end)
|
|
152
|
+
return
|
|
153
|
+
const version = bytes[start]!
|
|
154
|
+
const largeProp = (bytes[start + 3]! & 1) !== 0
|
|
155
|
+
const entryCount = view.getUint32(start + 4)
|
|
156
|
+
let cur = start + 8
|
|
157
|
+
for (let e = 0; e < entryCount; e++) {
|
|
158
|
+
const idSize = version < 1 ? 2 : 4
|
|
159
|
+
if (cur + idSize + 1 > end)
|
|
160
|
+
return
|
|
161
|
+
const itemId = version < 1 ? view.getUint16(cur) : view.getUint32(cur)
|
|
162
|
+
cur += idSize
|
|
163
|
+
const assocCount = bytes[cur++]!
|
|
164
|
+
let list = assocs.get(itemId)
|
|
165
|
+
if (list === undefined) {
|
|
166
|
+
list = []
|
|
167
|
+
assocs.set(itemId, list)
|
|
168
|
+
}
|
|
169
|
+
for (let a = 0; a < assocCount; a++) {
|
|
170
|
+
if (largeProp) {
|
|
171
|
+
if (cur + 2 > end)
|
|
172
|
+
return
|
|
173
|
+
list.push(view.getUint16(cur) & 0x7FFF)
|
|
174
|
+
cur += 2
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
if (cur + 1 > end)
|
|
178
|
+
return
|
|
179
|
+
list.push(bytes[cur++]! & 0x7F)
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function parseIprpBox(
|
|
186
|
+
bytes: Uint8Array,
|
|
187
|
+
view: DataView,
|
|
188
|
+
start: number,
|
|
189
|
+
end: number,
|
|
190
|
+
ispes: Map<number, { width: number, height: number }>,
|
|
191
|
+
irots: Map<number, number>,
|
|
192
|
+
assocs: Map<number, number[]>,
|
|
193
|
+
): void {
|
|
194
|
+
let at = start
|
|
195
|
+
while (at < end) {
|
|
196
|
+
const box = readIsoBox(view, at, end)
|
|
197
|
+
if (box === undefined)
|
|
198
|
+
break
|
|
199
|
+
if (box.type === 0x6970636F) {
|
|
200
|
+
// ipco: 1-based property list
|
|
201
|
+
let pAt = box.payloadStart
|
|
202
|
+
let idx = 1
|
|
203
|
+
while (pAt < box.end) {
|
|
204
|
+
const prop = readIsoBox(view, pAt, box.end)
|
|
205
|
+
if (prop === undefined)
|
|
206
|
+
break
|
|
207
|
+
if (prop.type === 0x69737065 && prop.end - prop.payloadStart >= 12) {
|
|
208
|
+
const width = view.getUint32(prop.payloadStart + 4)
|
|
209
|
+
const height = view.getUint32(prop.payloadStart + 8)
|
|
210
|
+
if (width > 0 && height > 0 && width <= 65535 && height <= 65535)
|
|
211
|
+
ispes.set(idx, { width, height })
|
|
212
|
+
}
|
|
213
|
+
else if (prop.type === 0x69726F74 && prop.end - prop.payloadStart >= 1) {
|
|
214
|
+
irots.set(idx, bytes[prop.payloadStart]! & 0x03)
|
|
215
|
+
}
|
|
216
|
+
idx++
|
|
217
|
+
pAt = prop.end
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
else if (box.type === 0x69706D61) {
|
|
221
|
+
parseIpmaBox(bytes, view, box.payloadStart, box.end, assocs)
|
|
222
|
+
}
|
|
223
|
+
at = box.end
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** AVIF / HEIF (ISOBMFF): meta -> pitm + iprp (ipco ispe/irot + ipma), swapping width and height for 90°/270° irot. */
|
|
58
228
|
function isobmffDimensions(bytes: Uint8Array, view: DataView): { width: number, height: number } | undefined {
|
|
59
|
-
|
|
229
|
+
const len = bytes.byteLength
|
|
230
|
+
let at = 0
|
|
231
|
+
while (at < len) {
|
|
232
|
+
const box = readIsoBox(view, at, len)
|
|
233
|
+
if (box === undefined)
|
|
234
|
+
break
|
|
235
|
+
if (box.type === 0x6D657461 && box.payloadStart + 4 <= box.end) {
|
|
236
|
+
let primaryId: number | undefined
|
|
237
|
+
const ispes = new Map<number, { width: number, height: number }>()
|
|
238
|
+
const irots = new Map<number, number>()
|
|
239
|
+
const assocs = new Map<number, number[]>()
|
|
240
|
+
let mAt = box.payloadStart + 4
|
|
241
|
+
while (mAt < box.end) {
|
|
242
|
+
const child = readIsoBox(view, mAt, box.end)
|
|
243
|
+
if (child === undefined)
|
|
244
|
+
break
|
|
245
|
+
if (child.type === 0x7069746D && child.end - child.payloadStart >= 6) {
|
|
246
|
+
const version = bytes[child.payloadStart]!
|
|
247
|
+
if (version === 0)
|
|
248
|
+
primaryId = view.getUint16(child.payloadStart + 4)
|
|
249
|
+
else if (child.end - child.payloadStart >= 8)
|
|
250
|
+
primaryId = view.getUint32(child.payloadStart + 4)
|
|
251
|
+
}
|
|
252
|
+
else if (child.type === 0x69707270) {
|
|
253
|
+
parseIprpBox(bytes, view, child.payloadStart, child.end, ispes, irots, assocs)
|
|
254
|
+
}
|
|
255
|
+
mAt = child.end
|
|
256
|
+
}
|
|
257
|
+
const itemProps = primaryId !== undefined ? assocs.get(primaryId) : undefined
|
|
258
|
+
let dims: { width: number, height: number } | undefined
|
|
259
|
+
let angle = 0
|
|
260
|
+
if (itemProps !== undefined) {
|
|
261
|
+
for (const propIdx of itemProps) {
|
|
262
|
+
dims ??= ispes.get(propIdx)
|
|
263
|
+
const rot = irots.get(propIdx)
|
|
264
|
+
if (rot !== undefined)
|
|
265
|
+
angle = (angle + rot) & 3
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
dims = ispes.values().next().value
|
|
270
|
+
for (const rot of irots.values())
|
|
271
|
+
angle = (angle + rot) & 3
|
|
272
|
+
}
|
|
273
|
+
if (dims !== undefined)
|
|
274
|
+
return (angle & 1) === 1 ? { width: dims.height, height: dims.width } : dims
|
|
275
|
+
}
|
|
276
|
+
at = box.end
|
|
277
|
+
}
|
|
278
|
+
// Fallback for minimal fixtures that embed an 'ispe' box without a full meta container.
|
|
279
|
+
for (let i = 12; i + 16 <= len; i++) {
|
|
60
280
|
if (bytes[i] === 0x69 && bytes[i + 1] === 0x73 && bytes[i + 2] === 0x70 && bytes[i + 3] === 0x65) {
|
|
61
281
|
const width = view.getUint32(i + 8)
|
|
62
282
|
const height = view.getUint32(i + 12)
|
|
@@ -105,26 +325,130 @@ function jpegDimensions(bytes: Uint8Array, view: DataView): { width: number, hei
|
|
|
105
325
|
return undefined
|
|
106
326
|
}
|
|
107
327
|
|
|
108
|
-
/**
|
|
109
|
-
function
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return 1
|
|
113
|
-
const tiff = start + 6
|
|
328
|
+
/** Reads ImageWidth (0x0100), ImageLength (0x0101), and Orientation (0x0112) from a TIFF header's first IFD. */
|
|
329
|
+
function tiffIfdInfo(view: DataView, tiff: number, end: number): { width?: number | undefined, height?: number | undefined, orientation: number } {
|
|
330
|
+
if (end - tiff < 8)
|
|
331
|
+
return { orientation: 1 }
|
|
114
332
|
const order = view.getUint16(tiff)
|
|
115
333
|
if (order !== 0x4949 && order !== 0x4D4D)
|
|
116
|
-
return 1
|
|
334
|
+
return { orientation: 1 }
|
|
117
335
|
const little = order === 0x4949
|
|
336
|
+
if (view.getUint16(tiff + 2, little) !== 42)
|
|
337
|
+
return { orientation: 1 }
|
|
118
338
|
const ifd = tiff + view.getUint32(tiff + 4, little)
|
|
119
|
-
if (ifd + 2 > end)
|
|
120
|
-
return 1
|
|
339
|
+
if (ifd < tiff || ifd + 2 > end)
|
|
340
|
+
return { orientation: 1 }
|
|
121
341
|
const count = view.getUint16(ifd, little)
|
|
342
|
+
let width: number | undefined
|
|
343
|
+
let height: number | undefined
|
|
344
|
+
let orientation = 1
|
|
122
345
|
for (let i = 0; i < count; i++) {
|
|
123
346
|
const entry = ifd + 2 + i * 12
|
|
124
347
|
if (entry + 12 > end)
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
348
|
+
break
|
|
349
|
+
const tag = view.getUint16(entry, little)
|
|
350
|
+
const type = view.getUint16(entry + 2, little)
|
|
351
|
+
if (tag === 0x0112)
|
|
352
|
+
orientation = view.getUint16(entry + 8, little)
|
|
353
|
+
else if (tag === 0x0100)
|
|
354
|
+
width = type === 3 ? view.getUint16(entry + 8, little) : type === 4 ? view.getUint32(entry + 8, little) : undefined
|
|
355
|
+
else if (tag === 0x0101)
|
|
356
|
+
height = type === 3 ? view.getUint16(entry + 8, little) : type === 4 ? view.getUint32(entry + 8, little) : undefined
|
|
357
|
+
}
|
|
358
|
+
return { width, height, orientation }
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** The Orientation tag (0x0112) of an APP1 segment's first IFD, or 1 when it is no Exif segment, has none or is cut short. */
|
|
362
|
+
function exifOrientation(view: DataView, start: number, end: number): number {
|
|
363
|
+
if (end - start < 14 || view.getUint32(start) !== 0x45786966 || view.getUint16(start + 4) !== 0)
|
|
364
|
+
return 1
|
|
365
|
+
return tiffIfdInfo(view, start + 6, end).orientation
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function bmpDimensions(view: DataView): { width: number, height: number } | undefined {
|
|
369
|
+
const dibSize = view.getUint32(14, true)
|
|
370
|
+
const width = dibSize === 12 ? view.getUint16(18, true) : dibSize >= 40 ? view.getInt32(18, true) : 0
|
|
371
|
+
const height = dibSize === 12 ? view.getUint16(20, true) : dibSize >= 40 ? Math.abs(view.getInt32(22, true)) : 0
|
|
372
|
+
return width > 0 && height > 0 ? { width, height } : undefined
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function icoDimensions(bytes: Uint8Array, view: DataView): { width: number, height: number } | undefined {
|
|
376
|
+
const count = view.getUint16(4, true)
|
|
377
|
+
if (count === 0)
|
|
378
|
+
return undefined
|
|
379
|
+
let bestW = 0
|
|
380
|
+
let bestH = 0
|
|
381
|
+
for (let i = 0; i < count; i++) {
|
|
382
|
+
const entry = 6 + i * 16
|
|
383
|
+
if (entry + 16 > bytes.byteLength)
|
|
384
|
+
break
|
|
385
|
+
const w = bytes[entry] === 0 ? 256 : bytes[entry]!
|
|
386
|
+
const h = bytes[entry + 1] === 0 ? 256 : bytes[entry + 1]!
|
|
387
|
+
if (w * h > bestW * bestH) {
|
|
388
|
+
bestW = w
|
|
389
|
+
bestH = h
|
|
390
|
+
}
|
|
128
391
|
}
|
|
129
|
-
return
|
|
392
|
+
return bestW > 0 && bestH > 0 ? { width: bestW, height: bestH } : undefined
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function tiffDimensions(view: DataView, len: number): { width: number, height: number } | undefined {
|
|
396
|
+
const { width, height, orientation } = tiffIfdInfo(view, 0, len)
|
|
397
|
+
if (width === undefined || height === undefined || width <= 0 || height <= 0)
|
|
398
|
+
return undefined
|
|
399
|
+
return orientation >= 5 && orientation <= 8 ? { width: height, height: width } : { width, height }
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function parseSvgLength(value: string | undefined): number | undefined {
|
|
403
|
+
const match = value === undefined ? null : SVG_PX_RE.exec(value)
|
|
404
|
+
if (match === null)
|
|
405
|
+
return undefined
|
|
406
|
+
const num = Number(match[1])
|
|
407
|
+
return Number.isFinite(num) && num > 0 ? num : undefined
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function svgDimensions(bytes: Uint8Array): { width: number, height: number } | undefined {
|
|
411
|
+
let at = bytes[0] === 0xEF && bytes[1] === 0xBB && bytes[2] === 0xBF ? 3 : 0
|
|
412
|
+
while (at < bytes.byteLength && (bytes[at] === 0x20 || bytes[at] === 0x09 || bytes[at] === 0x0A || bytes[at] === 0x0D))
|
|
413
|
+
at++
|
|
414
|
+
if (at >= bytes.byteLength || bytes[at] !== 0x3C)
|
|
415
|
+
return undefined
|
|
416
|
+
const head = new TextDecoder().decode(bytes.subarray(at, Math.min(bytes.byteLength, at + 8192)))
|
|
417
|
+
const tag = SVG_OPEN_TAG_RE.exec(head)
|
|
418
|
+
if (tag === null)
|
|
419
|
+
return undefined
|
|
420
|
+
let width: number | undefined
|
|
421
|
+
let height: number | undefined
|
|
422
|
+
let viewBox: string | undefined
|
|
423
|
+
SVG_ATTR_RE.lastIndex = 0
|
|
424
|
+
for (const [, name, dbl, sgl] of tag[1]!.matchAll(SVG_ATTR_RE)) {
|
|
425
|
+
const val = dbl ?? sgl ?? ''
|
|
426
|
+
const lower = name!.toLowerCase()
|
|
427
|
+
if (lower === 'width')
|
|
428
|
+
width = parseSvgLength(val)
|
|
429
|
+
else if (lower === 'height')
|
|
430
|
+
height = parseSvgLength(val)
|
|
431
|
+
else if (lower === 'viewbox')
|
|
432
|
+
viewBox = val
|
|
433
|
+
}
|
|
434
|
+
if (width === undefined || height === undefined) {
|
|
435
|
+
const parts = viewBox?.trim().split(SVG_BOX_SEP_RE).map(Number)
|
|
436
|
+
if (parts !== undefined && parts.length === 4 && parts.every(Number.isFinite) && parts[2]! > 0 && parts[3]! > 0) {
|
|
437
|
+
const vw = parts[2]!
|
|
438
|
+
const vh = parts[3]!
|
|
439
|
+
if (width !== undefined)
|
|
440
|
+
height = width * (vh / vw)
|
|
441
|
+
else if (height !== undefined)
|
|
442
|
+
width = height * (vw / vh)
|
|
443
|
+
else {
|
|
444
|
+
width = vw
|
|
445
|
+
height = vh
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
if (width === undefined || height === undefined)
|
|
450
|
+
return undefined
|
|
451
|
+
const w = Math.round(width)
|
|
452
|
+
const h = Math.round(height)
|
|
453
|
+
return w > 0 && h > 0 ? { width: w, height: h } : undefined
|
|
130
454
|
}
|
package/src/metadata.ts
CHANGED
|
@@ -23,13 +23,19 @@ function concat(parts: Uint8Array[]): Uint8Array<ArrayBuffer> {
|
|
|
23
23
|
return out
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
// The EXIF Orientation tag of IFD0, so a stripped photo still displays the right way up.
|
|
26
|
+
// The EXIF Orientation tag of IFD0 (from either raw TIFF or an `Exif\0\0`-prefixed payload), so a stripped photo still displays the right way up.
|
|
27
27
|
function exifOrientation(payload: Uint8Array): number | undefined {
|
|
28
|
-
const
|
|
28
|
+
const hasPrefix = payload.length >= EXIF_HEADER.length && EXIF_HEADER.every((b, i) => payload[i] === b)
|
|
29
|
+
const tiff = hasPrefix ? payload.subarray(EXIF_HEADER.length) : payload
|
|
29
30
|
if (tiff.length < 8)
|
|
30
31
|
return undefined
|
|
31
32
|
const view = new DataView(tiff.buffer, tiff.byteOffset, tiff.byteLength)
|
|
32
|
-
const
|
|
33
|
+
const order = view.getUint16(0)
|
|
34
|
+
if (order !== 0x4949 && order !== 0x4D4D)
|
|
35
|
+
return undefined
|
|
36
|
+
const little = order === 0x4949
|
|
37
|
+
if (view.getUint16(2, little) !== 42)
|
|
38
|
+
return undefined
|
|
33
39
|
const ifd = view.getUint32(4, little)
|
|
34
40
|
if (ifd + 2 > tiff.length)
|
|
35
41
|
return undefined
|
|
@@ -44,16 +50,54 @@ function exifOrientation(payload: Uint8Array): number | undefined {
|
|
|
44
50
|
return undefined
|
|
45
51
|
}
|
|
46
52
|
|
|
47
|
-
//
|
|
48
|
-
function
|
|
53
|
+
// A minimal big-endian TIFF IFD0 carrying only the Orientation tag (26 bytes, even length).
|
|
54
|
+
function orientationTiff(orientation: number): Uint8Array {
|
|
49
55
|
return new Uint8Array([
|
|
50
|
-
0xFF, 0xE1, 0x00, 0x22, ...EXIF_HEADER,
|
|
51
56
|
0x4D, 0x4D, 0x00, 0x2A, 0x00, 0x00, 0x00, 0x08,
|
|
52
57
|
0x00, 0x01, 0x01, 0x12, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, orientation >> 8, orientation & 0xFF, 0x00, 0x00,
|
|
53
58
|
0x00, 0x00, 0x00, 0x00,
|
|
54
59
|
])
|
|
55
60
|
}
|
|
56
61
|
|
|
62
|
+
// An APP1 segment holding only an Orientation tag.
|
|
63
|
+
function orientationSegment(orientation: number): Uint8Array {
|
|
64
|
+
return new Uint8Array([0xFF, 0xE1, 0x00, 0x22, ...EXIF_HEADER, ...orientationTiff(orientation)])
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const CRC32_TABLE = new Uint32Array(256)
|
|
68
|
+
for (let n = 0; n < 256; n++) {
|
|
69
|
+
let c = n
|
|
70
|
+
for (let k = 0; k < 8; k++) c = (c & 1) !== 0 ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1)
|
|
71
|
+
CRC32_TABLE[n] = c >>> 0
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function crc32(bytes: Uint8Array): number {
|
|
75
|
+
let c = 0xFFFFFFFF
|
|
76
|
+
for (let i = 0; i < bytes.length; i++)
|
|
77
|
+
c = CRC32_TABLE[(c ^ bytes[i]!) & 0xFF]! ^ (c >>> 8)
|
|
78
|
+
return (c ^ 0xFFFFFFFF) >>> 0
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function pngOrientationChunk(orientation: number): Uint8Array {
|
|
82
|
+
const tiff = orientationTiff(orientation)
|
|
83
|
+
const chunk = new Uint8Array(12 + tiff.length)
|
|
84
|
+
const view = new DataView(chunk.buffer)
|
|
85
|
+
view.setUint32(0, tiff.length)
|
|
86
|
+
chunk.set([0x65, 0x58, 0x49, 0x66], 4)
|
|
87
|
+
chunk.set(tiff, 8)
|
|
88
|
+
view.setUint32(8 + tiff.length, crc32(chunk.subarray(4, 8 + tiff.length)))
|
|
89
|
+
return chunk
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function webpOrientationChunk(orientation: number): Uint8Array {
|
|
93
|
+
const tiff = orientationTiff(orientation)
|
|
94
|
+
const chunk = new Uint8Array(8 + tiff.length)
|
|
95
|
+
chunk.set([0x45, 0x58, 0x49, 0x46], 0)
|
|
96
|
+
new DataView(chunk.buffer).setUint32(4, tiff.length, true)
|
|
97
|
+
chunk.set(tiff, 8)
|
|
98
|
+
return chunk
|
|
99
|
+
}
|
|
100
|
+
|
|
57
101
|
function jpegSegmentKind(marker: number, payload: Uint8Array): ImageMetadataKind | undefined {
|
|
58
102
|
if (marker === 0xE1)
|
|
59
103
|
return EXIF_HEADER.every((b, i) => payload[i] === b) ? 'exif' : 'xmp'
|
|
@@ -130,21 +174,29 @@ function stripPng(bytes: Uint8Array<ArrayBuffer>): StrippedImage {
|
|
|
130
174
|
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
131
175
|
const kept: Uint8Array[] = [bytes.subarray(0, 8)]
|
|
132
176
|
const removed = new Set<ImageMetadataKind>()
|
|
177
|
+
let orientation: number | undefined
|
|
133
178
|
let at = 8
|
|
134
179
|
while (at + 12 <= bytes.length) {
|
|
135
|
-
const
|
|
136
|
-
|
|
180
|
+
const size = view.getUint32(at)
|
|
181
|
+
const end = at + 12 + size
|
|
182
|
+
if (size > bytes.length - at - 12 || end > bytes.length)
|
|
137
183
|
return { bytes, removed: [] }
|
|
138
184
|
const type = ascii(bytes, at + 4, 4)
|
|
139
185
|
const kind = type === 'iTXt' && ascii(bytes, at + 8, 17) === 'XML:com.adobe.xmp' ? 'xmp' : PNG_METADATA[type]
|
|
140
|
-
if (kind === undefined)
|
|
186
|
+
if (kind === undefined) {
|
|
141
187
|
kept.push(bytes.subarray(at, end))
|
|
142
|
-
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
143
190
|
removed.add(kind)
|
|
191
|
+
if (kind === 'exif')
|
|
192
|
+
orientation ??= exifOrientation(bytes.subarray(at + 8, at + 8 + size))
|
|
193
|
+
}
|
|
144
194
|
at = end
|
|
145
195
|
}
|
|
146
196
|
if (at !== bytes.length || removed.size === 0)
|
|
147
197
|
return { bytes, removed: [] }
|
|
198
|
+
if (orientation !== undefined && orientation > 1 && orientation <= 8)
|
|
199
|
+
kept.splice(kept.length > 1 ? 2 : 1, 0, pngOrientationChunk(orientation))
|
|
148
200
|
return { bytes: concat(kept), removed: [...removed] }
|
|
149
201
|
}
|
|
150
202
|
|
|
@@ -154,24 +206,32 @@ function stripWebp(bytes: Uint8Array<ArrayBuffer>): StrippedImage {
|
|
|
154
206
|
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
155
207
|
const kept: Uint8Array[] = []
|
|
156
208
|
const removed = new Set<ImageMetadataKind>()
|
|
209
|
+
let orientation: number | undefined
|
|
157
210
|
let at = 12
|
|
158
211
|
while (at + 8 <= bytes.length) {
|
|
159
212
|
const size = view.getUint32(at + 4, true)
|
|
160
213
|
const end = Math.min(at + 8 + size + (size & 1), bytes.length)
|
|
161
|
-
if (at + 8 + size > bytes.length)
|
|
214
|
+
if (size > bytes.length - at - 8 || at + 8 + size > bytes.length)
|
|
162
215
|
return { bytes, removed: [] }
|
|
163
216
|
const kind = WEBP_METADATA[ascii(bytes, at, 4)]
|
|
164
|
-
if (kind === undefined)
|
|
217
|
+
if (kind === undefined) {
|
|
165
218
|
kept.push(bytes.slice(at, end))
|
|
166
|
-
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
167
221
|
removed.add(kind)
|
|
222
|
+
if (kind === 'exif')
|
|
223
|
+
orientation ??= exifOrientation(bytes.subarray(at + 8, at + 8 + size))
|
|
224
|
+
}
|
|
168
225
|
at = end
|
|
169
226
|
}
|
|
170
227
|
if (removed.size === 0)
|
|
171
228
|
return { bytes, removed: [] }
|
|
229
|
+
const keptOrientation = orientation !== undefined && orientation > 1 && orientation <= 8 ? orientation : undefined
|
|
172
230
|
const vp8x = kept.find(chunk => ascii(chunk, 0, 4) === 'VP8X')
|
|
173
231
|
if (vp8x !== undefined)
|
|
174
|
-
vp8x[8] = vp8x[8]! & ~0x0C
|
|
232
|
+
vp8x[8] = keptOrientation !== undefined ? ((vp8x[8]! & ~0x04) | 0x08) : (vp8x[8]! & ~0x0C)
|
|
233
|
+
if (keptOrientation !== undefined)
|
|
234
|
+
kept.push(webpOrientationChunk(keptOrientation))
|
|
175
235
|
const body = concat(kept)
|
|
176
236
|
const header = new Uint8Array(12)
|
|
177
237
|
header.set(bytes.subarray(0, 12))
|
|
@@ -179,7 +239,7 @@ function stripWebp(bytes: Uint8Array<ArrayBuffer>): StrippedImage {
|
|
|
179
239
|
return { bytes: concat([header, body]), removed: [...removed] }
|
|
180
240
|
}
|
|
181
241
|
|
|
182
|
-
/** Removes EXIF (GPS position, device serial), XMP, IPTC and text metadata from a JPEG, PNG or WebP file
|
|
242
|
+
/** Removes EXIF (GPS position, device serial), XMP, IPTC and text metadata from a JPEG, PNG or WebP file while preserving its EXIF orientation. Other formats and unreadable files come back unchanged. */
|
|
183
243
|
export function stripImageMetadata(bytes: Uint8Array<ArrayBuffer>): StrippedImage {
|
|
184
244
|
if (bytes[0] === 0xFF && bytes[1] === 0xD8)
|
|
185
245
|
return stripJpeg(bytes)
|
package/src/pipeline.ts
CHANGED
|
@@ -199,8 +199,8 @@ export async function encodeVariants(srcPath: string, jobs: readonly VariantEnco
|
|
|
199
199
|
return jobs.filter(job => existsSync(job.cachedFile))
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
-
// Bumped when the same source and options give other variants or sizes, so a cache from before is not reused: 2 sizes photos after their EXIF orientation (#3676), 3 keeps their ICC profile (#257).
|
|
203
|
-
const VARIANT_CACHE_VERSION =
|
|
202
|
+
// Bumped when the same source and options give other variants or sizes, so a cache from before is not reused: 2 sizes photos after their EXIF orientation (#3676), 3 keeps their ICC profile (#257), 4 sizes rotated PNG, WebP and AVIF after their orientation (#171).
|
|
203
|
+
const VARIANT_CACHE_VERSION = 4
|
|
204
204
|
|
|
205
205
|
export function computeImageKey(srcPath: string, rawBytes: Uint8Array, cfg: VariantPipelineConfig): { hash: string, stem: string, rawFileName: string } {
|
|
206
206
|
const ext = extname(srcPath).toLowerCase()
|
package/src/plugin.ts
CHANGED
|
@@ -63,6 +63,10 @@ const DEFAULT_WIDTHS = [320, 640, 960, 1280, 1920]
|
|
|
63
63
|
const DEFAULT_FORMATS: ('avif' | 'webp')[] = ['avif', 'webp']
|
|
64
64
|
const PUBLIC_IMAGE = /\.(?:png|jpe?g|webp)$/i
|
|
65
65
|
const PERSONAL_METADATA = new Set(['exif', 'xmp', 'iptc'])
|
|
66
|
+
// Formats shipped as-is rather than rasterized to AVIF/WebP variants.
|
|
67
|
+
const RAW_ONLY_IMAGE_EXTS = new Set(['.svg', '.ico'])
|
|
68
|
+
// Non-core Markdown image formats that fall back to the original <img> when their header cannot be parsed.
|
|
69
|
+
const FALLBACK_MARKDOWN_EXTS = new Set(['.ico', '.bmp', '.tif', '.tiff'])
|
|
66
70
|
|
|
67
71
|
function walkPublicImages(dir: string, out: string[] = []): string[] {
|
|
68
72
|
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
@@ -349,7 +353,8 @@ export function optimizeImageFile(absPath: string, options: OptimizeImageFileOpt
|
|
|
349
353
|
}
|
|
350
354
|
const appCtx = getAppContext()
|
|
351
355
|
const rawBytes = stripImageMetadata(new Uint8Array(readFileSync(absPath))).bytes
|
|
352
|
-
const
|
|
356
|
+
const isRawOnly = RAW_ONLY_IMAGE_EXTS.has(ext)
|
|
357
|
+
const dims = readImageDimensions(rawBytes) ?? (ext === '.svg' ? { width: 0, height: 0 } : undefined)
|
|
353
358
|
if (dims === undefined)
|
|
354
359
|
throw new Error(`@prunellejs/images: could not determine dimensions for "${absPath}"`)
|
|
355
360
|
|
|
@@ -359,8 +364,13 @@ export function optimizeImageFile(absPath: string, options: OptimizeImageFileOpt
|
|
|
359
364
|
|
|
360
365
|
// Runtime server mode (including --compile): never write to dist/public or block on Bun.spawnSync.
|
|
361
366
|
const runtimeStore = options.outPublicDir === undefined ? appCtx?.inject(RUNTIME_IMAGE_STORE_SERVICE) : undefined
|
|
362
|
-
if (runtimeStore !== undefined && !inBuild)
|
|
367
|
+
if (runtimeStore !== undefined && !inBuild) {
|
|
368
|
+
if (isRawOnly) {
|
|
369
|
+
runtimeStore.setStatic(`/img/${rawFileName}`, rawBytes, mimeForImageExt(ext))
|
|
370
|
+
return { src: `/img/${rawFileName}`, width: dims.width, height: dims.height }
|
|
371
|
+
}
|
|
363
372
|
return optimizeForRuntime(absPath, ext, rawBytes, dims, stem, hash, rawFileName, cacheRoot, pipelineCfg, runtimeStore)
|
|
373
|
+
}
|
|
364
374
|
|
|
365
375
|
const outPublicDir = resolve(options.outPublicDir ?? appCtx?.publicDir ?? 'dist/public')
|
|
366
376
|
const imgOutDir = join(outPublicDir, 'img')
|
|
@@ -371,7 +381,7 @@ export function optimizeImageFile(absPath: string, options: OptimizeImageFileOpt
|
|
|
371
381
|
return { src: `/img/${rawFileName}`, width: dims.width, height: dims.height }
|
|
372
382
|
}
|
|
373
383
|
|
|
374
|
-
if (!hasMagick())
|
|
384
|
+
if (isRawOnly || !hasMagick())
|
|
375
385
|
return emitRaw()
|
|
376
386
|
|
|
377
387
|
const metaPath = join(cacheRoot, `md-${stem}-${hash}.json`)
|
|
@@ -451,7 +461,10 @@ export function createMarkdownImageResolver(options: MarkdownImageResolverOption
|
|
|
451
461
|
}
|
|
452
462
|
if (!isInside(realBoundary, realTarget))
|
|
453
463
|
return undefined
|
|
454
|
-
|
|
464
|
+
const targetExt = extname(realTarget).toLowerCase()
|
|
465
|
+
if (!ALLOWED_IMAGE_EXTS.has(targetExt))
|
|
466
|
+
return undefined
|
|
467
|
+
if (FALLBACK_MARKDOWN_EXTS.has(targetExt) && readImageDimensions(new Uint8Array(readFileSync(realTarget))) === undefined)
|
|
455
468
|
return undefined
|
|
456
469
|
|
|
457
470
|
const asset = optimizeImageFile(realTarget, options)
|
|
@@ -461,8 +474,8 @@ export function createMarkdownImageResolver(options: MarkdownImageResolverOption
|
|
|
461
474
|
...(srcset !== undefined ? { srcset } : {}),
|
|
462
475
|
...(asset.avifSrcset !== undefined && asset.webpSrcset !== undefined ? { avifSrcset: asset.avifSrcset } : {}),
|
|
463
476
|
...(srcset !== undefined ? { sizes } : {}),
|
|
464
|
-
width: asset.width,
|
|
465
|
-
height: asset.height,
|
|
477
|
+
...(asset.width > 0 ? { width: asset.width } : {}),
|
|
478
|
+
...(asset.height > 0 ? { height: asset.height } : {}),
|
|
466
479
|
}
|
|
467
480
|
}
|
|
468
481
|
}
|