diff options
author | Chocobozzz <me@florianbigard.com> | 2021-02-16 10:19:09 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2021-02-16 10:36:44 +0100 |
commit | 1664bc60eb7aa3fa3792b6acff50f9bbabd3d877 (patch) | |
tree | 2434f642c2a7e895317bf791500dd181cd8f18c7 /server/helpers | |
parent | 374b725df52d941af1cf37cf211593340c05206c (diff) | |
download | PeerTube-1664bc60eb7aa3fa3792b6acff50f9bbabd3d877.tar.gz PeerTube-1664bc60eb7aa3fa3792b6acff50f9bbabd3d877.tar.zst PeerTube-1664bc60eb7aa3fa3792b6acff50f9bbabd3d877.zip |
Optimize remote image processing
Diffstat (limited to 'server/helpers')
-rw-r--r-- | server/helpers/image-utils.ts | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/server/helpers/image-utils.ts b/server/helpers/image-utils.ts index 3ebf07305..9285c12fc 100644 --- a/server/helpers/image-utils.ts +++ b/server/helpers/image-utils.ts | |||
@@ -1,10 +1,9 @@ | |||
1 | import { remove, rename } from 'fs-extra' | 1 | import { copy, readFile, remove, rename } from 'fs-extra' |
2 | import * as Jimp from 'jimp' | ||
2 | import { extname } from 'path' | 3 | import { extname } from 'path' |
3 | import { convertWebPToJPG, processGIF } from './ffmpeg-utils' | 4 | import { convertWebPToJPG, processGIF } from './ffmpeg-utils' |
4 | import { logger } from './logger' | 5 | import { logger } from './logger' |
5 | 6 | ||
6 | const Jimp = require('jimp') | ||
7 | |||
8 | async function processImage ( | 7 | async function processImage ( |
9 | path: string, | 8 | path: string, |
10 | destination: string, | 9 | destination: string, |
@@ -23,7 +22,7 @@ async function processImage ( | |||
23 | if (extension === '.gif') { | 22 | if (extension === '.gif') { |
24 | await processGIF(path, destination, newSize) | 23 | await processGIF(path, destination, newSize) |
25 | } else { | 24 | } else { |
26 | await jimpProcessor(path, destination, newSize) | 25 | await jimpProcessor(path, destination, newSize, extension) |
27 | } | 26 | } |
28 | 27 | ||
29 | if (keepOriginal !== true) await remove(path) | 28 | if (keepOriginal !== true) await remove(path) |
@@ -37,11 +36,12 @@ export { | |||
37 | 36 | ||
38 | // --------------------------------------------------------------------------- | 37 | // --------------------------------------------------------------------------- |
39 | 38 | ||
40 | async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }) { | 39 | async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }, inputExt: string) { |
41 | let jimpInstance: any | 40 | let jimpInstance: Jimp |
41 | const inputBuffer = await readFile(path) | ||
42 | 42 | ||
43 | try { | 43 | try { |
44 | jimpInstance = await Jimp.read(path) | 44 | jimpInstance = await Jimp.read(inputBuffer) |
45 | } catch (err) { | 45 | } catch (err) { |
46 | logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err }) | 46 | logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err }) |
47 | 47 | ||
@@ -54,8 +54,34 @@ async function jimpProcessor (path: string, destination: string, newSize: { widt | |||
54 | 54 | ||
55 | await remove(destination) | 55 | await remove(destination) |
56 | 56 | ||
57 | // Optimization if the source file has the appropriate size | ||
58 | if (await skipProcessing({ jimpInstance, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt: extname(destination) })) { | ||
59 | return copy(path, destination) | ||
60 | } | ||
61 | |||
57 | await jimpInstance | 62 | await jimpInstance |
58 | .resize(newSize.width, newSize.height) | 63 | .resize(newSize.width, newSize.height) |
59 | .quality(80) | 64 | .quality(80) |
60 | .writeAsync(destination) | 65 | .writeAsync(destination) |
61 | } | 66 | } |
67 | |||
68 | function skipProcessing (options: { | ||
69 | jimpInstance: Jimp | ||
70 | newSize: { width: number, height: number } | ||
71 | imageBytes: number | ||
72 | inputExt: string | ||
73 | outputExt: string | ||
74 | }) { | ||
75 | const { jimpInstance, newSize, imageBytes, inputExt, outputExt } = options | ||
76 | const { width, height } = newSize | ||
77 | |||
78 | if (jimpInstance.getWidth() > width || jimpInstance.getHeight() > height) return false | ||
79 | if (inputExt !== outputExt) return false | ||
80 | |||
81 | const kB = 1000 | ||
82 | |||
83 | if (height >= 1000) return imageBytes <= 200 * kB | ||
84 | if (height >= 500) return imageBytes <= 100 * kB | ||
85 | |||
86 | return imageBytes <= 15 * kB | ||
87 | } | ||