1 import { remove, rename } from 'fs-extra'
2 import { extname } from 'path'
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 if (path === destination) {
17 throw new Error('Jimp/FFmpeg needs an input path different that the output path.')
20 logger.debug('Processing image %s to %s.', path, destination)
22 // Use FFmpeg to process GIF
23 if (extension === '.gif') {
24 await processGIF(path, destination, newSize)
26 await jimpProcessor(path, destination, newSize)
29 if (keepOriginal !== true) await remove(path)
32 // ---------------------------------------------------------------------------
38 // ---------------------------------------------------------------------------
40 async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }) {
44 jimpInstance = await Jimp.read(path)
46 logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err })
48 const newName = path + '.jpg'
49 await convertWebPToJPG(path, newName)
50 await rename(newName, path)
52 jimpInstance = await Jimp.read(path)
55 await remove(destination)
58 .resize(newSize.width, newSize.height)
60 .writeAsync(destination)