Skip to content

Commit

Permalink
feat: add support for selected image quality parameters (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseZapata3 authored Sep 6, 2024
1 parent 331cfaa commit 46b3650
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/controlPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export default class ControlPanel {
url, author, profileLink, downloadLocation,
}) {
this.onSelectImage({
url,
url: this.unsplashClient.dynamicImageResizing(url),
unsplash: {
author,
profileLink,
Expand Down
17 changes: 17 additions & 0 deletions src/unsplashClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default class UnsplashClient {
// Remove trailing slashes from the URL
this.apiUrl = config && config.apiUrl.replace(/\/+$/, '');
this.perPage = config && config.maxResults ? config.maxResults : 30;
this.imageParamsModifier = config && config.imageParams ? config.imageParams : {};
}

/**
Expand Down Expand Up @@ -56,6 +57,22 @@ export default class UnsplashClient {
};
}

/**
* Applies image resize parameters on the given URL.
* https://unsplash.com/documentation#dynamically-resizable-images
* @param {String} imageUrl Url of the selected image
* @returns Url of the selected image with dynamic resizing parameters
*/
dynamicImageResizing(imageUrl) {
const newParams = Object.entries(this.imageParamsModifier);
const [urlBase, queryString] = imageUrl.split('?');
const existingParams = new URLSearchParams(queryString);
newParams.forEach(([key, value]) => {
existingParams.set(key, value);
});
return `${urlBase}?${existingParams.toString()}`;
}

/**
* Download image from Unsplash
* Required by Unsplash API Guideline for tracking purposes
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/unsplashClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ const unsplashConfig = {
apiUrl: 'http://localhost',
appName: 'DemoApp',
maxResults: 30,
imageParams: {
q: 90,
w: 1500,
fit: 'max',
},
};

/**
Expand Down
22 changes: 22 additions & 0 deletions test/unsplashClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,26 @@ describe('UnsplashClient', () => {
});
});
});
describe('modification of parameters of the selected image', () => {
beforeEach(() => {
unsplashClient = createUnsplashClient();
});
it('add new parameters to URL without existing parameters', () => {
const inputUrl = 'https://example.com/image.jpg?ixid=M3w2NTA0MjJ8MHwxfHNlYXJjaHwxfHxjYXR8ZW58MHx8fHwxNzI1NTQzOTQ2fDA&ixlib=rb-4.0.3';
const result = unsplashClient.dynamicImageResizing(inputUrl);
expect(result).toBe(`${inputUrl}&q=90&w=1500&fit=max`);
});

it('modify existing parameters in URL', () => {
const inputUrl = 'https://example.com/image.jpg?ixid=M3w2NTA0MjJ8MHwxfHNlYXJjaHwxfHxjYXR8ZW58MHx8fHwxNzI1NTQzOTQ2fDA&ixlib=rb-4.0.3&q=60&w=100&fit=min';
const result = unsplashClient.dynamicImageResizing(inputUrl);
expect(result).toBe('https://example.com/image.jpg?ixid=M3w2NTA0MjJ8MHwxfHNlYXJjaHwxfHxjYXR8ZW58MHx8fHwxNzI1NTQzOTQ2fDA&ixlib=rb-4.0.3&q=90&w=1500&fit=max');
});

it('modify the existing and new parameters in the URL', () => {
const inputUrl = 'https://example.com/image.jpg?ixid=M3w2NTA0MjJ8MHwxfHNlYXJjaHwxfHxjYXR8ZW58MHx8fHwxNzI1NTQzOTQ2fDA&ixlib=rb-4.0.3&w=100';
const result = unsplashClient.dynamicImageResizing(inputUrl);
expect(result).toBe('https://example.com/image.jpg?ixid=M3w2NTA0MjJ8MHwxfHNlYXJjaHwxfHxjYXR8ZW58MHx8fHwxNzI1NTQzOTQ2fDA&ixlib=rb-4.0.3&w=1500&q=90&fit=max');
});
});
});

0 comments on commit 46b3650

Please sign in to comment.