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 22 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
17 changes: 17 additions & 0 deletions .changeset/tame-cameras-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
"astro": minor
---

Remote images can now have their dimensions inferred just like local images, allow width and height to be optional when inferSize is set to true.
OliverSpeir marked this conversation as resolved.
Show resolved Hide resolved

```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
```


> added to trigger tests
OliverSpeir marked this conversation as resolved.
Show resolved Hide resolved
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
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.FailedToProbeRemoteImage,
message: AstroErrorData.FailedToProbeRemoteImage.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;
});
Loading
Loading