PhoenixKitOG.Render.Rasterizer (PhoenixKitOG v0.1.1)

Copy Markdown View Source

SVG → PNG conversion. Shells out to rsvg-convert (librsvg). The binary is part of the librsvg2-bin package on Debian/Ubuntu — if not installed, rendering returns {:error, :rasterizer_missing} and refine_og/4 falls back to the input OG map so nothing crashes user-facing.

A second backend (ImageMagick magick) is wired as a fallback for hosts without rsvg-convert, but its SVG rendering quality is lower — prefer rsvg.

Why rsvg-convert

  • Best quality / native librsvg renderer.
  • Single dependency, very fast (~50ms per 1200×630).
  • Drop-in piping: SVG via stdin, PNG via stdout.

Summary

Functions

Rasterizes the given SVG iodata/binary to a PNG binary.

Reports which rasterizer backend is reachable on this host.

Functions

render(svg, opts \\ [])

@spec render(
  iodata() | binary(),
  keyword()
) :: {:ok, binary()} | {:error, term()}

Rasterizes the given SVG iodata/binary to a PNG binary.

opts:

  • :width / :height — output size (defaults to the SVG's intrinsic dimensions).
  • :timeout — wallclock ms for the converter, default 5000.

Errors are returned, never raised — the caller decides whether to return a fallback URL or 404.

which_backend()

@spec which_backend() :: :resvg_nif | :resvg_cli | :rsvg | :magick | :none

Reports which rasterizer backend is reachable on this host.

Preference order:

  • :resvg_nif — the :resvg Hex package's NIF (mrdotb/resvg_nif). Precompiled via rustler_precompiled, shipped inside mix deps.get. Zero system-install burden in the common case. This is the default.
  • :resvg_clicargo install resvg binary on PATH. Same renderer but as a subprocess; only used if the NIF isn't available.
  • :rsvg — librsvg2's rsvg-convert. Pipes via stdin/stdout.
  • :magick — ImageMagick. Last-resort fallback.
  • :none — no backend reachable; refine_og/4 becomes a pass- through (never crashes).