Skip to content

Image converter

Variants images are generates by sharp module and require installation:

sh
npm install sharp
sh
pnpm install sharp
sh
yarn add sharp

Configuration

typescript
// config/attachment.ts
export default defineConfig({
  converters: [
    { 
      key: 'large',
      converter: () => import('@jrmc/adonis-attachment/converters/image_converter'), 
      options: {
        resize: 1280,
      }
    }
  ]
})

Format

The default format is webp, for change, use options format:

typescript
export default defineConfig({
  converters: [
    { 
      key: 'thumbnail',
      converter: () => import('@jrmc/adonis-attachment/converters/image_converter'),
      options: {
        resize: 300,
        format: 'jpeg', 
      }
    }
  ]
})

Options format is string or object [ format, options ] details in documentation : sharp api outpout

Sample for personalize image quality:

typescript
export default defineConfig({
  converters: [
    { 
      key: 'thumbnail',
      converter: () => import('@jrmc/adonis-attachment/converters/image_converter'),
      options: {
        resize: 300,
        format: {
          format: 'jpeg',
          options: {
            quality: 80
          }
        }
      }
    }
  ]
})

ReSize

Options resize is number or object(options) details in documentation : sharp api resize

Sample:

typescript
import { defineConfig } from '@jrmc/adonis-attachment'
import sharp from 'sharp'

export default defineConfig({
  converters: [
    {
      key: 'thumbnail',
      converter: () => import('@jrmc/adonis-attachment/converters/image_converter'),
      options: {
        format: 'jpeg',
        resize: { // https://sharp.pixelplumbing.com/api-resize
          width: 400,
          height: 400,
          fit: sharp.fit.cover,
          position: 'top'
        },
      }
    }
  ]
})