Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inferSize to getImage so width and height are optional for remote images #9976

Merged
merged 42 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
58e354a
add remote image dimension probing, and param for getImage options, a…
OliverSpeir Jan 30, 2024
a5c2f04
add inferSize parameter to getImage, which probes remote image size, …
OliverSpeir Feb 4, 2024
a39438b
add fixture and tests
OliverSpeir Feb 4, 2024
71fbdca
Merge branch 'main' of https://github.com/withastro/astro into get-re…
OliverSpeir Feb 5, 2024
466c033
add changeset
OliverSpeir Feb 5, 2024
eae056a
attempt to fix pnpm-lock.yaml
OliverSpeir Feb 5, 2024
c270d3c
fix pnpm-lock.yaml again
OliverSpeir Feb 5, 2024
4847cd1
pnpm-lock spacing
OliverSpeir Feb 5, 2024
579cb87
fix pnpm-lock AGAIN
OliverSpeir Feb 5, 2024
b078878
Merge branch 'main' into get-remote-dimensions
florian-lefebvre Feb 5, 2024
10507bc
better description of error for docgen
OliverSpeir Feb 6, 2024
28cdb58
improve failed to probe error message and correct required dimensions…
OliverSpeir Feb 6, 2024
551c7f1
increase timeout for mdx tests
OliverSpeir Feb 6, 2024
488858c
increasing mdx timeout to 2min to see if it passes, will reduce if it…
OliverSpeir Feb 6, 2024
f408f33
setting mdx timeout to 70 seconds
OliverSpeir Feb 6, 2024
374293d
Update packages/astro/src/assets/services/service.ts
OliverSpeir Feb 8, 2024
ab9cd45
refactor to move logic to getImage instead of validateOptions and to…
OliverSpeir Feb 8, 2024
5bac12d
fix broken link created by docs PR
OliverSpeir Feb 8, 2024
603c4df
remove the probe-image-size library due to its use of node APIs
OliverSpeir Feb 10, 2024
b642cb3
undo all changes to service.ts that were left after moving inferSize…
OliverSpeir Feb 12, 2024
878e9b6
Merge branch 'main' into get-remote-dimensions
OliverSpeir Feb 12, 2024
457ada2
update error message
sarah11918 Feb 13, 2024
624c86a
remove probe-image-size library all together, update error message, a…
OliverSpeir Feb 13, 2024
50b91c4
Merge branch 'get-remote-dimensions' of https://github.com/OliverSpei…
OliverSpeir Feb 13, 2024
3f09a2c
Update .changeset/tame-cameras-change.md
OliverSpeir Feb 13, 2024
a9fc180
Update .changeset/tame-cameras-change.md
OliverSpeir Feb 13, 2024
75422d0
reword the error message based on Sarah's previous review
OliverSpeir Feb 13, 2024
2a2050a
Merge branch 'get-remote-dimensions' of https://github.com/OliverSpei…
OliverSpeir Feb 13, 2024
39bc8d5
Merge branch 'main' into get-remote-dimensions
OliverSpeir Feb 13, 2024
a388af1
remove probe-image-size from pnpmlock
OliverSpeir Feb 13, 2024
63cecd5
fix lockfile?
OliverSpeir Feb 13, 2024
6d0d868
update error message name
OliverSpeir Feb 13, 2024
47ce1bf
move image-size into vendor folder
OliverSpeir Feb 13, 2024
b845055
add eslint ignore to a line in image-size
OliverSpeir Feb 13, 2024
f10c5a4
test if change to mdx test timeout was needed
OliverSpeir Feb 14, 2024
5dee749
Update .changeset/tame-cameras-change.md
ematipico Feb 14, 2024
ea220a0
update changset syntax
OliverSpeir Feb 14, 2024
58f0dc5
Merge branch 'main' into get-remote-dimensions
OliverSpeir Feb 14, 2024
f4a71e6
Merge branch 'main' into get-remote-dimensions
ematipico Feb 14, 2024
fdf64de
patch parse heif to account for filetype block being out of order
OliverSpeir Feb 14, 2024
60ee177
Merge branch 'get-remote-dimensions' of https://github.com/OliverSpei…
OliverSpeir Feb 14, 2024
f1fbb2f
Merge branch 'main' into get-remote-dimensions
OliverSpeir Feb 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .changeset/tame-cameras-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
"astro": minor
---

Adds a new optional `astro:assets` image attribute `inferSize` for use with remote images.

Remote images can now have their dimensions inferred just like local images. Setting `inferSize` to `true` allows you to use `getImage()` and the `<Image />` and `<Picture />` components without setting the `width` and `height` properties.

```astro
---
import { Image, Picture, getImage } from 'astro:assets';
const myPic = await getImage({src: "https://example.com/example.png", inferSize: true, alt: ""})
---
<Image src="https://example.com/example.png" inferSize={true} alt="">
<Picture src="https://example.com/example.png" inferSize={true} alt="">
ematipico marked this conversation as resolved.
Show resolved Hide resolved
```

Read more about [using `inferSize` with remote images](https://docs.astro.build/en/guides/images/#infersize) in our documentation.
6 changes: 5 additions & 1 deletion packages/astro/components/Image.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
---
import { getImage, type LocalImageProps, type RemoteImageProps } from 'astro:assets';
import {
getImage,
type LocalImageProps,
type RemoteImageProps,
} from 'astro:assets';
import { AstroError, AstroErrorData } from '../dist/core/errors/index.js';
import type { HTMLAttributes } from '../types';

Expand Down
6 changes: 5 additions & 1 deletion packages/astro/components/Picture.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
---
import { getImage, type LocalImageProps, type RemoteImageProps } from 'astro:assets';
import {
getImage,
type LocalImageProps,
type RemoteImageProps,
} from 'astro:assets';
import type { GetImageResult, ImageOutputFormat } from '../dist/@types/astro';
import { isESMImportedImage } from '../dist/assets/utils/imageKind';
import { AstroError, AstroErrorData } from '../dist/core/errors/index.js';
Expand Down
1 change: 0 additions & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@
"p-queue": "^8.0.1",
"path-to-regexp": "^6.2.1",
"preferred-pm": "^3.1.2",
"probe-image-size": "^7.2.3",
"prompts": "^2.4.2",
"rehype": "^13.0.1",
"resolve": "^1.22.4",
Expand Down
16 changes: 16 additions & 0 deletions packages/astro/src/assets/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
UnresolvedImageTransform,
} from './types.js';
import { isESMImportedImage, isRemoteImage } from './utils/imageKind.js';
import { probe } from "./utils/remoteProbe.js"

export async function getConfiguredImageService(): Promise<ImageService> {
if (!globalThis?.astroAsset?.imageService) {
Expand Down Expand Up @@ -61,6 +62,21 @@ export async function getImage(
: options.src,
};

// Infer size for remote images if inferSize is true
if (options.inferSize && isRemoteImage(resolvedOptions.src)) {
try {
const result = await probe(resolvedOptions.src); // Directly probe the image URL
resolvedOptions.width ??= result.width;
resolvedOptions.height ??= result.height;
delete resolvedOptions.inferSize; // Delete so it doesn't end up in the attributes
} catch {
throw new AstroError({
...AstroErrorData.FailedToFetchRemoteImageDimensions,
message: AstroErrorData.FailedToFetchRemoteImageDimensions.message(resolvedOptions.src),
});
}
}

const originalPath = isESMImportedImage(resolvedOptions.src)
? resolvedOptions.src.fsPath
: resolvedOptions.src;
Expand Down
49 changes: 36 additions & 13 deletions packages/astro/src/assets/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export type SrcSetValue = UnresolvedSrcSetValue & {
*/
export type UnresolvedImageTransform = Omit<ImageTransform, 'src'> & {
src: ImageMetadata | string | Promise<{ default: ImageMetadata }>;
inferSize?: boolean;
};

/**
Expand Down Expand Up @@ -176,16 +177,38 @@ export type LocalImageProps<T> = ImageSharedProps<T> & {
quality?: ImageQuality;
};

export type RemoteImageProps<T> = WithRequired<ImageSharedProps<T>, 'width' | 'height'> & {
/**
* URL of a remote image. Can start with a protocol (ex: `https://`) or alternatively `/`, or `Astro.url`, for images in the `public` folder
*
* Remote images are not optimized, and require both `width` and `height` to be set.
*
* **Example**:
* ```
* <Image src="https://example.com/image.png" width={450} height={300} alt="..." />
* ```
*/
src: string;
};
export type RemoteImageProps<T> =
| (ImageSharedProps<T> & {
/**
* URL of a remote image. Can start with a protocol (ex: `https://`) or alternatively `/`, or `Astro.url`, for images in the `public` folder
*
* Remote images are not optimized, and require both `width` and `height` to be set.
*
* **Example**:
* ```
* <Image src="https://example.com/image.png" width={450} height={300} alt="..." />
* ```
*/
src: string;
/**
* When inferSize is true width and height are not required
*/
inferSize: true;
})
| (WithRequired<ImageSharedProps<T>, 'width' | 'height'> & {
/**
* URL of a remote image. Can start with a protocol (ex: `https://`) or alternatively `/`, or `Astro.url`, for images in the `public` folder
*
* Remote images are not optimized, and require both `width` and `height` to be set.
*
* **Example**:
* ```
* <Image src="https://example.com/image.png" width={450} height={300} alt="..." />
* ```
*/
src: string;
/**
* When inferSize is false or undefined width and height are required
*/
inferSize?: false | undefined;
});
8 changes: 4 additions & 4 deletions packages/astro/src/assets/utils/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import probe from 'probe-image-size';

import { lookup as probe } from '../utils/vendor/image-size/lookup.js'
import { AstroError, AstroErrorData } from '../../core/errors/index.js';
import type { ImageInputFormat, ImageMetadata } from '../types.js';

export async function imageMetadata(
data: Uint8Array,
src?: string
): Promise<Omit<ImageMetadata, 'src' | 'fsPath'>> {
// @ts-expect-error probe-image-size types are wrong, it does accept Uint8Array. From the README: "Sync version can eat arrays, typed arrays and buffers."
const result = probe.sync(data);
const result = probe(data);

if (result === null) {
if (!result.height || !result.width || !result.type ) {
throw new AstroError({
...AstroErrorData.NoImageMetadata,
message: AstroErrorData.NoImageMetadata.message(src),
Expand Down
48 changes: 48 additions & 0 deletions packages/astro/src/assets/utils/remoteProbe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

import { lookup } from './vendor/image-size/lookup.js'
import type { ISize } from './vendor/image-size/types/interface.ts';

export async function probe(url: string): Promise<ISize> {
// Start fetching the image
const response = await fetch(url);
if (!response.body || !response.ok) {
throw new Error('Failed to fetch image');
}

const reader = response.body.getReader();

let done: boolean | undefined, value: Uint8Array;
let accumulatedChunks = new Uint8Array();

// Process the stream chunk by chunk
while (!done) {
const readResult = await reader.read();
done = readResult.done;

if (done) break;

if (readResult.value) {
value = readResult.value;

// Accumulate chunks
let tmp = new Uint8Array(accumulatedChunks.length + value.length);
tmp.set(accumulatedChunks, 0);
tmp.set(value, accumulatedChunks.length);
accumulatedChunks = tmp;

try {
// Attempt to determine the size with each new chunk
const dimensions = lookup(accumulatedChunks);
if (dimensions) {
await reader.cancel(); // stop stream as we have size now
return dimensions;
}
} catch (error) {
// This catch block is specifically for `sizeOf` failures,
// which might occur if the accumulated data isn't yet sufficient.
}
}
}

throw new Error('Failed to parse the size');
}
9 changes: 9 additions & 0 deletions packages/astro/src/assets/utils/vendor/image-size/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright © 2013-Present Aditya Yadav, http://netroy.in

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions packages/astro/src/assets/utils/vendor/image-size/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This code comes from https://github.com/image-size/image-size/pull/370, and is slightly modified (all import statements have file extensions added to them).

The `fromFile` functionality has also been removed, as it was not being used.
25 changes: 25 additions & 0 deletions packages/astro/src/assets/utils/vendor/image-size/detector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { imageType } from './types/index.js'
import { typeHandlers, types } from './types/index.js'

// This map helps avoid validating for every single image type
const firstBytes = new Map<number, imageType>([
[0x38, 'psd'],
[0x42, 'bmp'],
[0x44, 'dds'],
[0x47, 'gif'],
[0x49, 'tiff'],
[0x4d, 'tiff'],
[0x52, 'webp'],
[0x69, 'icns'],
[0x89, 'png'],
[0xff, 'jpg'],
])

export function detector(input: Uint8Array): imageType | undefined {
const byte = input[0]
const type = firstBytes.get(byte)
if (type && typeHandlers.get(type)!.validate(input)) {
return type
}
return types.find((fileType) => typeHandlers.get(fileType)!.validate(input))
}
43 changes: 43 additions & 0 deletions packages/astro/src/assets/utils/vendor/image-size/lookup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { imageType } from './types/index.js'
import { typeHandlers } from './types/index.js'
import { detector } from './detector.js'
import type { ISizeCalculationResult } from './types/interface.ts'

type Options = {
disabledTypes: imageType[]
}

const globalOptions: Options = {
disabledTypes: [],
}

/**
* Return size information based on an Uint8Array
*
* @param {Uint8Array} input
* @returns {ISizeCalculationResult}
*/
export function lookup(input: Uint8Array): ISizeCalculationResult {
// detect the file type... don't rely on the extension
const type = detector(input)

if (typeof type !== 'undefined') {
if (globalOptions.disabledTypes.indexOf(type) > -1) {
throw new TypeError('disabled file type: ' + type)
}

// find an appropriate handler for this file type
const size = typeHandlers.get(type)!.calculate(input)
if (size !== undefined) {
size.type = size.type ?? type
return size
}
}

// throw up, if we don't understand the file
throw new TypeError('unsupported file type: ' + type)
}

export const disableTypes = (types: imageType[]): void => {
globalOptions.disabledTypes = types
}
11 changes: 11 additions & 0 deletions packages/astro/src/assets/utils/vendor/image-size/types/bmp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { IImage } from './interface.ts'
import { toUTF8String, readInt32LE, readUInt32LE } from './utils.js'

export const BMP: IImage = {
validate: (input) => toUTF8String(input, 0, 2) === 'BM',

calculate: (input) => ({
height: Math.abs(readInt32LE(input, 22)),
width: readUInt32LE(input, 18),
}),
}
17 changes: 17 additions & 0 deletions packages/astro/src/assets/utils/vendor/image-size/types/cur.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { IImage } from './interface.ts'
import { ICO } from './ico.js'
import { readUInt16LE } from './utils.js'

const TYPE_CURSOR = 2
export const CUR: IImage = {
validate(input) {
const reserved = readUInt16LE(input, 0)
const imageCount = readUInt16LE(input, 4)
if (reserved !== 0 || imageCount === 0) return false

const imageType = readUInt16LE(input, 2)
return imageType === TYPE_CURSOR
},

calculate: (input) => ICO.calculate(input),
}
11 changes: 11 additions & 0 deletions packages/astro/src/assets/utils/vendor/image-size/types/dds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { IImage } from './interface.ts'
import { readUInt32LE } from './utils.js'

export const DDS: IImage = {
validate: (input) => readUInt32LE(input, 0) === 0x20534444,

calculate: (input) => ({
height: readUInt32LE(input, 12),
width: readUInt32LE(input, 16),
}),
}
12 changes: 12 additions & 0 deletions packages/astro/src/assets/utils/vendor/image-size/types/gif.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { IImage } from './interface.ts'
import { toUTF8String, readUInt16LE } from './utils.js'

const gifRegexp = /^GIF8[79]a/
export const GIF: IImage = {
validate: (input) => gifRegexp.test(toUTF8String(input, 0, 6)),

calculate: (input) => ({
height: readUInt16LE(input, 8),
width: readUInt16LE(input, 6),
}),
}
36 changes: 36 additions & 0 deletions packages/astro/src/assets/utils/vendor/image-size/types/heif.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { IImage } from './interface.ts'
import { findBox, readUInt32BE, toUTF8String } from './utils.js'

const brandMap = {
avif: 'avif',
mif1: 'heif',
msf1: 'heif', // hief-sequence
heic: 'heic',
heix: 'heic',
hevc: 'heic', // heic-sequence
hevx: 'heic', // heic-sequence
}

export const HEIF: IImage = {
validate(buffer) {
const ftype = toUTF8String(buffer, 4, 8)
const brand = toUTF8String(buffer, 8, 12)
return 'ftyp' === ftype && brand in brandMap
},

calculate(buffer) {
// Based on https://nokiatech.github.io/heif/technical.html
const metaBox = findBox(buffer, 'meta', 0)
const iprpBox = metaBox && findBox(buffer, 'iprp', metaBox.offset + 12)
const ipcoBox = iprpBox && findBox(buffer, 'ipco', iprpBox.offset + 8)
const ispeBox = ipcoBox && findBox(buffer, 'ispe', ipcoBox.offset + 8)
if (ispeBox) {
return {
height: readUInt32BE(buffer, ispeBox.offset + 16),
width: readUInt32BE(buffer, ispeBox.offset + 12),
type: toUTF8String(buffer, 8, 12),
}
}
throw new TypeError('Invalid HEIF, no size found')
}
}
Loading
Loading