]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/image-utils.ts
Updated notification types in openapi reference
[github/Chocobozzz/PeerTube.git] / server / helpers / image-utils.ts
1 import { copy, readFile, remove, rename } from 'fs-extra'
2 import Jimp, { read } from 'jimp'
3 import { buildUUID, getLowercaseExtension } from '@shared/core-utils'
4 import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
5 import { logger } from './logger'
6
7 function generateImageFilename (extension = '.jpg') {
8 return buildUUID() + extension
9 }
10
11 async function processImage (
12 path: string,
13 destination: string,
14 newSize: { width: number, height: number },
15 keepOriginal = false
16 ) {
17 const extension = getLowercaseExtension(path)
18
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
25 // Use FFmpeg to process GIF
26 if (extension === '.gif') {
27 await processGIF(path, destination, newSize)
28 } else {
29 await jimpProcessor(path, destination, newSize, extension)
30 }
31
32 if (keepOriginal !== true) await remove(path)
33 }
34
35 // ---------------------------------------------------------------------------
36
37 export {
38 generateImageFilename,
39 processImage
40 }
41
42 // ---------------------------------------------------------------------------
43
44 async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }, inputExt: string) {
45 let sourceImage: Jimp
46 const inputBuffer = await readFile(path)
47
48 try {
49 sourceImage = await read(inputBuffer)
50 } catch (err) {
51 logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err })
52
53 const newName = path + '.jpg'
54 await convertWebPToJPG(path, newName)
55 await rename(newName, path)
56
57 sourceImage = await read(path)
58 }
59
60 await remove(destination)
61
62 // Optimization if the source file has the appropriate size
63 const outputExt = getLowercaseExtension(destination)
64 if (skipProcessing({ sourceImage, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt })) {
65 return copy(path, destination)
66 }
67
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
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) {
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
91 return write(sourceImage.cover(newSize.width, newSize.height), destination)
92 }
93
94 function write (image: Jimp, destination: string) {
95 return image.quality(80).writeAsync(destination)
96 }
97
98 function skipProcessing (options: {
99 sourceImage: Jimp
100 newSize: { width: number, height: number }
101 imageBytes: number
102 inputExt: string
103 outputExt: string
104 }) {
105 const { sourceImage, newSize, imageBytes, inputExt, outputExt } = options
106 const { width, height } = newSize
107
108 if (sourceImage.getWidth() > width || sourceImage.getHeight() > height) return false
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 }