PhoenixKit.Storage.ImageProcessor (phoenix_kit v1.5.1)

View Source

ImageMagick-based image processing module.

Handles image operations using ImageMagick command-line tools:

  • identify - Extract image metadata (dimensions, format)
  • convert/magick - Resize and format conversion

This replaces Vix with ImageMagick, which is more widely trusted and has better long-term support.

Summary

Functions

Extract both width and height from an image file.

Get the height of an image file using ImageMagick identify.

Get the width of an image file using ImageMagick identify.

Resize an image to fit within specified dimensions.

Resize and center-crop an image to exact dimensions.

Functions

extract_dimensions(file_path)

Extract both width and height from an image file.

Uses ImageMagick's identify command to extract image dimensions.

Returns:

  • {:ok, {width, height}} - Dimensions in pixels
  • {:error, reason} - If extraction fails

get_height(file_path)

Get the height of an image file using ImageMagick identify.

Returns the height in pixels or nil if extraction fails.

get_width(file_path)

Get the width of an image file using ImageMagick identify.

Returns the width in pixels or nil if extraction fails.

resize(input_path, output_path, width, height, opts \\ [])

Resize an image to fit within specified dimensions.

Maintains aspect ratio by scaling to fit within bounds. Optionally converts format based on output_format parameter.

Parameters:

  • input_path - Path to input image file
  • output_path - Path to save resized image
  • width - Target width (nil to use original)
  • height - Target height (nil to use original)
  • opts - Additional options
    • :quality - JPEG quality 1-100 (default: 85)
    • :format - Output format override (jpg, png, webp, etc)

Returns:

  • {:ok, output_path} - Success
  • {:error, reason} - If resize fails

resize_and_crop_center(input_path, output_path, width, height, opts \\ [])

Resize and center-crop an image to exact dimensions.

Zooms into the image to fill the target dimensions completely, then center-crops to extract the exact target size. No padding borders - the entire output is filled with the image content.

This is ideal for thumbnails where you want perfect squares (e.g., 150x150) with the image zoomed in and centered, no white/black borders.

The algorithm:

  1. Resizes image to fill the target box (scales to cover both dimensions)
  2. Centers the image using gravity
  3. Crops from center to exact target dimensions

Parameters:

  • input_path - Path to input image file
  • output_path - Path to save cropped image
  • width - Target width (required)
  • height - Target height (required)
  • opts - Additional options
    • :quality - JPEG quality 1-100 (default: 85)
    • :format - Output format override (jpg, png, webp, etc)
    • :background - Background color (rarely used, default: "white")

Returns:

  • {:ok, output_path} - Success
  • {:error, reason} - If processing fails