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