]>
Commit | Line | Data |
---|---|---|
1664bc60 C |
1 | import { copy, readFile, remove, rename } from 'fs-extra' |
2 | import * as Jimp from 'jimp' | |
84531547 | 3 | import { v4 as uuidv4 } from 'uuid' |
ea54cd04 | 4 | import { getLowercaseExtension } from './core-utils' |
123f6193 | 5 | import { convertWebPToJPG, processGIF } from './ffmpeg-utils' |
74577825 | 6 | import { logger } from './logger' |
18782242 | 7 | |
84531547 C |
8 | function generateImageFilename (extension = '.jpg') { |
9 | return uuidv4() + extension | |
10 | } | |
11 | ||
ac81d1a0 | 12 | async function processImage ( |
2fb5b3a5 | 13 | path: string, |
ac81d1a0 | 14 | destination: string, |
e8bafea3 C |
15 | newSize: { width: number, height: number }, |
16 | keepOriginal = false | |
ac81d1a0 | 17 | ) { |
ea54cd04 | 18 | const extension = getLowercaseExtension(path) |
123f6193 | 19 | |
f619de0e C |
20 | if (path === destination) { |
21 | throw new Error('Jimp/FFmpeg needs an input path different that the output path.') | |
22 | } | |
23 | ||
24 | logger.debug('Processing image %s to %s.', path, destination) | |
25 | ||
123f6193 K |
26 | // Use FFmpeg to process GIF |
27 | if (extension === '.gif') { | |
f619de0e C |
28 | await processGIF(path, destination, newSize) |
29 | } else { | |
1664bc60 | 30 | await jimpProcessor(path, destination, newSize, extension) |
123f6193 K |
31 | } |
32 | ||
f619de0e C |
33 | if (keepOriginal !== true) await remove(path) |
34 | } | |
a8a63227 | 35 | |
f619de0e | 36 | // --------------------------------------------------------------------------- |
a8a63227 | 37 | |
f619de0e | 38 | export { |
84531547 | 39 | generateImageFilename, |
f619de0e C |
40 | processImage |
41 | } | |
42 | ||
43 | // --------------------------------------------------------------------------- | |
44 | ||
1664bc60 C |
45 | async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }, inputExt: string) { |
46 | let jimpInstance: Jimp | |
47 | const inputBuffer = await readFile(path) | |
18782242 C |
48 | |
49 | try { | |
1664bc60 | 50 | jimpInstance = await Jimp.read(inputBuffer) |
18782242 | 51 | } catch (err) { |
b5b68755 | 52 | logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err }) |
18782242 C |
53 | |
54 | const newName = path + '.jpg' | |
55 | await convertWebPToJPG(path, newName) | |
56 | await rename(newName, path) | |
57 | ||
58 | jimpInstance = await Jimp.read(path) | |
59 | } | |
a8a63227 C |
60 | |
61 | await remove(destination) | |
62 | ||
1664bc60 | 63 | // Optimization if the source file has the appropriate size |
ea54cd04 C |
64 | const outputExt = getLowercaseExtension(destination) |
65 | if (skipProcessing({ jimpInstance, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt })) { | |
1664bc60 C |
66 | return copy(path, destination) |
67 | } | |
68 | ||
e6dfa586 | 69 | await jimpInstance |
ac81d1a0 | 70 | .resize(newSize.width, newSize.height) |
e6dfa586 RK |
71 | .quality(80) |
72 | .writeAsync(destination) | |
ac81d1a0 | 73 | } |
1664bc60 C |
74 | |
75 | function skipProcessing (options: { | |
76 | jimpInstance: Jimp | |
77 | newSize: { width: number, height: number } | |
78 | imageBytes: number | |
79 | inputExt: string | |
80 | outputExt: string | |
81 | }) { | |
82 | const { jimpInstance, newSize, imageBytes, inputExt, outputExt } = options | |
83 | const { width, height } = newSize | |
84 | ||
85 | if (jimpInstance.getWidth() > width || jimpInstance.getHeight() > height) return false | |
86 | if (inputExt !== outputExt) return false | |
87 | ||
88 | const kB = 1000 | |
89 | ||
90 | if (height >= 1000) return imageBytes <= 200 * kB | |
91 | if (height >= 500) return imageBytes <= 100 * kB | |
92 | ||
93 | return imageBytes <= 15 * kB | |
94 | } |