import { Readable } from 'stream'; import gm from 'gm'; import { MAX_IMAGE_SIZE, ONLY_RESIZE_IF_IMAGE_IS_BIGGER, TO_BE_RESIZED_DIRECTORY_NAME } from './constants'; const imageMagick = gm.subClass({ imageMagick: true }); export const shouldResizeImage = (filePath: string): boolean => filePath.includes(TO_BE_RESIZED_DIRECTORY_NAME); export const resizeImageToFitDimensions = (filePath: string): Promise => new Promise((resolve, reject) => { imageMagick(filePath) .resize(MAX_IMAGE_SIZE, MAX_IMAGE_SIZE, ONLY_RESIZE_IF_IMAGE_IS_BIGGER) .write(filePath, (err, stdout) => { if (err) { reject(Error(`Failed to resize image: ${err}`)); } else { console.log(`Successfully resized image: ${filePath}`); resolve(stdout); } }); });