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

Support custom parameters for imgix provider #311

Merged
merged 1 commit into from
Jul 23, 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
7 changes: 7 additions & 0 deletions addon/helpers/responsive-image-imgix-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface ImgixConfig {
}

interface ImgixOptions {
params?: Record<string, string | number>;
formats?: ImageType[];
}

Expand All @@ -35,6 +36,12 @@ export const provider = (
params.set('w', String(width));
params.set('fit', 'max');

if (options.params) {
for (const [key, value] of Object.entries(options.params)) {
params.set(key, String(value));
}
}

return url.toString();
},
};
Expand Down
10 changes: 10 additions & 0 deletions docs/providers/imgix.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
<ResponsiveImage @src={{responsive-image-imgix-provider "path/to/image.jpg"}}/>
```

### Custom parameters

Besides the transformations that the addon itself implicitly adds (related to resizing images)
you can add your own [imgix parameters](https://docs.imgix.com/apis/rendering) by passing a `params` hash:

```hbs
<ResponsiveImage @src={{responsive-image-imgix-provider "path/to/image.jpg" params=(hash monochrome="44768B" px=10)}}/>
```


### Image formats

By default, all image formats supported by imgix (PNG, JPEG, WEBP, but no AVIF yet) are referenced in the generated `<source>` tags.
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/app/templates/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<h2>imgix</h2>

<ResponsiveImage @src={{responsive-image-imgix-provider "pages/approach/agile_2000x1200.jpg"}} @width={{320}}/>
<ResponsiveImage @src={{responsive-image-imgix-provider "pages/approach/agile_2000x1200.jpg" params=(hash monochrome="44768B")}} @width={{320}}/>

<h2>Local</h2>

Expand Down
13 changes: 13 additions & 0 deletions tests/integration/helpers/responsive-image-imgix-provider-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ module(
);
});

test('it supports custom params', async function (assert) {
await render(
hbs`{{dump (responsive-image-imgix-provider "foo/bar.jpg" params=(hash monochrome="44768B" px=10))}}`
);

const data = getData() as ProviderResult;

assert.equal(
data.imageUrlFor(100, 'jpeg'),
'https://kaliber5.imgix.net/foo/bar.jpg?fm=jpg&w=100&fit=max&monochrome=44768B&px=10'
);
});

test('it supports custom image formats', async function (assert) {
await render(
hbs`{{dump (responsive-image-imgix-provider "foo/bar.jpg" formats=(array "webp" "jpeg"))}}`
Expand Down