]>
Commit | Line | Data |
---|---|---|
1 | import 'multer' | |
2 | import { readFile, remove } from 'fs-extra' | |
3 | import { logger } from './logger' | |
4 | const Jimp = require('jimp') | |
5 | ||
6 | async function processImage ( | |
7 | path: string, | |
8 | destination: string, | |
9 | newSize: { width: number, height: number }, | |
10 | keepOriginal = false | |
11 | ) { | |
12 | if (path === destination) { | |
13 | throw new Error('Jimp needs an input path different that the output path.') | |
14 | } | |
15 | ||
16 | logger.debug('Processing image %s to %s.', path, destination) | |
17 | ||
18 | // Avoid sharp cache | |
19 | const buf = await readFile(path) | |
20 | const jimpInstance = await Jimp.read(buf) | |
21 | ||
22 | await remove(destination) | |
23 | ||
24 | await jimpInstance | |
25 | .resize(newSize.width, newSize.height) | |
26 | .quality(80) | |
27 | .writeAsync(destination) | |
28 | ||
29 | if (keepOriginal !== true) await remove(path) | |
30 | } | |
31 | ||
32 | // --------------------------------------------------------------------------- | |
33 | ||
34 | export { | |
35 | processImage | |
36 | } |