Files
flyer-crawler.projectium.com/src/utils/imageProcessor.ts
Torben Sorensen 1df7cf6801
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Has been cancelled
fix image saving + new icon
2025-11-30 01:25:53 -08:00

34 lines
1.2 KiB
TypeScript

// src/utils/imageProcessor.ts
import sharp from 'sharp';
import path from 'path';
import fs from 'fs/promises';
import { logger } from '../services/logger.server';
/**
* Generates a 64x64 square icon from a source image.
* @param sourceImagePath The full path to the original image file.
* @param iconsDirectory The directory where the icon should be saved.
* @returns A promise that resolves to the filename of the newly created icon.
* @throws An error if the icon generation fails.
*/
export async function generateFlyerIcon(sourceImagePath: string, iconsDirectory: string): Promise<string> {
try {
const originalFileName = path.basename(sourceImagePath);
const iconFileName = `icon-${originalFileName}`;
const iconOutputPath = path.join(iconsDirectory, iconFileName);
// Ensure the icons subdirectory exists.
await fs.mkdir(iconsDirectory, { recursive: true });
// Use sharp to resize the image to 64x64.
await sharp(sourceImagePath)
.resize(64, 64)
.toFile(iconOutputPath);
logger.info(`Generated 64x64 icon: ${iconFileName}`);
return iconFileName;
} catch (error) {
logger.error('Failed to generate flyer icon:', { error, sourceImagePath });
throw new Error('Icon generation failed.');
}
}