UNPKG

@jrmc/adonis-attachment

Version:

Turn any field on your Lucid model to an attachment data type

32 lines (31 loc) 942 B
/** * @jrmc/adonis-attachment * * @license MIT * @copyright Jeremy Chaufourier <jeremy@chaufourier.fr> */ import Converter from './converter.js'; import { use } from '../utils/helpers.js'; export default class ImageConverter extends Converter { async handle({ input, options }) { const sharp = await use('sharp'); const resize = options?.resize || {}; let format = options?.format || 'webp'; const autoOrient = options?.autoOrient ?? true; let formatoptions = {}; if (typeof format !== 'string') { formatoptions = format?.options; format = format.format; } const image = sharp(input) .withMetadata(); if (autoOrient) { image.autoOrient(); } const buffer = await image .resize(resize) .toFormat(format, formatoptions) .toBuffer(); return buffer; } }