module Clacky::Utils::FileProcessor

def self.downscale_image_base64(b64, mime_type, max_width: IMAGE_MAX_WIDTH)

Returns:
  • (String) - base64-encoded (possibly downscaled) image data

Parameters:
  • max_width (Integer) -- maximum output width in pixels (default: IMAGE_MAX_WIDTH)
  • mime_type (String) -- e.g. "image/png", "image/jpeg", "image/webp"
  • b64 (String) -- base64-encoded image data
def self.downscale_image_base64(b64, mime_type, max_width: IMAGE_MAX_WIDTH)
  require "base64"
  result = if mime_type == "image/png"
             downscale_png_chunky(b64, max_width)
           else
             downscale_via_cli(b64, mime_type, max_width)
           end
  return result if result
  # No resize tool available — enforce API hard size limit (5MB)
  if b64.bytesize > IMAGE_MAX_BASE64_BYTES
    size_kb = b64.bytesize / 1024
    limit_mb = IMAGE_MAX_BASE64_BYTES / 1_000_000
    raise ArgumentError,
      "Image too large to send (#{size_kb}KB > #{limit_mb}MB). " \
      "Install ImageMagick (`brew install imagemagick`) to enable automatic resizing."
  end
  b64
end