nfp_sites/api/media/resize.mjs

45 lines
993 B
JavaScript
Raw Normal View History

2019-02-20 16:10:37 +00:00
import sharp from 'sharp'
2019-09-14 19:03:38 +00:00
import Media from './model.mjs'
2019-02-20 16:10:37 +00:00
export default class Resizer {
constructor(opts = {}) {
Object.assign(this, {
Media: opts.Media || Media,
sharp: opts.sharp || sharp,
})
}
createSmall(input) {
let output = this.Media.getSubUrl(input, 'small')
return this.sharp(input)
2019-09-13 13:33:10 +00:00
.resize(360, 360, {
fit: sharp.fit.inside,
2019-09-14 19:03:38 +00:00
withoutEnlargement: true,
2019-09-13 13:33:10 +00:00
})
2019-02-20 16:10:37 +00:00
.toFile(output)
.then(() => output)
}
createMedium(input) {
let output = this.Media.getSubUrl(input, 'medium')
return this.sharp(input)
2019-09-13 13:33:10 +00:00
.resize(700, 700, {
fit: sharp.fit.inside,
2019-09-14 19:03:38 +00:00
withoutEnlargement: true,
2019-09-13 13:33:10 +00:00
})
2019-02-20 16:10:37 +00:00
.toFile(output)
.then(() => output)
}
autoRotate(input) {
const output = `${input}_2.jpg`
return this.sharp(input)
.rotate()
.toFile(output)
.then(() => output)
}
}