Packages

Rapport aims to provide a robust set of modules to generate HTML reports that both looks good in the browser and when being printed.

Current section

Files

Jump to
rapport lib rapport image.ex
Raw

lib/rapport/image.ex

defmodule Rapport.Image do
@doc """
Converts an image to a base64 encoded string that can be to embedded in a image tag:
<img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
Supported image types are JPEG, PNG and GIF.
## Options
* `image` - Image binary
"""
def as_data(image) do
mime_type = detect_mime_type(image)
encoded_image = Base.encode64(image)
"data:#{mime_type};base64,#{encoded_image}"
end
defp detect_mime_type(<< 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A >> <> _), do: "image/png"
defp detect_mime_type(<< 0xFF, 0xD8, 0xFF >> <> _), do: "image/jpeg"
defp detect_mime_type("GIF87a" <> _), do: "image/gif"
defp detect_mime_type("GIF89a" <> _), do: "image/gif"
defp detect_mime_type(_), do: raise ArgumentError, message: "Invalid image"
end