fp/packages/image/index.ts

30 lines
926 B
TypeScript

// import ColorThief from 'colorthief';
import sharp from 'sharp';
import Prevvy from 'prevvy';
import path from 'path';
import { getTmpFile } from '@futureporn/utils';
export async function getProminentColor(imageFile: string): Promise<string> {
const { dominant } = await sharp(imageFile).stats();
const { r, g, b } = dominant;
return rgbToHex(r, g, b);
}
export function rgbToHex(r: number, g: number, b: number): string {
return "#" + (1 << 24 | r << 16 | g << 8 | b).toString(16).slice(1);
}
export async function getStoryboard(imageFileOrUrl: string): Promise<string> {
let base = path.basename(imageFileOrUrl);
let outputImagePath = getTmpFile(base);
let options = {
input: imageFileOrUrl,
output: outputImagePath,
width: 265,
cols: 5,
rows: 5,
};
let prevvy = new Prevvy(options);
await prevvy.generate();
return outputImagePath;
}