1 import { extname } from 'path'
2 import { remove, rename } from 'fs-extra'
3 import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
4 import { logger } from './logger'
6 const Jimp = require('jimp')
8 async function processImage (
11 newSize: { width: number, height: number },
14 const extension = extname(path)
16 // Use FFmpeg to process GIF
17 if (extension === '.gif') {
18 return processGIF(path, destination, newSize, keepOriginal)
21 if (path === destination) {
22 throw new Error('Jimp needs an input path different that the output path.')
25 logger.debug('Processing image %s to %s.', path, destination)
30 jimpInstance = await Jimp.read(path)
32 logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err })
34 const newName = path + '.jpg'
35 await convertWebPToJPG(path, newName)
36 await rename(newName, path)
38 jimpInstance = await Jimp.read(path)
41 await remove(destination)
44 .resize(newSize.width, newSize.height)
46 .writeAsync(destination)
48 if (keepOriginal !== true) await remove(path)
51 // ---------------------------------------------------------------------------