Skip to content

Commit

Permalink
Add inferSize to getImage so width and height are optional for remo…
Browse files Browse the repository at this point in the history
…te images (#9976)

* add remote image dimension probing, and param for getImage options, and new astro error

* add inferSize parameter to getImage, which probes remote image size, and respective types to picture and image component

* add fixture and tests

* add changeset

* attempt to fix pnpm-lock.yaml

* fix pnpm-lock.yaml again

* pnpm-lock spacing

* fix pnpm-lock AGAIN

* better description of error for docgen

* improve failed to probe error message and correct required dimensions error

* increase timeout for mdx tests

* increasing mdx timeout to 2min to see if it passes, will reduce if it does

* setting mdx timeout to 70 seconds

* Update packages/astro/src/assets/services/service.ts

Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>

* refactor to move logic to getImage instead of validateOptions and  to not add a third type to image and picture

* fix broken link created by docs PR

* remove the probe-image-size library due to its use of node APIs

* undo all changes to service.ts that were left  after moving inferSize logic to getImage

* update error message

* remove probe-image-size library all together, update error message, add vendored version of complete image-size library instead of refactored version

* Update .changeset/tame-cameras-change.md

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

* Update .changeset/tame-cameras-change.md

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

* reword the error message based on Sarah's previous review

* remove probe-image-size from pnpmlock

* fix lockfile?

* update error message name

* move image-size into vendor folder

* add eslint ignore to a line in image-size

* test if change to mdx test timeout was needed

* Update .changeset/tame-cameras-change.md

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

* update changset syntax

* patch parse heif to account for filetype block being out of order

---------

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
  • Loading branch information
5 people authored Feb 15, 2024
1 parent 787e6f5 commit 91f75af
Show file tree
Hide file tree
Showing 40 changed files with 1,414 additions and 69 deletions.
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 })
---
<Image src="https://example.com/example.png" inferSize alt="" />
<Picture src="https://example.com/example.png" inferSize alt="" />
```

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 @@ -162,7 +162,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),
}),
}
Loading

0 comments on commit 91f75af

Please sign in to comment.