module

Hwaro::Content::Processors::ImageProcessor

Constants

IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".bmp"}

Only formats that stb can both read AND write back correctly.

MAX_PIXELS = 64000000_i64

Maximum pixel count to prevent excessive memory allocation (64 megapixels)

Instance methods

dominant_color(pixels : Pointer(UInt8), w : Int32, h : Int32, channels : Int32) : String

Compute dominant color as a hex string (e.g., "#a3b2c1"). Channels: 1=gray, 2=gray+alpha, 3=RGB, 4=RGBA

Source
generate_lqip(pixels : Pointer(UInt8), src_w : Int32, src_h : Int32, channels : Int32, lqip_width : Int32 = 32, quality : Int32 = 20) : String | Nil

Generate a low-quality image placeholder as a base64 data URI. Operates on already-decoded pixel data (avoids a second stbi_load).

Source
generate_lqip_with_color(pixels : Pointer(UInt8), src_w : Int32, src_h : Int32, channels : Int32, lqip_width : Int32 = 32, quality : Int32 = 20) : Tuple(String, String) | Nil

Generate LQIP data URI and dominant color from the thumbnail in one pass. Returns {data_uri, dominant_color_hex} or nil on failure.

Source
image?(path : String) : Bool

Check if a file is a resizable image based on extension

Source
process_configured_widths(source_path : String, output_base : String, url_prefix : String, widths : Array(Int32), quality : Int32 = 85) : Array(Tuple(String, Int32, String))

Process all images for configured widths. Returns array of {original_url, width, resized_url} tuples.

Source
resize(source : String, dest : String, width : Int32, height : Int32 = 0, quality : Int32 = 85) : String | Nil

Resize a single image to the given width, preserving aspect ratio. Returns the output path on success, nil on failure.

Source
resize_and_lqip(source : String, dest_dir : String, widths : Array(Int32), quality : Int32 = 85, lqip_width : Int32 = 32, lqip_quality : Int32 = 20) : Tuple(Hash(Int32, String), String | Nil, String)

Combined resize + LQIP in a single decode pass. Returns {width_map, lqip_data_uri_or_nil, dominant_color_hex}

Source
resize_multi_widths(source : String, dest_dir : String, widths : Array(Int32), quality : Int32 = 85) : Hash(Int32, String)

Resize a single source image to multiple widths with one decode pass. Returns a Hash mapping width => dest_path for successful resizes.

Source
resized_filename(original : String, width : Int32) : String

Generate a resized filename: photo.jpg -> photo_800w.jpg

Source