Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Has been cancelled
34 lines
1.2 KiB
TypeScript
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.');
|
|
}
|
|
} |