]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/image-utils.ts
Fix thumbnails/previews for portrait videos
[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 { getLowercaseExtension } from './core-utils'
4 import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
5 import { logger } from './logger'
6 import { buildUUID } from './uuid'
7
8 function generateImageFilename (extension = '.jpg') {
9 return buildUUID() + extension
10 }
11
12 async function processImage (
13 path: string,
14 destination: string,
15 newSize: { width: number, height: number },
16 keepOriginal = false
17 ) {
18 const extension = getLowercaseExtension(path)
19
20 if (path === destination) {
21 throw new Error('Jimp/FFmpeg needs an input path different that the output path.')
22 }
23
24 logger.debug('Processing image %s to %s.', path, destination)
25
26 // Use FFmpeg to process GIF
27 if (extension === '.gif') {
28 await processGIF(path, destination, newSize)
29 } else {
30 await jimpProcessor(path, destination, newSize, extension)
31 }
32
33 if (keepOriginal !== true) await remove(path)
34 }
35
36 // ---------------------------------------------------------------------------
37
38 export {
39 generateImageFilename,
40 processImage
41 }
42
43 // ---------------------------------------------------------------------------
44
45 async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }, inputExt: string) {
46 let sourceImage: Jimp
47 const inputBuffer = await readFile(path)
48
49 try {
50 sourceImage = await read(inputBuffer)
51 } catch (err) {
52 logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err })
53
54 const newName = path + '.jpg'
55 await convertWebPToJPG(path, newName)
56 await rename(newName, path)
57
58 sourceImage = await read(path)
59 }
60
61 await remove(destination)
62
63 // Optimization if the source file has the appropriate size
64 const outputExt = getLowercaseExtension(destination)
65 if (skipProcessing({ sourceImage, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt })) {
66 return copy(path, destination)
67 }
68
69 await autoResize({ sourceImage, newSize, destination })
70 }
71
72 async function autoResize (options: {
73 sourceImage: Jimp
74 newSize: { width: number, height: number }
75 destination: string
76 }) {
77 const { sourceImage, newSize, destination } = options
78
79 // Portrait mode, special handling
80 if (sourceImage.getWidth() < sourceImage.getHeight()) {
81 const baseImage = sourceImage.cloneQuiet().cover(newSize.width, newSize.height)
82 .color([ { apply: 'shade', params: [ 50 ] } ])
83
84 const topImage = sourceImage.cloneQuiet().contain(newSize.width, newSize.height)
85
86 return write(baseImage.blit(topImage, 0, 0), destination)
87 }
88
89 return write(sourceImage.contain(newSize.width, newSize.height), destination)
90 }
91
92 function write (image: Jimp, destination: string) {
93 return image.quality(80).writeAsync(destination)
94 }
95
96 function skipProcessing (options: {
97 sourceImage: Jimp
98 newSize: { width: number, height: number }
99 imageBytes: number
100 inputExt: string
101 outputExt: string
102 }) {
103 const { sourceImage, newSize, imageBytes, inputExt, outputExt } = options
104 const { width, height } = newSize
105
106 if (sourceImage.getWidth() > width || sourceImage.getHeight() > height) return false
107 if (inputExt !== outputExt) return false
108
109 const kB = 1000
110
111 if (height >= 1000) return imageBytes <= 200 * kB
112 if (height >= 500) return imageBytes <= 100 * kB
113
114 return imageBytes <= 15 * kB
115 }