nfp_sites/api/media/resize.mjs

66 lines
1.5 KiB
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
})
.jpeg({
quality: 90,
})
2019-02-20 16:10:37 +00:00
.toFile(output)
.then(() => output)
}
createMedium(input, height) {
2019-02-20 16:10:37 +00:00
let output = this.Media.getSubUrl(input, 'medium')
return this.sharp(input)
.resize(700, height || 700, {
fit: height && sharp.fit.cover || sharp.fit.inside,
2019-09-14 19:03:38 +00:00
withoutEnlargement: true,
2019-09-13 13:33:10 +00:00
})
.jpeg({
quality: 90,
})
.toFile(output)
.then(() => output)
}
createLarge(input) {
let output = this.Media.getSubUrl(input, 'large')
return this.sharp(input)
.resize(1280, 1280, {
fit: sharp.fit.inside,
withoutEnlargement: true,
})
.jpeg({
quality: 90,
})
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)
}
}