Skip to content

Commit

Permalink
Support Cloudinary's chained transformations
Browse files Browse the repository at this point in the history
  • Loading branch information
simonihmig committed Jul 23, 2021
1 parent 658eeeb commit f360c27
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
22 changes: 12 additions & 10 deletions addon/helpers/responsive-image-cloudinary-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,18 @@ export const provider = (
return {
imageTypes: options.formats ?? ['png', 'jpeg', 'webp', 'avif'],
imageUrlFor(width: number, type: ImageType = 'jpeg'): string {
const params = [
`w_${width}`,
'c_limit',
'q_auto',
deliveryType !== 'upload' ? `f_${type}` : undefined,
options.transformations,
];
return `https://res.cloudinary.com/${cloudName}/image/${deliveryType}/${params
.filter(Boolean)
.join(',')}/${imageId}${deliveryType === 'upload' ? '.' + type : ''}`;
let resizeParams = `w_${width},c_limit,q_auto`;
if (deliveryType !== 'upload') {
resizeParams += `,f_${type}`;
}

const params = options.transformations
? `${options.transformations}/${resizeParams}`
: resizeParams;

return `https://res.cloudinary.com/${cloudName}/image/${deliveryType}/${params}/${imageId}${
deliveryType === 'upload' ? '.' + type : ''
}`;
},
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,20 @@ module(

assert.equal(
data.imageUrlFor(100, 'jpeg'),
'https://res.cloudinary.com/kaliber5/image/upload/w_100,c_limit,q_auto,co_rgb:20a020,e_colorize:50/samples/animals/three-dogs.jpeg'
'https://res.cloudinary.com/kaliber5/image/upload/co_rgb:20a020,e_colorize:50/w_100,c_limit,q_auto/samples/animals/three-dogs.jpeg'
);
});

test('it supports custom chained transformations', async function (assert) {
await render(
hbs`{{dump (responsive-image-cloudinary-provider "samples/animals/three-dogs" transformations="co_rgb:20a020,e_colorize:50/ar_1.0,c_fill,w_150/r_max")}}`
);

const data = getData() as ProviderResult;

assert.equal(
data.imageUrlFor(100, 'jpeg'),
'https://res.cloudinary.com/kaliber5/image/upload/co_rgb:20a020,e_colorize:50/ar_1.0,c_fill,w_150/r_max/w_100,c_limit,q_auto/samples/animals/three-dogs.jpeg'
);
});

Expand Down

0 comments on commit f360c27

Please sign in to comment.