Skip to content

Commit

Permalink
fix(twicpics): fix minor issues and adds custom operations (#132)
Browse files Browse the repository at this point in the history
* TwicPics provider fixes & examples

* Optimizes TwicPics runtime

* Removes duplicate TwicPics examples

* Adds `focus`and `zoom` to TwicPics’ operations
  • Loading branch information
florentb authored Dec 30, 2020
1 parent 8600d02 commit 5a4e5d3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
7 changes: 7 additions & 0 deletions playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@

<h2>JPEG image on TwicPics</h2>
<NuxtImg provider="twicpics" src="/football.jpg" />
<NuxtImg
provider="twicpics"
src="/football.jpg"
width="250"
height="400"
:operations="{ focus: 'auto' }"
/>
<NuxtImg provider="twicpics" src="/football.jpg" width="100" height="100" quality="1" />

<h2>JPEG image on Fastly</h2>
Expand Down
31 changes: 25 additions & 6 deletions src/providers/twicpics/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,37 @@ const fits = createMapper({

const operationsGenerator = createOperationsGenerator({
keyMap: {
format: 'format',
format: 'output',
quality: 'quality',
background: 'background'
background: 'background',
focus: 'focus',
zoom: 'zoom'
},
valueMap: {
format (value) {
format (value: string) {
if (value === 'jpg') {
return 'jpeg'
}
return value
},
background (value: string) {
if (value.startsWith('#')) {
return value.replace('#', '')
}
return value
},
focus: {
auto: 'auto',
faces: 'faces',
north: '50px0p',
northEast: '100px0p',
northWest: '0px0p',
west: '0px50p',
southWest: '100px100p',
south: '50px100p',
southEast: '0px100p',
east: '100px50p',
center: '50px50p'
}
},
joinWith: '/',
Expand All @@ -36,9 +57,7 @@ export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL = '/'
providerModifiers[fits(fit)] = `${width || '-'}x${height || '-'}`
}
const operations = operationsGenerator(providerModifiers)
const twicpicsOperations = (operations) ? '?twic=v1/' + operations : ''

return {
url: joinURL(baseURL, src + twicpicsOperations)
url: joinURL(baseURL, src + (operations ? ('?twic=v1/' + operations) : ''))
}
}

0 comments on commit 5a4e5d3

Please sign in to comment.