@danielbiegler/vendure-plugin-blurry-image-lazy-loading
Version:
Generates image hashes for displaying blurry previews when loading images on the frontend.
35 lines (34 loc) • 1.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ThumbHashStrategy = void 0;
const thumbhash_1 = require("../vendors/thumbhash");
const PreviewImageHashStrategy_1 = require("./PreviewImageHashStrategy");
/**
* A very compact representation of a placeholder for an image.
* Store it inline with your data and show it while the real image is loading for a smoother loading experience.
*
* It's similar to BlurHash but with the following advantages:
* - Encodes more detail in the same space
* - Also encodes the aspect ratio
* - Gives more accurate colors
* - Supports images with alpha
*
* @category Strategy
*/
class ThumbHashStrategy extends PreviewImageHashStrategy_1.PreviewImageHashBase {
constructor(args = {}) {
super(args);
}
async encode(buffer, input) {
const resizeOptions = {
// Clone so as to not overwrite `width` and or `height`
...this.resizeOptions,
width: input.width ?? this.resizeOptions.width,
height: input.height ?? this.resizeOptions.height,
};
const result = await buffer.resize(resizeOptions).raw().ensureAlpha().toBuffer({ resolveWithObject: true });
const hash = (0, thumbhash_1.rgbaToThumbHash)(result.info.width, result.info.height, result.data);
return Buffer.from(hash).toString(this.encoding);
}
}
exports.ThumbHashStrategy = ThumbHashStrategy;