Skip to content

Commit

Permalink
feat: add lqip directive
Browse files Browse the repository at this point in the history
Adds a low-quality-placeholder directive as discussed in #86
  • Loading branch information
JonasKruckenberg committed May 14, 2021
1 parent db919aa commit 373839b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/core/src/transforms/lqip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getMetadata } from "../lib/metadata";
import { TransformFactory } from "../types";

export interface LqipOptions {
lqip: string
}

export const lqip: TransformFactory<LqipOptions> = (config, ctx) => {
const width = Math.log(1 + parseInt(config.lqip || '')) * 10
const quality = clamp(Math.log(1 + parseInt(config.lqip || '')) * 10, 0, 100)

if(!width || !quality) return

return function lqipTransform(image) {
const format = getMetadata(image, 'format') // needed for the quality directive

console.log(width,quality);

return image
.toFormat(format,{ quality: Math.floor(quality) })
.resize({ width: Math.floor(width) })
}
}

function clamp(num: number, min: number, max: number) {
const t = num < min ? min : num
return t > max ? max : t
}

0 comments on commit 373839b

Please sign in to comment.