Skip to content

Commit

Permalink
Fix local images having the wrong width and height when both dimensio…
Browse files Browse the repository at this point in the history
…ns were provided (#6547)

* fix(images): Fix Image providing the wrong width and height if both attributes were provided on local images

* chore: changeset
  • Loading branch information
Princesseuh committed Mar 14, 2023
1 parent 811436d commit 04dddd7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-flies-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix images having the wrong width and height when using the new astro:assets features if both dimensions were provided
7 changes: 4 additions & 3 deletions packages/astro/src/assets/services/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ export const baseService: Omit<LocalImageService, 'transform'> = {
let targetHeight = options.height;
if (isESMImportedImage(options.src)) {
const aspectRatio = options.src.width / options.src.height;

// If we have a desired height and no width, calculate the target width automatically
if (targetHeight && !targetWidth) {
// If we have a height but no width, use height to calculate the width
targetWidth = Math.round(targetHeight * aspectRatio);
} else if (targetWidth && !targetHeight) {
// If we have a width but no height, use width to calculate the height
targetHeight = Math.round(targetWidth / aspectRatio);
} else {
} else if (!targetWidth && !targetHeight) {
// If we have neither width or height, use the original image's dimensions
targetWidth = options.src.width;
targetHeight = options.src.height;
}
Expand Down
20 changes: 19 additions & 1 deletion packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,30 @@ describe('astro:image', () => {
expect(!!$img.attr('decoding')).to.equal(true);
});

it('has width and height', () => {
it('has width and height - no dimensions set', () => {
let $img = $('#local img');
expect($img.attr('width')).to.equal('207');
expect($img.attr('height')).to.equal('243');
});

it('has proper width and height - only width', () => {
let $img = $('#local-width img');
expect($img.attr('width')).to.equal('350');
expect($img.attr('height')).to.equal('411');
});

it('has proper width and height - only height', () => {
let $img = $('#local-height img');
expect($img.attr('width')).to.equal('170');
expect($img.attr('height')).to.equal('200');
});

it('has proper width and height - has both width and height', () => {
let $img = $('#local-both img');
expect($img.attr('width')).to.equal('300');
expect($img.attr('height')).to.equal('400');
});

it('includes the provided alt', () => {
let $img = $('#local img');
expect($img.attr('alt')).to.equal('a penguin');
Expand Down
12 changes: 12 additions & 0 deletions packages/astro/test/fixtures/core-image/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ import myImage from "../assets/penguin1.jpg";
<Image src={myImage} alt="a penguin" />
</div>

<div id="local-width">
<Image src={myImage} alt="a penguin" width={350} />
</div>

<div id="local-height">
<Image src={myImage} alt="a penguin" height={200} />
</div>

<div id="local-both">
<Image src={myImage} alt="a penguin" width={300} height={400} />
</div>

<div id="remote">
<Image src="https://avatars.githubusercontent.com/u/622227?s=64" alt="fred" width="48" height="48" />
</div>
Expand Down

0 comments on commit 04dddd7

Please sign in to comment.