|
| 1 | +import type { LocalImageService, ImageTransform, AstroConfig } from "astro"; |
| 2 | + |
| 3 | +type OutputFormat = "avif" | "jpeg" | "jpg" | "png" | "webp"; |
| 4 | + |
| 5 | +const service: LocalImageService = { |
| 6 | + getURL(options: ImageTransform, imageConfig: AstroConfig["image"]) { |
| 7 | + const searchParams = new URLSearchParams(); |
| 8 | + searchParams.append( |
| 9 | + "href", |
| 10 | + typeof options.src === "string" ? options.src : options.src.src |
| 11 | + ); |
| 12 | + if (options.width) searchParams.append("w", options.width.toString()); |
| 13 | + if (options.height) searchParams.append("h", options.height.toString()); |
| 14 | + if (options.quality) searchParams.append("q", options.quality.toString()); |
| 15 | + if (options.format) searchParams.append("f", options.format); |
| 16 | + return `/_image/?${searchParams.toString()}`; |
| 17 | + }, |
| 18 | + |
| 19 | + parseURL(url: URL, imageConfig) { |
| 20 | + const params = url.searchParams; |
| 21 | + return { |
| 22 | + src: params.get("href")!, |
| 23 | + width: params.has("w") ? parseInt(params.get("w")!) : undefined, |
| 24 | + height: params.has("h") ? parseInt(params.get("h")!) : undefined, |
| 25 | + quality: params.has("q") ? parseInt(params.get("q")!) : undefined, |
| 26 | + format: params.get("f") as OutputFormat | undefined, |
| 27 | + }; |
| 28 | + }, |
| 29 | + |
| 30 | + async transform( |
| 31 | + inputBuffer: Uint8Array, |
| 32 | + transform: { |
| 33 | + src: string; |
| 34 | + width?: number; |
| 35 | + height?: number; |
| 36 | + quality?: number; |
| 37 | + format?: OutputFormat; |
| 38 | + }, |
| 39 | + imageConfig |
| 40 | + ) { |
| 41 | + let buffer = inputBuffer; |
| 42 | + |
| 43 | + if (/^https?:\/\//.test(transform.src)) { |
| 44 | + const response = await fetch(transform.src); |
| 45 | + if (!response.ok) { |
| 46 | + throw new Error(`Failed to fetch image from ${transform.src}`); |
| 47 | + } |
| 48 | + buffer = new Uint8Array(await response.arrayBuffer()); |
| 49 | + } |
| 50 | + |
| 51 | + const sharp = (await import("sharp")).default; |
| 52 | + let image = sharp(buffer); |
| 53 | + |
| 54 | + if (transform.width || transform.height) { |
| 55 | + image = image.resize(transform.width, transform.height); |
| 56 | + } |
| 57 | + |
| 58 | + if (transform.format) { |
| 59 | + image = image.toFormat(transform.format, { |
| 60 | + quality: transform.quality, |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + const outputBuffer = await image.toBuffer(); |
| 65 | + |
| 66 | + return { |
| 67 | + data: outputBuffer, |
| 68 | + format: transform.format ?? "webp", |
| 69 | + }; |
| 70 | + }, |
| 71 | + |
| 72 | + getHTMLAttributes(options, imageConfig) { |
| 73 | + let targetWidth = options.width; |
| 74 | + let targetHeight = options.height; |
| 75 | + |
| 76 | + if (typeof options.src === "object") { |
| 77 | + const aspectRatio = options.src.width / options.src.height; |
| 78 | + if (targetHeight && !targetWidth) { |
| 79 | + targetWidth = Math.round(targetHeight * aspectRatio); |
| 80 | + } else if (targetWidth && !targetHeight) { |
| 81 | + targetHeight = Math.round(targetWidth / aspectRatio); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + const { src, width, height, format, quality, ...attributes } = options; |
| 86 | + |
| 87 | + return { |
| 88 | + ...attributes, |
| 89 | + width: targetWidth, |
| 90 | + height: targetHeight, |
| 91 | + loading: attributes.loading ?? "lazy", |
| 92 | + decoding: attributes.decoding ?? "async", |
| 93 | + }; |
| 94 | + }, |
| 95 | + |
| 96 | + propertiesToHash: ["src", "width", "height", "format", "quality"], |
| 97 | +}; |
| 98 | + |
| 99 | +export default service; |
0 commit comments