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