Skip to content

Instantly share code, notes, and snippets.

@igorescobar
Last active May 16, 2020 16:36
Show Gist options
  • Save igorescobar/7d0b9604f889d0b3f3f0222fbfbae415 to your computer and use it in GitHub Desktop.
Save igorescobar/7d0b9604f889d0b3f3f0222fbfbae415 to your computer and use it in GitHub Desktop.
Generating ImageBoss URLs with JavaScript
function mountImageBossUrl(src, { source, operation, cover_mode, width, height, options }) {
var serviceUrl = 'https://img.imageboss.me';
var template = '/:source/:operation/:options/';
if (operation === 'cover') {
template = '/:source/:operation::cover_mode/:widthx:height/:options/';
} else if (operation === 'width') {
template = '/:source/:operation/:width/:options/';
} else if (operation === 'height') {
template = '/:source/:operation/:height/:options/';
}
var finalUrl = template
.replace(':source', source || '')
.replace(':operation', operation || 'cdn')
.replace(':cover_mode', cover_mode || '')
.replace(':width', width || '')
.replace(':height', height || '')
.replace(':options', options || '')
.replace(/\/\//g, '/')
return serviceUrl + finalUrl + src;
}
var image = '/img.jpg';
console.log(mountImageBossUrl(image, {source: 'my-images', operation: 'cdn'}));
console.log(mountImageBossUrl(image, {source: 'my-images', operation: 'cover', width: 500, height: 500 }));
console.log(mountImageBossUrl(image, {source: 'my-images', operation: 'cover', cover_mode: 'face', width: 500, height: 500 }));
console.log(mountImageBossUrl(image, {source: 'my-images', operation: 'cover', cover_mode: 'face', width: 500, height: 500, options: 'grayscale:true' }));
console.log(mountImageBossUrl(image, {source: 'my-images', operation: 'width', width: 500 }));
console.log(mountImageBossUrl(image, {source: 'my-images', operation: 'height', height: 500 }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment