-
Notifications
You must be signed in to change notification settings - Fork 17
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 #137 from kaliber5/blurhash
Add support for blurhash-based LQIP
- Loading branch information
Showing
11 changed files
with
266 additions
and
4 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
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,80 @@ | ||
const sharp = require('sharp'); | ||
|
||
class LqipBlurhashPlugin { | ||
constructor(addon) { | ||
this.processed = []; | ||
this.metaData = new Map(); | ||
|
||
if ( | ||
addon.addonOptions.find( | ||
(imageConfig) => | ||
imageConfig.lqip && imageConfig.lqip.type === 'blurhash' | ||
) | ||
) { | ||
this.blurhash = require('blurhash'); | ||
addon.options['@embroider/macros'].setOwnConfig.usesBlurhash = true; | ||
|
||
addon.addMetadataExtension(this.addMetaData, this); | ||
addon.addImagePreProcessor(this.imagePreProcessor, this); | ||
} | ||
} | ||
|
||
canProcessImage(config) { | ||
return config.lqip && config.lqip.type === 'blurhash'; | ||
} | ||
|
||
async getLqipDimensions(config, sharped) { | ||
const meta = await sharped.metadata(); | ||
const targetPixels = config.lqip.targetPixels || 16; | ||
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 rawWidth = width * 8; | ||
const rawHeight = height * 8; | ||
const buffer = await sharped.toBuffer(); | ||
const lqi = await sharp(buffer) | ||
.ensureAlpha() | ||
.resize(rawWidth, rawHeight, { | ||
fit: 'fill', | ||
}) | ||
.raw(); | ||
|
||
const data = new Uint8ClampedArray(await lqi.toBuffer()); | ||
const hash = this.blurhash.encode(data, rawWidth, rawHeight, width, height); | ||
|
||
const meta = { | ||
type: 'blurhash', | ||
hash, | ||
width, | ||
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 = LqipBlurhashPlugin; |
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.