Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 7 additions & 10 deletions packages/astro/src/assets/services/sharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,29 @@ const sharpService: LocalImageService<SharpImageServiceConfig> = {
// always call rotate to adjust for EXIF data orientation
result.rotate();

// If `fit` isn't set then use old behavior:
// - Do not use both width and height for resizing, and prioritize width over height
// - Allow enlarging images
if (transform.width && transform.height) {
const fit: keyof FitEnum | undefined = transform.fit
? (fitMap[transform.fit] ?? 'inside')
: undefined;

const withoutEnlargement = Boolean(transform.fit);
if (transform.width && transform.height && transform.fit) {
const fit: keyof FitEnum = fitMap[transform.fit] ?? 'inside';
result.resize({
width: Math.round(transform.width),
height: Math.round(transform.height),
fit,
position: transform.position,
withoutEnlargement,
withoutEnlargement: true,
});
} else if (transform.height && !transform.width) {
result.resize({
height: Math.round(transform.height),
withoutEnlargement,
withoutEnlargement: true,
});
} else if (transform.width) {
result.resize({
width: Math.round(transform.width),
withoutEnlargement,
withoutEnlargement: true,
});
}

if (transform.format) {
let quality: number | string | undefined = undefined;
if (transform.quality) {
Expand Down
10 changes: 4 additions & 6 deletions packages/astro/test/core-image-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,17 @@ describe('astro image service', () => {
assert.equal(height, originalHeight);
});

// To match old behavior, we should upscale if the requested size is larger than the original
it('does upscale image if requested size is larger than original and fit is unset', async () => {
it('does not upscale image if requested size is larger than original and fit is unset', async () => {
const url = new URL(src);
url.searchParams.set('w', '3000');
url.searchParams.set('h', '2000');
url.searchParams.delete('fit');
const { width, height } = await getImageDimensionsFromFixture(fixture, url);
assert.equal(width, 3000);
assert.equal(height, 2000);
assert.equal(width, originalWidth);
assert.equal(height, originalHeight);
});

// To match old behavior, we should upscale if the requested size is larger than the original
it('does not upscale is only one dimension is provided and fit is set', async () => {
it('does not upscale if only one dimension is provided and fit is set', async () => {
const url = new URL(src);
url.searchParams.set('w', '3000');
url.searchParams.delete('h');
Expand Down