api/libs/resize.js

68 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-09-19 21:49:02 +02:00
const fs = require('fs')
const resizeImg = require('resize-img')
class Resize {
constructor (width, heigth) {
this.size = {
width: 128,
heigth: 128
}
if (width && heigth) {
this.setSize(width, heigth)
}
}
_createOutputFilename (input) {
return `${input}_${this.size.width}x${this.size.heigth}`
}
_resize (input, output, callback) {
fs.readFile(input, (err, data) => {
2019-03-12 23:00:37 +01:00
console.log('après readfile')
2018-09-19 21:49:02 +02:00
if (err) {
callback(err, null)
return false
}
2019-03-12 22:41:45 +01:00
resizeImg(data, this.size)
.then(buf => {
2019-03-12 23:00:37 +01:00
console.log('then rezise')
2019-03-12 22:41:45 +01:00
fs.writeFile(output, buf, (err) => {
2019-03-12 23:00:37 +01:00
console.log('write file')
2019-03-12 22:41:45 +01:00
if (err) {
callback(err, null)
return false
}
callback(null, { input: input, output: output, size: this.size })
})
2018-09-19 21:49:02 +02:00
})
2019-03-12 22:41:45 +01:00
.catch(callback)
2018-09-19 21:49:02 +02:00
})
}
setSize (width, heigth) {
this.size = {
width: width,
heigth: heigth
}
}
createThumbnail (file, callback) {
this.setSize(128, 128)
this._resize(file, this._createOutputFilename(file), callback)
}
createLargeImage (file, callback) {
2018-10-02 07:58:26 +02:00
this.setSize(1200, 900)
2018-09-19 21:49:02 +02:00
this._resize(file, this._createOutputFilename(file), callback)
}
resize (file, width, heigth, callback) {
this.setSize(width, heigth)
this._resize(file, this._createOutputFilename(file), callback)
}
}
module.exports = Resize