Last active
July 16, 2018 20:10
-
-
Save hputzek/48f63df97e6e2a8a114569b17b664b1c to your computer and use it in GitHub Desktop.
Processing images with buttercms - for more details refer to https://buttercms.com/docs/api/#image-api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* example options: | |
teaserImageConfig: [ | |
{ | |
resize: | |
{ | |
width: '420', | |
height: '420', | |
fit: 'crop' | |
}, | |
quality: {value: 80} | |
}, | |
{ | |
resize: | |
{ | |
width: '768' | |
}, | |
quality: {value: 80} | |
}, | |
{ | |
resize: | |
{ | |
width: '1024' | |
}, | |
quality: {value: 80} | |
} | |
] | |
Another example: | |
thumbnailOptions: { | |
resize: { | |
width: '380', | |
height: '380', | |
fit: 'crop' | |
}, | |
monochrome: {} | |
} | |
Example call: getProcessedImageUrl('URL_FROM_BUTTERCMS_API', thumbnailOptions); | |
this will return the URL to the image processed with the given options. | |
*/ | |
function getImageIdFromUrl (url) { | |
return url.substr(url.lastIndexOf('/') + 1) | |
} | |
function buildUrlParametersFromImageOptions (options) { | |
let imageUrl = '' | |
Object.keys(options).forEach(function (optionGroup) { | |
imageUrl += '/' + optionGroup + '=' | |
Object.keys(options[optionGroup]).forEach(function (option, index) { | |
imageUrl += option + ':' + options[optionGroup][option] | |
if (Object.keys(options[optionGroup]).length - 1 > index) { | |
imageUrl += ',' | |
} | |
}) | |
imageUrl += '/' | |
}) | |
return imageUrl | |
} | |
export function getProcessedImageUrl (imageUrl, processingOptions) { | |
if (!imageUrl) { | |
return '' | |
} | |
const imageId = getImageIdFromUrl(imageUrl) | |
const urlParameters = buildUrlParametersFromImageOptions(processingOptions) | |
return "https://fs.buttercms.com + urlParameters" + imageId | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment