Skip to content

Commit dfb51d4

Browse files
authored
feat(assets): Always allow cropping and never upscale (#14629)
1 parent 1479a0b commit dfb51d4

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

packages/astro/src/assets/services/sharp.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,29 @@ const sharpService: LocalImageService<SharpImageServiceConfig> = {
7171
// always call rotate to adjust for EXIF data orientation
7272
result.rotate();
7373

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

78-
const withoutEnlargement = Boolean(transform.fit);
79-
if (transform.width && transform.height && transform.fit) {
80-
const fit: keyof FitEnum = fitMap[transform.fit] ?? 'inside';
8179
result.resize({
8280
width: Math.round(transform.width),
8381
height: Math.round(transform.height),
8482
fit,
8583
position: transform.position,
86-
withoutEnlargement,
84+
withoutEnlargement: true,
8785
});
8886
} else if (transform.height && !transform.width) {
8987
result.resize({
9088
height: Math.round(transform.height),
91-
withoutEnlargement,
89+
withoutEnlargement: true,
9290
});
9391
} else if (transform.width) {
9492
result.resize({
9593
width: Math.round(transform.width),
96-
withoutEnlargement,
94+
withoutEnlargement: true,
9795
});
9896
}
99-
10097
if (transform.format) {
10198
let quality: number | string | undefined = undefined;
10299
if (transform.quality) {

packages/astro/test/core-image-service.test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,17 @@ describe('astro image service', () => {
111111
assert.equal(height, originalHeight);
112112
});
113113

114-
// To match old behavior, we should upscale if the requested size is larger than the original
115-
it('does upscale image if requested size is larger than original and fit is unset', async () => {
114+
it('does not upscale image if requested size is larger than original and fit is unset', async () => {
116115
const url = new URL(src);
117116
url.searchParams.set('w', '3000');
118117
url.searchParams.set('h', '2000');
119118
url.searchParams.delete('fit');
120119
const { width, height } = await getImageDimensionsFromFixture(fixture, url);
121-
assert.equal(width, 3000);
122-
assert.equal(height, 2000);
120+
assert.equal(width, originalWidth);
121+
assert.equal(height, originalHeight);
123122
});
124123

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

0 commit comments

Comments
 (0)