1 import { copy, readFile, remove, rename } from 'fs-extra'
2 import Jimp, { read } from 'jimp'
3 import { getLowercaseExtension } from './core-utils'
4 import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
5 import { logger } from './logger'
6 import { buildUUID } from './uuid'
8 function generateImageFilename (extension = '.jpg') {
9 return buildUUID() + extension
12 async function processImage (
15 newSize: { width: number, height: number },
18 const extension = getLowercaseExtension(path)
20 if (path === destination) {
21 throw new Error('Jimp/FFmpeg needs an input path different that the output path.')
24 logger.debug('Processing image %s to %s.', path, destination)
26 // Use FFmpeg to process GIF
27 if (extension === '.gif') {
28 await processGIF(path, destination, newSize)
30 await jimpProcessor(path, destination, newSize, extension)
33 if (keepOriginal !== true) await remove(path)
36 // ---------------------------------------------------------------------------
39 generateImageFilename,
43 // ---------------------------------------------------------------------------
45 async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }, inputExt: string) {
47 const inputBuffer = await readFile(path)
50 sourceImage = await read(inputBuffer)
52 logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err })
54 const newName = path + '.jpg'
55 await convertWebPToJPG(path, newName)
56 await rename(newName, path)
58 sourceImage = await read(path)
61 await remove(destination)
63 // Optimization if the source file has the appropriate size
64 const outputExt = getLowercaseExtension(destination)
65 if (skipProcessing({ sourceImage, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt })) {
66 return copy(path, destination)
69 await autoResize({ sourceImage, newSize, destination })
72 async function autoResize (options: {
74 newSize: { width: number, height: number }
77 const { sourceImage, newSize, destination } = options
79 // Portrait mode targetting a landscape, apply some effect on the image
80 const sourceIsPortrait = sourceImage.getWidth() < sourceImage.getHeight()
81 const destIsPortraitOrSquare = newSize.width <= newSize.height
83 if (sourceIsPortrait && !destIsPortraitOrSquare) {
84 const baseImage = sourceImage.cloneQuiet().cover(newSize.width, newSize.height)
85 .color([ { apply: 'shade', params: [ 50 ] } ])
87 const topImage = sourceImage.cloneQuiet().contain(newSize.width, newSize.height)
89 return write(baseImage.blit(topImage, 0, 0), destination)
92 return write(sourceImage.cover(newSize.width, newSize.height), destination)
95 function write (image: Jimp, destination: string) {
96 return image.quality(80).writeAsync(destination)
99 function skipProcessing (options: {
101 newSize: { width: number, height: number }
106 const { sourceImage, newSize, imageBytes, inputExt, outputExt } = options
107 const { width, height } = newSize
109 if (sourceImage.getWidth() > width || sourceImage.getHeight() > height) return false
110 if (inputExt !== outputExt) return false
114 if (height >= 1000) return imageBytes <= 200 * kB
115 if (height >= 500) return imageBytes <= 100 * kB
117 return imageBytes <= 15 * kB