]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/image-utils.ts
Remove exif tags when processing images
[github/Chocobozzz/PeerTube.git] / server / helpers / image-utils.ts
CommitLineData
1664bc60 1import { copy, readFile, remove, rename } from 'fs-extra'
41fb13c3 2import Jimp, { read } from 'jimp'
0628157f
C
3import { getLowercaseExtension } from '@shared/core-utils'
4import { buildUUID } from '@shared/extra-utils'
123f6193 5import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
74577825 6import { logger } from './logger'
18782242 7
84531547 8function generateImageFilename (extension = '.jpg') {
d4a8e7a6 9 return buildUUID() + extension
84531547
C
10}
11
ac81d1a0 12async function processImage (
2fb5b3a5 13 path: string,
ac81d1a0 14 destination: string,
e8bafea3
C
15 newSize: { width: number, height: number },
16 keepOriginal = false
ac81d1a0 17) {
ea54cd04 18 const extension = getLowercaseExtension(path)
123f6193 19
f619de0e
C
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
123f6193
K
26 // Use FFmpeg to process GIF
27 if (extension === '.gif') {
f619de0e
C
28 await processGIF(path, destination, newSize)
29 } else {
1664bc60 30 await jimpProcessor(path, destination, newSize, extension)
123f6193
K
31 }
32
f619de0e
C
33 if (keepOriginal !== true) await remove(path)
34}
a8a63227 35
f619de0e 36// ---------------------------------------------------------------------------
a8a63227 37
f619de0e 38export {
84531547 39 generateImageFilename,
f619de0e
C
40 processImage
41}
42
43// ---------------------------------------------------------------------------
44
1664bc60 45async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }, inputExt: string) {
7196a70b 46 let sourceImage: Jimp
1664bc60 47 const inputBuffer = await readFile(path)
18782242
C
48
49 try {
7196a70b 50 sourceImage = await read(inputBuffer)
18782242 51 } catch (err) {
b5b68755 52 logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err })
18782242
C
53
54 const newName = path + '.jpg'
55 await convertWebPToJPG(path, newName)
56 await rename(newName, path)
57
7196a70b 58 sourceImage = await read(path)
18782242 59 }
a8a63227
C
60
61 await remove(destination)
62
1664bc60 63 // Optimization if the source file has the appropriate size
ea54cd04 64 const outputExt = getLowercaseExtension(destination)
7196a70b 65 if (skipProcessing({ sourceImage, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt })) {
1664bc60
C
66 return copy(path, destination)
67 }
68
7196a70b
C
69 await autoResize({ sourceImage, newSize, destination })
70}
71
72async function autoResize (options: {
73 sourceImage: Jimp
74 newSize: { width: number, height: number }
75 destination: string
76}) {
77 const { sourceImage, newSize, destination } = options
78
9c7cf007
C
79 // Portrait mode targetting a landscape, apply some effect on the image
80 const sourceIsPortrait = sourceImage.getWidth() < sourceImage.getHeight()
81 const destIsPortraitOrSquare = newSize.width <= newSize.height
82
0c058f25
C
83 removeExif(sourceImage)
84
9c7cf007 85 if (sourceIsPortrait && !destIsPortraitOrSquare) {
7196a70b
C
86 const baseImage = sourceImage.cloneQuiet().cover(newSize.width, newSize.height)
87 .color([ { apply: 'shade', params: [ 50 ] } ])
88
89 const topImage = sourceImage.cloneQuiet().contain(newSize.width, newSize.height)
90
91 return write(baseImage.blit(topImage, 0, 0), destination)
92 }
93
9c7cf007 94 return write(sourceImage.cover(newSize.width, newSize.height), destination)
7196a70b
C
95}
96
97function write (image: Jimp, destination: string) {
98 return image.quality(80).writeAsync(destination)
ac81d1a0 99}
1664bc60
C
100
101function skipProcessing (options: {
7196a70b 102 sourceImage: Jimp
1664bc60
C
103 newSize: { width: number, height: number }
104 imageBytes: number
105 inputExt: string
106 outputExt: string
107}) {
7196a70b 108 const { sourceImage, newSize, imageBytes, inputExt, outputExt } = options
1664bc60
C
109 const { width, height } = newSize
110
0c058f25 111 if (hasExif(sourceImage)) return false
7196a70b 112 if (sourceImage.getWidth() > width || sourceImage.getHeight() > height) return false
1664bc60
C
113 if (inputExt !== outputExt) return false
114
115 const kB = 1000
116
117 if (height >= 1000) return imageBytes <= 200 * kB
118 if (height >= 500) return imageBytes <= 100 * kB
119
120 return imageBytes <= 15 * kB
121}
0c058f25
C
122
123function hasExif (image: Jimp) {
124 return !!(image.bitmap as any).exifBuffer
125}
126
127function removeExif (image: Jimp) {
128 (image.bitmap as any).exifBuffer = null
129}