]>
Commit | Line | Data |
---|---|---|
1664bc60 | 1 | import { copy, readFile, remove, rename } from 'fs-extra' |
41fb13c3 | 2 | import Jimp, { read } from 'jimp' |
c55e3d72 | 3 | import { buildUUID, getLowercaseExtension } from '@shared/core-utils' |
123f6193 | 4 | import { convertWebPToJPG, processGIF } from './ffmpeg-utils' |
74577825 | 5 | import { logger } from './logger' |
18782242 | 6 | |
84531547 | 7 | function generateImageFilename (extension = '.jpg') { |
d4a8e7a6 | 8 | return buildUUID() + extension |
84531547 C |
9 | } |
10 | ||
ac81d1a0 | 11 | async function processImage ( |
2fb5b3a5 | 12 | path: string, |
ac81d1a0 | 13 | destination: string, |
e8bafea3 C |
14 | newSize: { width: number, height: number }, |
15 | keepOriginal = false | |
ac81d1a0 | 16 | ) { |
ea54cd04 | 17 | const extension = getLowercaseExtension(path) |
123f6193 | 18 | |
f619de0e C |
19 | if (path === destination) { |
20 | throw new Error('Jimp/FFmpeg needs an input path different that the output path.') | |
21 | } | |
22 | ||
23 | logger.debug('Processing image %s to %s.', path, destination) | |
24 | ||
123f6193 K |
25 | // Use FFmpeg to process GIF |
26 | if (extension === '.gif') { | |
f619de0e C |
27 | await processGIF(path, destination, newSize) |
28 | } else { | |
1664bc60 | 29 | await jimpProcessor(path, destination, newSize, extension) |
123f6193 K |
30 | } |
31 | ||
f619de0e C |
32 | if (keepOriginal !== true) await remove(path) |
33 | } | |
a8a63227 | 34 | |
f619de0e | 35 | // --------------------------------------------------------------------------- |
a8a63227 | 36 | |
f619de0e | 37 | export { |
84531547 | 38 | generateImageFilename, |
f619de0e C |
39 | processImage |
40 | } | |
41 | ||
42 | // --------------------------------------------------------------------------- | |
43 | ||
1664bc60 | 44 | async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }, inputExt: string) { |
7196a70b | 45 | let sourceImage: Jimp |
1664bc60 | 46 | const inputBuffer = await readFile(path) |
18782242 C |
47 | |
48 | try { | |
7196a70b | 49 | sourceImage = await read(inputBuffer) |
18782242 | 50 | } catch (err) { |
b5b68755 | 51 | logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err }) |
18782242 C |
52 | |
53 | const newName = path + '.jpg' | |
54 | await convertWebPToJPG(path, newName) | |
55 | await rename(newName, path) | |
56 | ||
7196a70b | 57 | sourceImage = await read(path) |
18782242 | 58 | } |
a8a63227 C |
59 | |
60 | await remove(destination) | |
61 | ||
1664bc60 | 62 | // Optimization if the source file has the appropriate size |
ea54cd04 | 63 | const outputExt = getLowercaseExtension(destination) |
7196a70b | 64 | if (skipProcessing({ sourceImage, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt })) { |
1664bc60 C |
65 | return copy(path, destination) |
66 | } | |
67 | ||
7196a70b C |
68 | await autoResize({ sourceImage, newSize, destination }) |
69 | } | |
70 | ||
71 | async function autoResize (options: { | |
72 | sourceImage: Jimp | |
73 | newSize: { width: number, height: number } | |
74 | destination: string | |
75 | }) { | |
76 | const { sourceImage, newSize, destination } = options | |
77 | ||
9c7cf007 C |
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 | |
81 | ||
82 | if (sourceIsPortrait && !destIsPortraitOrSquare) { | |
7196a70b C |
83 | const baseImage = sourceImage.cloneQuiet().cover(newSize.width, newSize.height) |
84 | .color([ { apply: 'shade', params: [ 50 ] } ]) | |
85 | ||
86 | const topImage = sourceImage.cloneQuiet().contain(newSize.width, newSize.height) | |
87 | ||
88 | return write(baseImage.blit(topImage, 0, 0), destination) | |
89 | } | |
90 | ||
9c7cf007 | 91 | return write(sourceImage.cover(newSize.width, newSize.height), destination) |
7196a70b C |
92 | } |
93 | ||
94 | function write (image: Jimp, destination: string) { | |
95 | return image.quality(80).writeAsync(destination) | |
ac81d1a0 | 96 | } |
1664bc60 C |
97 | |
98 | function skipProcessing (options: { | |
7196a70b | 99 | sourceImage: Jimp |
1664bc60 C |
100 | newSize: { width: number, height: number } |
101 | imageBytes: number | |
102 | inputExt: string | |
103 | outputExt: string | |
104 | }) { | |
7196a70b | 105 | const { sourceImage, newSize, imageBytes, inputExt, outputExt } = options |
1664bc60 C |
106 | const { width, height } = newSize |
107 | ||
7196a70b | 108 | if (sourceImage.getWidth() > width || sourceImage.getHeight() > height) return false |
1664bc60 C |
109 | if (inputExt !== outputExt) return false |
110 | ||
111 | const kB = 1000 | |
112 | ||
113 | if (height >= 1000) return imageBytes <= 200 * kB | |
114 | if (height >= 500) return imageBytes <= 100 * kB | |
115 | ||
116 | return imageBytes <= 15 * kB | |
117 | } |