import sharp from 'sharp'; import { SeededRandom } from 'src/generators/timeline/utils'; export const randomThumbnail = async (seed: string, ratio: number) => { const height = 235; const width = Math.round(height * ratio); return randomImageFromString(seed, { width, height }); }; export const randomPreview = async (seed: string, ratio: number) => { const height = 500; const width = Math.round(height * ratio); return randomImageFromString(seed, { width, height }); }; export const randomImageFromString = async ( seed: string = '', { width = 100, height = 100 }: { width: number; height: number }, ) => { // Convert string to number for seeding let seedNumber = 0; for (let i = 0; i < seed.length; i++) { seedNumber = (seedNumber << 5) - seedNumber + (seed.codePointAt(i) ?? 0); seedNumber = seedNumber & seedNumber; // Convert to 32bit integer } return randomImage(new SeededRandom(Math.abs(seedNumber)), { width, height }); }; export const randomImage = async (rng: SeededRandom, { width, height }: { width: number; height: number }) => { const r1 = rng.nextInt(0, 256); const g1 = rng.nextInt(0, 256); const b1 = rng.nextInt(0, 256); const r2 = rng.nextInt(0, 256); const g2 = rng.nextInt(0, 256); const b2 = rng.nextInt(0, 256); const patternType = rng.nextInt(0, 5); let svgPattern = ''; switch (patternType) { case 0: { // Solid color svgPattern = ` `; break; } case 1: { // Horizontal stripes const stripeHeight = 10; svgPattern = ` ${Array.from( { length: height / stripeHeight }, (_, i) => ``, ).join('')} `; break; } case 2: { // Vertical stripes const stripeWidth = 10; svgPattern = ` ${Array.from( { length: width / stripeWidth }, (_, i) => ``, ).join('')} `; break; } case 3: { // Checkerboard const squareSize = 10; svgPattern = ` ${Array.from({ length: height / squareSize }, (_, row) => Array.from({ length: width / squareSize }, (_, col) => { const isEven = (row + col) % 2 === 0; return ``; }).join(''), ).join('')} `; break; } case 4: { // Diagonal stripes svgPattern = ` `; break; } } const svgBuffer = Buffer.from(svgPattern); const jpegData = await sharp(svgBuffer).jpeg({ quality: 50 }).toBuffer(); return jpegData; };