Oxipng (oxipng v0.1.0)

Copy Markdown View Source

Elixir bindings for oxipng, a PNG optimizer written in Rust.

Calls are synchronous and run via Rustler NIFs on dirty CPU schedulers. The calling process waits for the result while normal BEAM schedulers remain available.

Compression is lossless by default. scale_16: true reduces precision, and optimize_alpha: true can change the RGB values of fully transparent pixels. Stripping color metadata can affect how an image is displayed.

Examples

# Optimize PNG binary in memory
{:ok, optimized_binary} = Oxipng.optimize(png_data)

# Choose a preset and metadata stripping policy
{:ok, optimized_binary} = Oxipng.optimize(png_data, level: 4, strip: :safe)

# Optimize using bang variant
optimized_binary = Oxipng.optimize!(png_data)

# Optimize a file in-place
{:ok, result} = Oxipng.optimize_file("image.png")
#=> {:ok, %{in_bytes: 12450, out_bytes: 8320}}

# Optimize a file to a new location
{:ok, result} = Oxipng.optimize_file("image.png", "image_optimized.png", level: 6)

# Create an optimized PNG directly from raw RGBA pixel data
{:ok, png} = Oxipng.create_optimized_from_raw(raw_rgba_bytes, 100, 100)

Summary

Functions

Creates an optimized PNG directly from raw uncompressed pixel data.

Optimizes a PNG binary image in memory.

Same as optimize/2, but returns the optimized binary directly or raises an Oxipng.Error.

Same as optimize_file/3, but returns %{in_bytes: in_bytes, out_bytes: out_bytes} or raises Oxipng.Error.

Returns the version of the underlying oxipng Rust library.

Types

optimization_stats()

@type optimization_stats() :: %{
  in_bytes: non_neg_integer(),
  out_bytes: non_neg_integer()
}

Functions

create_optimized_from_raw(data, width, height, color_type \\ :rgba, bit_depth \\ 8, opts \\ [])

@spec create_optimized_from_raw(
  binary(),
  pos_integer(),
  pos_integer(),
  atom() | tuple(),
  1 | 2 | 4 | 8 | 16,
  keyword() | map() | Oxipng.Options.t()
) :: {:ok, binary()} | {:error, String.t()}

Creates an optimized PNG directly from raw uncompressed pixel data.

Parameters

  • data - Raw pixel binary.
  • width - Image width in pixels.
  • height - Image height in pixels.
  • color_type - One of :rgba (default), :rgb, :grayscale, :grayscale_alpha, or {:indexed, palette_binary}.
  • bit_depth - 1, 2, 4, 8 (default), or 16.
  • opts - Optimization options (see Oxipng.Options).

Returns

  • {:ok, binary} containing the optimized PNG.
  • {:error, reason} on failure.

create_optimized_from_raw!(data, width, height, color_type \\ :rgba, bit_depth \\ 8, opts \\ [])

@spec create_optimized_from_raw!(
  binary(),
  pos_integer(),
  pos_integer(),
  atom() | tuple(),
  1 | 2 | 4 | 8 | 16,
  keyword() | map() | Oxipng.Options.t()
) :: binary()

Same as create_optimized_from_raw/6, but returns the binary or raises Oxipng.Error.

optimize(png_data, opts \\ [])

@spec optimize(binary(), keyword() | map() | Oxipng.Options.t()) ::
  {:ok, binary()} | {:error, String.t()}

Optimizes a PNG binary image in memory.

Options

See Oxipng.Options for available options and presets.

Returns

  • {:ok, binary} on success.
  • {:error, reason} if the input is not a valid PNG or optimization fails.

optimize!(png_data, opts \\ [])

@spec optimize!(binary(), keyword() | map() | Oxipng.Options.t()) :: binary()

Same as optimize/2, but returns the optimized binary directly or raises an Oxipng.Error.

optimize_file(in_path, out_path_or_opts \\ nil, opts \\ [])

@spec optimize_file(
  Path.t(),
  Path.t() | nil | keyword() | map() | Oxipng.Options.t(),
  keyword() | map() | Oxipng.Options.t()
) :: {:ok, optimization_stats()} | {:error, String.t()}

Optimizes a PNG file on disk.

If out_path is omitted or nil, the input file will be optimized in-place. Empty paths are rejected. Output is written to a temporary sibling and then atomically replaces the destination, so failed writes leave existing files intact. Symbolic links are followed; other hard links retain the previous file contents.

Parameters

  • in_path - Path to the source PNG file.
  • out_path - (Optional) Destination path. If not provided or nil, in_path is overwritten.
  • opts - (Optional) Optimization options (see Oxipng.Options).

Returns

  • {:ok, %{in_bytes: in_size, out_bytes: out_size}} on success.
  • {:error, reason} on failure.

optimize_file!(in_path, out_path_or_opts \\ nil, opts \\ [])

@spec optimize_file!(
  Path.t(),
  Path.t() | nil | keyword() | map() | Oxipng.Options.t(),
  keyword() | map() | Oxipng.Options.t()
) :: optimization_stats()

Same as optimize_file/3, but returns %{in_bytes: in_bytes, out_bytes: out_bytes} or raises Oxipng.Error.

version()

@spec version() :: String.t()

Returns the version of the underlying oxipng Rust library.