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