-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from kaliber5/lqip
Add built-in support for a blurry placeholder (LQIP)
- Loading branch information
Showing
14 changed files
with
245 additions
and
14 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// taken from https://github.com/google/eleventy-high-performance-blog/blob/5ed39db7fd3f21ae82ac1a8e833bf283355bd3d0/_11ty/blurry-placeholder.js#L51 | ||
|
||
export default function blurrySvg( | ||
src: string, | ||
width: number, | ||
height: number | ||
): string { | ||
return `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 ${width} ${height}"> | ||
<filter id="b" color-interpolation-filters="sRGB"><feGaussianBlur stdDeviation=".5"></feGaussianBlur><feComponentTransfer><feFuncA type="discrete" tableValues="1 1"></feFuncA></feComponentTransfer></filter> | ||
<image filter="url(#b)" preserveAspectRatio="none" height="100%" width="100%" xlink:href="${src}"></image> | ||
</svg>`; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function dataUri( | ||
data: string, | ||
type: string, | ||
base64 = false | ||
): string { | ||
return `data:${type};base64,${base64 ? data : btoa(data)}`; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const sharp = require('sharp'); | ||
|
||
class LqipInlinePlugin { | ||
constructor(addon) { | ||
this.processed = []; | ||
this.metaData = new Map(); | ||
|
||
addon.addMetadataExtension(this.addMetaData, this); | ||
addon.addImagePreProcessor(this.imagePreProcessor, this); | ||
} | ||
|
||
canProcessImage(config) { | ||
return config.lqip && config.lqip.type === 'inline'; | ||
} | ||
|
||
async getLqipDimensions(config, sharped) { | ||
const meta = await sharped.metadata(); | ||
const targetPixels = config.lqip.targetPixels || 60; | ||
const aspectRatio = meta.width / meta.height; | ||
|
||
// taken from https://github.com/google/eleventy-high-performance-blog/blob/5ed39db7fd3f21ae82ac1a8e833bf283355bd3d0/_11ty/blurry-placeholder.js#L74-L92 | ||
let bitmapHeight = targetPixels / aspectRatio; | ||
bitmapHeight = Math.sqrt(bitmapHeight); | ||
const bitmapWidth = targetPixels / bitmapHeight; | ||
return { width: Math.round(bitmapWidth), height: Math.round(bitmapHeight) }; | ||
} | ||
|
||
async imagePreProcessor(sharped, image, _width, config) { | ||
if (this.processed.includes(image) || !this.canProcessImage(config)) { | ||
return sharped; | ||
} | ||
this.processed.push(image); | ||
|
||
const { width, height } = await this.getLqipDimensions(config, sharped); | ||
const buffer = await sharped.toBuffer(); | ||
const lqi = await sharp(buffer) | ||
.resize(width, height, { | ||
withoutEnlargement: true, | ||
fit: 'fill', | ||
}) | ||
.png(); | ||
|
||
const sharpMeta = await lqi.metadata(); | ||
|
||
const meta = { | ||
image: (await lqi.toBuffer()).toString('base64'), | ||
width: sharpMeta.width, | ||
height: sharpMeta.height, | ||
}; | ||
|
||
this.metaData.set(image, meta); | ||
|
||
return sharped; | ||
} | ||
|
||
addMetaData(image, metadata /*, config*/) { | ||
const ourMeta = this.metaData.get(image); | ||
if (ourMeta) { | ||
metadata.lqip = ourMeta; | ||
} | ||
|
||
return metadata; | ||
} | ||
} | ||
|
||
module.exports = LqipInlinePlugin; |
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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.