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