PhoenixKit.Modules.Storage.VariantGenerator (phoenix_kit v1.7.168)

Copy Markdown View Source

Variant generation system for images and videos.

This module handles the creation of different variants (thumbnails, resizes, quality adjustments) for uploaded files based on dimension configurations.

Supported Operations

Images

  • Resize to specific dimensions
  • Generate thumbnails (square crops)
  • Quality adjustments
  • Format conversion (JPEG, PNG, WebP)

Videos

  • Quality variants (360p, 720p, 1080p)
  • Thumbnail extraction
  • Format conversion (MP4)

Dependencies

Requires external tools to be installed:

  • Images: ImageMagick (convert and identify commands)
  • Videos: FFmpeg

Summary

Functions

Generates a specific variant for a file.

Generates variants for a file based on enabled dimensions.

Stores an already-rendered variant file as variant_name on file.

Functions

generate_variant(file, dimension)

Generates a specific variant for a file.

Parameters

  • file - The file struct
  • dimension - The dimension configuration

Returns

  • {:ok, file_instance} - Generated variant
  • {:error, reason} - Error if generation fails

generate_variants(file, opts \\ [])

Generates variants for a file based on enabled dimensions.

Parameters

  • file - The file struct to generate variants for
  • opts - Options for variant generation

Options

  • :dimensions - List of specific dimensions to generate (default: all enabled)

Variant generation is always synchronous from the caller's perspective. Internally process_variants/2 parallelizes the per-dimension work via Task.await_many/2 with a 10-minute cap. Callers that need fire-and-forget semantics should enqueue the work via Oban (see process_file_job.ex).

Returns

  • {:ok, variants} - List of generated file instances
  • {:error, reason} - Error if generation fails

store_prepared_variant(file, variant_name, prepared_path, variant_ext, variant_mime_type)

Stores an already-rendered variant file as variant_name on file.

Use this when the variant bytes are produced outside the normal resize pipeline (e.g. the baked annotated thumbnail). Handles stats, bucket storage, the FileInstance row, and file-location records — then removes prepared_path.

Returns {:ok, instance} or {:error, reason}.