Nuxt Image

repository·main·Indexed 23 days ago

https://github.com/nuxt/image

A plug-and-play image optimization module for Nuxt applications that enables resizing, transforming, and serving optimized images. It provides <nuxt-img> and <nuxt-picture> components as drop-in replacements for native elements, supports over 20 image providers, and includes built-in resizing via unjs/ipx. Key features include automatic responsive size generation, support for modern formats like webp and avif, and customizable image presets.

Tokens
49.7K
Snippets
179
Records
259
Agent score
81%

What's inside @nuxt/image

  1. Overview of Nuxt Image features

    main

    Nuxt Image provides plug-and-play image optimization for Nuxt applications. It allows you to resize and transform images using a built-in optimizer or an external Image CDN.

    Key capabilities include:

    • Using <nuxt-img> as a drop-in replacement for the native <img> element.
    • Using <nuxt-picture> as a drop-in replacement for the native <picture> element.
    • Built-in resizing and transformation via unjs/ipx.
    • Support for over 20 different image providers.
    • Automatic generation of responsive sizes.
    • Optimization using modern formats like webp and avif.
  2. How `fit` values map to Imgproxy resizing

    main

    The fit modifier in Nuxt Image controls how Imgproxy handles resizing by mapping to resizingType and extend (for letterboxing).

    Dimension Rules:

    • Both width and height provided: Exact box-based behaviors (cover, contain, fill, outside) are fully applied.
    • One dimension provided: Falls back to proportional resizing.
    • Neither dimension provided: Defaults to proportional resizing.

    Supported fit Values

    fit valueBoth Dimensions ProvidedresizingTypeextend
    coverYesfill
    coverNofit
    containYesfittrue
    containNofit
    fillYesforce
    fillNofit
    insideAnyfit
    outsideYesfill
    outsideNofit

    Behavior Descriptions

    • cover: Preserves aspect ratio and ensures the image covers the target box. Parts may be cropped if both dimensions are set.
    • contain: Preserves aspect ratio and fits the image within the box. Applies padding (letterboxing) via extend: true if both dimensions are set.
    • fill: Ignores aspect ratio and stretches the image to match dimensions (if both are set).
    • inside: Resizes the image to be as large as possible while staying within the specified box (proportional).
    • outside: Approximates resizing so both dimensions are greater than or equal to the box (may crop, similar to cover).
  3. Configure Builder.io fit and position modifiers

    main

    The fit and position modifiers allow you to control how images are cropped and anchored.

    Important: Both fit and position only take effect if the format modifier is set to webp.

    Fit

    Determines how the image fits within the specified dimensions. Valid values:

    • cover (default)
    • contain
    • fill
    • inside
    • outside

    Position

    Determines the anchor point for cropping. Valid values:

    • top
    • right top
    • right
    • right bottom
    • bottom
    • left bottom
    • left
    • left top
    • center (default)

    Example usage:

    <NuxtImg src="..." width="300" height="300" modifiers="{ fit: 'contain', format: 'webp' }" />
    <NuxtImg src="..." width="300" height="300" modifiers="{ position: 'bottom left', format: 'webp' }" />
    <NuxtImg src="..." width="300" height="300" modifiers="{ fit: 'contain', format: 'webp' }" />
    
    <NuxtImg src="..." width="300" height="300" modifiers="{ position: 'bottom left', format: 'webp' }" />
  4. Map fit modes for the Sanity provider

    main

    Nuxt Image maps its standard fit options to specific Sanity modifiers. Note the distinction between fill and contain to ensure expected behavior:

    Nuxt Image fitSanity Modifier Equivalent
    covercrop
    containfill (defaults to white background)
    insidemin
    outsidemax
    fillscale

    Important: For compatibility with other providers, fit: fill uses Sanity's scale behavior. If you require Sanity's ?fit=fill behavior, use fit: contain instead.

  5. Use image presets

    main

    Presets allow you to define collections of pre-defined configurations (like modifiers) to unify image usage across your project. You can apply a preset to a component using the preset prop.

    export default defineNuxtConfig({
      image: {
        presets: {
          avatar: {
            modifiers: {
              format: 'jpg',
              width: 50,
              height: 50
            }
          }
        }
      }
    })
    <template>
      <NuxtImg
        preset="avatar"
        src="/nuxt-icon.png"
      />
    </template>
  6. Use Directus-specific modifiers

    main

    The modifiers object allows you to access Directus-specific features. There are two primary ways to use modifiers:

    1. Direct Transforms: Use withoutEnlargement to prevent upscaling and transforms to pass a pipeline of Sharp operations.
    2. Keyed Transforms: Use a key to reference a pre-configured Directus transform. Note: When using key, do not combine it with other modifiers.

    Example: Pipeline of transforms

    <NuxtImg
      provider="directus"
      :modifiers="{
        withoutEnlargement: true,
        transforms: [['blur', 4], ['negate']]
      }"
    />

    Example: Keyed transform

    <NuxtImg
      provider="directus"
      :modifiers="{
        key: 'system-large-cover'
      }"
    />
    NOTE

    Directus defaults ASSETS_TRANSFORM_MAX_OPERATIONS to 5. If you need more operations, it is recommended to use a keyed transform or update your Directus configuration.

  7. How Nuxt Image works with providers

    main

    Nuxt Image supports multiple providers to achieve high performance. A provider is an integration between Nuxt Image and a third-party image transformation service. Each provider is responsible for generating the correct URLs required by that specific service to perform image transformations.

    Nuxt Image can be configured to use a specific provider or can be configured to work with any external image transformation service.

  8. Use ImageKit `fit` parameters

    main

    ImageKit provides additional cropping and resizing modes via the fit property that extend the standard Nuxt Image options:

    • extract: The output image maintains the requested height and width while preserving the aspect ratio. It extracts a specific region from the original image.
    • pad_extract: Similar to extract, but if the requested dimension is larger than the original image, it adds solid colored padding to match the requested size.

    Refer to ImageKit's documentation for detailed crop mode behaviors.

  9. How named variants and flexible transformations work in Cloudflare Images

    main

    Cloudflare Images supports two delivery methods. The provider chooses between them based on the modifiers you provide:

    1. Named variants: Uses predefined sizes configured in your Cloudflare dashboard (e.g., public, thumbnail). This is triggered when you use the variant modifier or provide no image modifiers at all.
    2. Flexible variants: Uses on-the-fly transformations (width, height, fit, etc.). This requires flexible transformations to be enabled in your Cloudflare Images dashboard. This is triggered when you provide modifiers like width or height (and do not use a variant).
    <!-- Uses the 'public' variant (default when no modifiers) -->
    <NuxtImg provider="cloudflareimages" src="my-image-id" />
    
    <!-- Uses a named variant -->
    <NuxtImg
      provider="cloudflareimages"
      src="my-image-id"
      :modifiers="{ variant: 'thumbnail' }"
    />
    
    <!-- Uses flexible variants (requires enablement in dashboard) -->
    <NuxtImg
      provider="cloudflareimages"
      src="my-image-id"
      width="400"
      height="300"
      fit="cover"
    />