1 import { copy, readFile, remove, rename } from 'fs-extra'
2 import Jimp, { read } from 'jimp'
3 import { buildUUID, getLowercaseExtension } from '@shared/core-utils'
4 import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
5 import { logger } from './logger'
7 function generateImageFilename (extension = '.jpg') {
8 return buildUUID() + extension
11 async function processImage (
14 newSize: { width: number, height: number },
17 const extension = getLowercaseExtension(path)
19 if (path === destination) {
20 throw new Error('Jimp/FFmpeg needs an input path different that the output path.')
23 logger.debug('Processing image %s to %s.', path, destination)
25 // Use FFmpeg to process GIF
26 if (extension === '.gif') {
27 await processGIF(path, destination, newSize)
29 await jimpProcessor(path, destination, newSize, extension)
32 if (keepOriginal !== true) await remove(path)
35 // ---------------------------------------------------------------------------
38 generateImageFilename,
42 // ---------------------------------------------------------------------------
44 async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }, inputExt: string) {
46 const inputBuffer = await readFile(path)
49 sourceImage = await read(inputBuffer)
51 logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err })
53 const newName = path + '.jpg'
54 await convertWebPToJPG(path, newName)
55 await rename(newName, path)
57 sourceImage = await read(path)
60 await remove(destination)
62 // Optimization if the source file has the appropriate size
63 const outputExt = getLowercaseExtension(destination)
64 if (skipProcessing({ sourceImage, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt })) {
65 return copy(path, destination)
68 await autoResize({ sourceImage, newSize, destination })
71 async function autoResize (options: {
73 newSize: { width: number, height: number }
76 const { sourceImage, newSize, destination } = options
78 // Portrait mode targetting a landscape, apply some effect on the image
79 const sourceIsPortrait = sourceImage.getWidth() < sourceImage.getHeight()
80 const destIsPortraitOrSquare = newSize.width <= newSize.height
82 if (sourceIsPortrait && !destIsPortraitOrSquare) {
83 const baseImage = sourceImage.cloneQuiet().cover(newSize.width, newSize.height)
84 .color([ { apply: 'shade', params: [ 50 ] } ])
86 const topImage = sourceImage.cloneQuiet().contain(newSize.width, newSize.height)
88 return write(baseImage.blit(topImage, 0, 0), destination)
91 return write(sourceImage.cover(newSize.width, newSize.height), destination)
94 function write (image: Jimp, destination: string) {
95 return image.quality(80).writeAsync(destination)
98 function skipProcessing (options: {
100 newSize: { width: number, height: number }
105 const { sourceImage, newSize, imageBytes, inputExt, outputExt } = options
106 const { width, height } = newSize
108 if (sourceImage.getWidth() > width || sourceImage.getHeight() > height) return false
109 if (inputExt !== outputExt) return false
113 if (height >= 1000) return imageBytes <= 200 * kB
114 if (height >= 500) return imageBytes <= 100 * kB
116 return imageBytes <= 15 * kB