On this page

Image field

The file field stores a relationship to an image record in the uploads collection.

Image field

Examples

Here's an example of using the image field in a block:

# blocks/Product.vue

<template>
  <div>
    <PruviousPicture :image="image" :lazy="true" />
    ...
  </div>
</template>

<script lang="ts" setup>
import { imageField } from '#pruvious'

defineProps({
  image: imageField({
    minWidth: 1600,
    minHeight: 1600,
    sources: [
      { media: '(max-width: 768px)', format: 'webp', width: 1024, height: 1024, resize: 'cover' },
      { media: '(max-width: 768px)', format: 'jpeg', width: 1024, height: 1024, resize: 'cover' },
      { format: 'webp', width: 1600, height: 1600, resize: 'cover' },
      { format: 'jpeg', width: 1600, height: 1600, resize: 'cover' },
    ],
  }),
})
</script>

Here is another example of how to use it in a collection definition:

# collections/products.ts

import { defineCollection } from '#pruvious'

export default defineCollection({
  name: 'products',
  mode: 'multi',
  fields: {
    image: {
      type: 'image',
      options: {
        minWidth: 1600,
        minHeight: 1600,
        sources: [
          { media: '(max-width: 768px)', format: 'webp', width: 1024, height: 1024, resize: 'cover' },
          { media: '(max-width: 768px)', format: 'jpeg', width: 1024, height: 1024, resize: 'cover' },
          { format: 'webp', width: 1600, height: 1600, resize: 'cover' },
          { format: 'jpeg', width: 1600, height: 1600, resize: 'cover' },
        ],
      },
    // ...
  },
})

Options

You can specify the following options in the image field:

OptionDescription

allowedTypes

An array of allowed image types or extensions (e.g., ['image/jpeg', '.png', 'SVG']). By default, it includes all standard image types.

default

The default field value (null if not defined).

description

A brief descriptive text displayed in code comments and in a tooltip at the upper right corner of the field.

label

The field label displayed in the UI. By default, it is automatically generated based on the property name assigned to the field. For example, productImage becomes Product image.

minHeight

The minimum allowed image height in pixels.

minWidth

The minimum allowed image width in pixels.

name

A string that specifies the name for the input control. If not specified, the name attribute will be automatically generated.

required

Specifies that the field input is mandatory during creation.

sources

An array of optimized image sources. When this field is populated, the resulting object will include a sources property. This property is an array of objects, each with the following properties:

  • srcset - The URL or absolute path of the image source.

  • width - The width of the image source in pixels.

  • height - The height of the image source in pixels.

  • type - The MIME type of the image source.

  • media - The media query of the image source or null if not specified.

transformSvgs

Specifies whether to transform SVG images. If false, the sources option is disregarded. Defaults to false.

Last updated on January 7, 2024 at 17:44