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 AVIF format support #132

Merged
merged 1 commit into from
Feb 5, 2021
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
4 changes: 2 additions & 2 deletions addon/components/responsive-image.hbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<picture>
{{#each this.sources as |s|}}
{{#each this.sourcesSorted as |s|}}
<source
srcset={{s.srcset}}
type={{s.type}}
type={{s.mimeType}}
sizes={{s.sizes}}
/>
{{/each}}
Expand Down
24 changes: 21 additions & 3 deletions addon/components/responsive-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import ResponsiveImageService, {
ImageMeta,
ImageType,
} from 'ember-responsive-image/services/responsive-image';
import { assert } from '@ember/debug';

Expand All @@ -15,7 +16,8 @@ interface ResponsiveImageComponentArgs {

interface PictureSource {
srcset: string;
type: string;
type: ImageType;
mimeType: string;
sizes?: string;
}

Expand All @@ -26,6 +28,14 @@ enum Layout {

const PIXEL_DENSITIES = [1, 2];

// determines the order of sources, prefereing next-gen formats over legacy
const typeScore = new Map<ImageType, number>([
['png', 1],
['jpeg', 1],
['webp', 2],
['avif', 3],
]);

export default class ResponsiveImageComponent extends Component<ResponsiveImageComponentArgs> {
@service
responsiveImage!: ResponsiveImageService;
Expand Down Expand Up @@ -55,7 +65,8 @@ export default class ResponsiveImageComponent extends Component<ResponsiveImageC
sizes:
this.args.sizes ??
(this.args.size ? `${this.args.size}vw` : undefined),
type: `image/${type}`,
type,
mimeType: `image/${type}`,
};
});
} else {
Expand All @@ -79,12 +90,19 @@ export default class ResponsiveImageComponent extends Component<ResponsiveImageC

return {
srcset: sources.join(', '),
type: `image/${type}`,
type,
mimeType: `image/${type}`,
};
});
}
}

get sourcesSorted(): PictureSource[] {
return this.sources.sort(
(a, b) => (typeScore.get(b.type) ?? 0) - (typeScore.get(a.type) ?? 0)
);
}

get imageMeta(): ImageMeta | undefined {
if (this.layout === Layout.RESPONSIVE) {
return this.responsiveImage.getImageMetaBySize(
Expand Down
2 changes: 1 addition & 1 deletion addon/services/responsive-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const screenWidth = typeof screen !== 'undefined' ? screen.width : 320;

const extentionTypeMapping = new Map<string, ImageType>([['jpg', 'jpeg']]);

export type ImageType = 'png' | 'jpeg' | 'webp';
export type ImageType = 'png' | 'jpeg' | 'webp' | 'avif';

export interface ImageMeta {
image: string;
Expand Down
2 changes: 1 addition & 1 deletion ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (defaults) {
// Add options here
fingerprint: {
enabled: true,
extensions: ['js', 'css', 'png', 'jpg', 'gif', 'map', 'webp'],
extensions: ['js', 'css', 'png', 'jpg', 'gif', 'map', 'webp', 'avif'],
exclude: ['testem.js'],
customHash: '00e24234f1b58e32b935b1041432916f',
},
Expand Down
6 changes: 5 additions & 1 deletion lib/image-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class ImageResizer extends CachingWriter {
.webp({
quality: this.image_options.quality,
force: false,
})
.avif({
quality: this.image_options.quality,
force: false,
});
this.writeInfoLine(file);
this.addConfigData(file);
Expand Down Expand Up @@ -125,7 +129,7 @@ class ImageResizer extends CachingWriter {
return this.image_options.formats;
}

const preferredFormats = ['webp'];
const preferredFormats = ['webp', 'avif'];
// generate next-gen formats, but also include the original one (e.g. png or jpeg)
return preferredFormats.includes(meta.format)
? preferredFormats
Expand Down
40 changes: 32 additions & 8 deletions node-tests/generate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,78 @@ const hash = '00e24234f1b58e32b935b1041432916f';
const images = [
{
file: 'assets/images/image.jpg',
type: 'image/jpeg',
type: 'jpeg',
sizes: [50, 100],
alpha: false,
jpeqQuality: 50,
removeSource: true,
},
{
file: 'assets/images/image.webp',
type: 'image/webp',
type: 'webp',
sizes: [50, 100],
alpha: false,
removeSource: true,
},
{
file: 'assets/images/image.avif',
// sharp wrongly reports this as heif. See https://github.com/lovell/sharp/issues/2504
type: 'heif',
sizes: [50, 100],
alpha: false,
removeSource: true,
},
{
file: 'assets/images/test.png',
type: 'image/png',
type: 'png',
sizes: [50, 100],
alpha: true,
removeSource: true,
},
{
file: 'assets/images/test.webp',
type: 'image/webp',
type: 'webp',
sizes: [50, 100],
alpha: true,
removeSource: true,
},
{
file: 'assets/images/test.avif',
type: 'heif',
sizes: [50, 100],
alpha: true,
removeSource: true,
},
{
file: 'assets/images/recursive/dir/test.png',
type: 'image/png',
type: 'png',
sizes: [50, 100],
alpha: false,
removeSource: true,
},
{
file: 'assets/images/small.png',
type: 'image/png',
type: 'png',
sizes: [10, 25],
alpha: false,
removeSource: false,
},
{
file: 'assets/images/small.webp',
type: 'image/webp',
type: 'webp',
sizes: [10, 25],
alpha: false,
removeSource: true,
},
// image output is somehow broken for avif at the the tiny size of 10px
// and sharp reports the size of 25 as 24 :-/
// {
// file: 'assets/images/small.avif',
// type: 'heif',
// sizes: [10, 25],
// alpha: false,
// removeSource: true,
// },
];
const aspectRatio = 2;
const appDir = './dist';
Expand All @@ -80,7 +104,7 @@ images.forEach((img) => {
expect(meta).toBeDefined();
expect(meta.width).toEqual(width);
expect(meta.height).toEqual(Math.round(width / aspectRatio));
expect(meta.format).toEqual(img.type.split('/')[1]);
expect(meta.format).toEqual(img.type);
expect(meta.hasAlpha).toEqual(img.alpha);
expect(meta.hasProfile).toEqual(false);
expect(fs.existsSync(originalSource)).toBe(!img.removeSource);
Expand Down
71 changes: 70 additions & 1 deletion tests/integration/components/responsive-image-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ module('Integration: Responsive Image Component', function (hooks) {
.dom('picture source[type="image/webp"]')
.hasAttribute('srcset', new RegExp('/assets/images/test50w.webp 50w'));

// avif
assert
.dom('picture source[type="image/avif"]')
.hasAttribute(
'srcset',
new RegExp('/assets/images/test640w.avif 640w')
);
assert
.dom('picture source[type="image/avif"]')
.hasAttribute(
'srcset',
new RegExp('/assets/images/test100w.avif 100w')
);
assert
.dom('picture source[type="image/avif"]')
.hasAttribute('srcset', new RegExp('/assets/images/test50w.avif 50w'));

await render(hbs`<ResponsiveImage @image="assets/images/small.png"/>`);
// png
assert
Expand All @@ -68,6 +85,14 @@ module('Integration: Responsive Image Component', function (hooks) {
.dom('picture source[type="image/webp"]')
.hasAttribute('srcset', new RegExp('/assets/images/small25w.webp 25w'));

// avif
assert
.dom('picture source[type="image/avif"]')
.hasAttribute('srcset', new RegExp('/assets/images/small10w.avif 10w'));
assert
.dom('picture source[type="image/avif"]')
.hasAttribute('srcset', new RegExp('/assets/images/small25w.avif 25w'));

await render(
hbs`<ResponsiveImage @image="assets/images/recursive/dir/test.png"/>`
);
Expand Down Expand Up @@ -98,6 +123,20 @@ module('Integration: Responsive Image Component', function (hooks) {
'srcset',
new RegExp('/assets/images/recursive/dir/test50w.webp 50w')
);

// avif
assert
.dom('picture source[type="image/avif"]')
.hasAttribute(
'srcset',
new RegExp('/assets/images/recursive/dir/test100w.avif 100w')
);
assert
.dom('picture source[type="image/avif"]')
.hasAttribute(
'srcset',
new RegExp('/assets/images/recursive/dir/test50w.avif 50w')
);
});

test('it renders the fallback src next to needed display size', async function (assert) {
Expand Down Expand Up @@ -219,6 +258,14 @@ module('Integration: Responsive Image Component', function (hooks) {
.dom('picture source[type="image/webp"]')
.hasAttribute('srcset', new RegExp('/assets/images/test50w.webp 1x'));

// avif
assert
.dom('picture source[type="image/avif"]')
.hasAttribute('srcset', new RegExp('/assets/images/test100w.avif 2x'));
assert
.dom('picture source[type="image/avif"]')
.hasAttribute('srcset', new RegExp('/assets/images/test50w.avif 1x'));

await render(
hbs`<ResponsiveImage @width={{10}} @image="assets/images/small.png"/>`
);
Expand All @@ -238,6 +285,14 @@ module('Integration: Responsive Image Component', function (hooks) {
.dom('picture source[type="image/webp"]')
.hasAttribute('srcset', new RegExp('/assets/images/small25w.webp 2x'));

// avif
assert
.dom('picture source[type="image/avif"]')
.hasAttribute('srcset', new RegExp('/assets/images/small10w.avif 1x'));
assert
.dom('picture source[type="image/avif"]')
.hasAttribute('srcset', new RegExp('/assets/images/small25w.avif 2x'));

await render(
hbs`<ResponsiveImage @image="assets/images/recursive/dir/test.png"/>`
);
Expand Down Expand Up @@ -268,6 +323,20 @@ module('Integration: Responsive Image Component', function (hooks) {
'srcset',
new RegExp('/assets/images/recursive/dir/test50w.webp 50w')
);

// avif
assert
.dom('picture source[type="image/avif"]')
.hasAttribute(
'srcset',
new RegExp('/assets/images/recursive/dir/test100w.avif 100w')
);
assert
.dom('picture source[type="image/avif"]')
.hasAttribute(
'srcset',
new RegExp('/assets/images/recursive/dir/test50w.avif 50w')
);
});

test('it renders the fallback src next to needed display size', async function (assert) {
Expand Down Expand Up @@ -302,7 +371,7 @@ module('Integration: Responsive Image Component', function (hooks) {
await render(hbs`<ResponsiveImage @image="assets/images/test.png"/>`);

assert.dom('picture').exists({ count: 1 });
assert.dom('picture source').exists({ count: 2 });
assert.dom('picture source').exists({ count: 3 });
assert.dom('picture source[type="image/png"]').exists({ count: 1 });
assert.dom('picture source[type="image/webp"]').exists({ count: 1 });
});
Expand Down