]>
Commit | Line | Data |
---|---|---|
1664bc60 | 1 | import { copy, readFile, remove, rename } from 'fs-extra' |
7bde6250 | 2 | import Jimp, { read as jimpRead } from 'jimp' |
c729caf6 | 3 | import { join } from 'path' |
0628157f C |
4 | import { getLowercaseExtension } from '@shared/core-utils' |
5 | import { buildUUID } from '@shared/extra-utils' | |
0c9668f7 | 6 | import { convertWebPToJPG, generateThumbnailFromVideo, processGIF } from './ffmpeg' |
c729caf6 C |
7 | import { logger, loggerTagsFactory } from './logger' |
8 | ||
9 | const lTags = loggerTagsFactory('image-utils') | |
18782242 | 10 | |
84531547 | 11 | function generateImageFilename (extension = '.jpg') { |
d4a8e7a6 | 12 | return buildUUID() + extension |
84531547 C |
13 | } |
14 | ||
3a54605d C |
15 | async function processImage (options: { |
16 | path: string | |
17 | destination: string | |
18 | newSize: { width: number, height: number } | |
19 | keepOriginal?: boolean // default false | |
20 | }) { | |
21 | const { path, destination, newSize, keepOriginal = false } = options | |
22 | ||
ea54cd04 | 23 | const extension = getLowercaseExtension(path) |
123f6193 | 24 | |
f619de0e C |
25 | if (path === destination) { |
26 | throw new Error('Jimp/FFmpeg needs an input path different that the output path.') | |
27 | } | |
28 | ||
29 | logger.debug('Processing image %s to %s.', path, destination) | |
30 | ||
123f6193 K |
31 | // Use FFmpeg to process GIF |
32 | if (extension === '.gif') { | |
0c9668f7 | 33 | await processGIF({ path, destination, newSize }) |
f619de0e | 34 | } else { |
1664bc60 | 35 | await jimpProcessor(path, destination, newSize, extension) |
123f6193 K |
36 | } |
37 | ||
f619de0e C |
38 | if (keepOriginal !== true) await remove(path) |
39 | } | |
a8a63227 | 40 | |
3a54605d C |
41 | async function generateImageFromVideoFile (options: { |
42 | fromPath: string | |
43 | folder: string | |
44 | imageName: string | |
45 | size: { width: number, height: number } | |
46 | }) { | |
47 | const { fromPath, folder, imageName, size } = options | |
48 | ||
c729caf6 C |
49 | const pendingImageName = 'pending-' + imageName |
50 | const pendingImagePath = join(folder, pendingImageName) | |
51 | ||
52 | try { | |
0c9668f7 | 53 | await generateThumbnailFromVideo({ fromPath, folder, imageName }) |
c729caf6 C |
54 | |
55 | const destination = join(folder, imageName) | |
3a54605d | 56 | await processImage({ path: pendingImagePath, destination, newSize: size }) |
c729caf6 C |
57 | } catch (err) { |
58 | logger.error('Cannot generate image from video %s.', fromPath, { err, ...lTags() }) | |
59 | ||
60 | try { | |
61 | await remove(pendingImagePath) | |
62 | } catch (err) { | |
63 | logger.debug('Cannot remove pending image path after generation error.', { err, ...lTags() }) | |
64 | } | |
65 | } | |
66 | } | |
67 | ||
7bde6250 C |
68 | async function getImageSize (path: string) { |
69 | const inputBuffer = await readFile(path) | |
70 | ||
71 | const image = await jimpRead(inputBuffer) | |
72 | ||
73 | return { | |
74 | width: image.getWidth(), | |
75 | height: image.getHeight() | |
76 | } | |
77 | } | |
78 | ||
f619de0e | 79 | // --------------------------------------------------------------------------- |
a8a63227 | 80 | |
f619de0e | 81 | export { |
84531547 | 82 | generateImageFilename, |
c729caf6 | 83 | generateImageFromVideoFile, |
7bde6250 C |
84 | |
85 | processImage, | |
86 | ||
87 | getImageSize | |
f619de0e C |
88 | } |
89 | ||
90 | // --------------------------------------------------------------------------- | |
91 | ||
1664bc60 | 92 | async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }, inputExt: string) { |
7196a70b | 93 | let sourceImage: Jimp |
1664bc60 | 94 | const inputBuffer = await readFile(path) |
18782242 C |
95 | |
96 | try { | |
7bde6250 | 97 | sourceImage = await jimpRead(inputBuffer) |
18782242 | 98 | } catch (err) { |
b5b68755 | 99 | logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err }) |
18782242 C |
100 | |
101 | const newName = path + '.jpg' | |
0c9668f7 | 102 | await convertWebPToJPG({ path, destination: newName }) |
18782242 C |
103 | await rename(newName, path) |
104 | ||
7bde6250 | 105 | sourceImage = await jimpRead(path) |
18782242 | 106 | } |
a8a63227 C |
107 | |
108 | await remove(destination) | |
109 | ||
1664bc60 | 110 | // Optimization if the source file has the appropriate size |
ea54cd04 | 111 | const outputExt = getLowercaseExtension(destination) |
7196a70b | 112 | if (skipProcessing({ sourceImage, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt })) { |
1664bc60 C |
113 | return copy(path, destination) |
114 | } | |
115 | ||
7196a70b C |
116 | await autoResize({ sourceImage, newSize, destination }) |
117 | } | |
118 | ||
119 | async function autoResize (options: { | |
120 | sourceImage: Jimp | |
121 | newSize: { width: number, height: number } | |
122 | destination: string | |
123 | }) { | |
124 | const { sourceImage, newSize, destination } = options | |
125 | ||
7a4fd56c | 126 | // Portrait mode targeting a landscape, apply some effect on the image |
9c7cf007 C |
127 | const sourceIsPortrait = sourceImage.getWidth() < sourceImage.getHeight() |
128 | const destIsPortraitOrSquare = newSize.width <= newSize.height | |
129 | ||
0c058f25 C |
130 | removeExif(sourceImage) |
131 | ||
9c7cf007 | 132 | if (sourceIsPortrait && !destIsPortraitOrSquare) { |
7196a70b C |
133 | const baseImage = sourceImage.cloneQuiet().cover(newSize.width, newSize.height) |
134 | .color([ { apply: 'shade', params: [ 50 ] } ]) | |
135 | ||
136 | const topImage = sourceImage.cloneQuiet().contain(newSize.width, newSize.height) | |
137 | ||
138 | return write(baseImage.blit(topImage, 0, 0), destination) | |
139 | } | |
140 | ||
9c7cf007 | 141 | return write(sourceImage.cover(newSize.width, newSize.height), destination) |
7196a70b C |
142 | } |
143 | ||
144 | function write (image: Jimp, destination: string) { | |
145 | return image.quality(80).writeAsync(destination) | |
ac81d1a0 | 146 | } |
1664bc60 C |
147 | |
148 | function skipProcessing (options: { | |
7196a70b | 149 | sourceImage: Jimp |
1664bc60 C |
150 | newSize: { width: number, height: number } |
151 | imageBytes: number | |
152 | inputExt: string | |
153 | outputExt: string | |
154 | }) { | |
7196a70b | 155 | const { sourceImage, newSize, imageBytes, inputExt, outputExt } = options |
1664bc60 C |
156 | const { width, height } = newSize |
157 | ||
0c058f25 | 158 | if (hasExif(sourceImage)) return false |
7196a70b | 159 | if (sourceImage.getWidth() > width || sourceImage.getHeight() > height) return false |
1664bc60 C |
160 | if (inputExt !== outputExt) return false |
161 | ||
162 | const kB = 1000 | |
163 | ||
164 | if (height >= 1000) return imageBytes <= 200 * kB | |
165 | if (height >= 500) return imageBytes <= 100 * kB | |
166 | ||
167 | return imageBytes <= 15 * kB | |
168 | } | |
0c058f25 C |
169 | |
170 | function hasExif (image: Jimp) { | |
171 | return !!(image.bitmap as any).exifBuffer | |
172 | } | |
173 | ||
174 | function removeExif (image: Jimp) { | |
175 | (image.bitmap as any).exifBuffer = null | |
176 | } |