exmagick v0.0.4 ExMagick

NIF bindings to the GraphicsMagick API.

Examples

Transform a PNG image to JPEG

ExMagick.init!()
|> ExMagick.image_load!(Path.join(__DIR__, "../test/images/elixir.png"))
|> ExMagick.image_dump!("/tmp/elixir.jpg")

Query a file type

ExMagick.init!()
|> ExMagick.image_load!(Path.join(__DIR__, "../test/images/elixir.png"))
|> ExMagick.attr!(:magick)

Generate a thumbnail from an image

ExMagick.init!()
|> ExMagick.image_load!(Path.join(__DIR__, "../test/images/elixir.png"))
|> ExMagick.thumb!(64, 64)
|> ExMagick.image_dump!("/tmp/elixir-thumbnail.jpg")

Generate a thumbnail from an image without breaking errors

with {:ok, handler} <- ExMagick.init(),
     img_path = Path.join(__DIR__, "../test/images/elixir.png"),
     {:ok, _} <- ExMagick.image_load(handler, img_path),
     {:ok, _} <- ExMagick.thumb(handler, 128, 64),
     thumb_path = "/tmp/elixir-thumbnail.png",
     {:ok, _} <- ExMagick.image_dump(handler, thumb_path),
  do: {:ok, thumb_path}
  • Converting a multi-page PDF to individual PNG images with 300dpi
ExMagick.init!
|> ExMagick.attr!(:density, "300") # density should be set before loading the image
|> ExMagick.image_load!(Path.joint(__DIR__, "../test/images/elixir.pdf"))
|> ExMagick.attr!(:adjoin, false)
|> ExMagick.image_dump!("/tmp/splitted-page-%0d.png")

Summary

Types

An opaque handle used by the GraphicsMagick API

Functions

Queries attribute on image. Refer to attr/3 for more information

Changes image attributes

Returns the image as a binary. You can change the type of this image using the :magick attribute

Saves an image to one or multiple files

Loads an image into the handler. You may provide a file path or a tuple {:blob, ...} which the second argument is the blob to load

Creates a new image handle with default values

Refer to init/0

Queries the image size

Resizes the image

Generates a thumbnail for image

Types

exm_error :: {:error, String.t}
handle

An opaque handle used by the GraphicsMagick API

Functions

attr(handle, attribute)

Specs

attr(handle, atom) ::
  {:ok, String.t | boolean | non_neg_integer} |
  exm_error

Queries attribute on image. Refer to attr/3 for more information.

In addition to attr/3 the following attributes are defined:

  • :rows The horizontal size in pixels of the image
  • :columns The vertical size in pixels of the image
attr(handle, attribute, value)

Specs

attr(handle, :atom, String.t | boolean) ::
  {:ok, handle} |
  exm_error

Changes image attributes.

Currently the following attributes are available:

  • :adjoin (defaults to true) - set to false to produce different images for each frame;
  • :magick - the image type [ex.: PNG]
  • :density - horizontal and vertical resolution in pixels of this image; [default: 72]
attr!(handle, attribute)

Specs

attr!(handle, atom) ::
  String.t |
  boolean |
  non_neg_integer

Refer to attr/2

attr!(handle, attribute, value)

Specs

attr!(handle, :atom, String.t | boolean) :: handle

Refer to attr/3

crop(handle, x, y, width, height)

Specs

crop(handle, non_neg_integer, non_neg_integer, non_neg_integer, non_neg_integer) ::
  {:ok, handle} |
  exm_error

Crops the image.

  • x, y refer to starting point, where (0, 0) is top left
crop!(handle, x, y, width, height)

Specs

crop!(handle, non_neg_integer, non_neg_integer, non_neg_integer, non_neg_integer) :: handle

Refer to crop/5.

image_dump(handle)

Specs

image_dump(handle) :: {:ok, binary} | exm_error

Returns the image as a binary. You can change the type of this image using the :magick attribute.

image_dump(handle, path)

Specs

image_dump(handle, Path.t) ::
  {:ok, handle} |
  exm_error

Saves an image to one or multiple files.

If the attr :adjoin is false, multiple files will be created and the filename is expected to have a printf-formatting sytle (ex.: foo%0d.png).

image_dump!(handle)

Specs

image_dump!(handle) :: binary

Refer to image_dump/1

image_dump!(handle, path)

Specs

image_dump!(handle, Path.t) :: handle

Refer to image_dump/2

image_load(handle, path)

Specs

image_load(handle, Path.t | {:blob, binary}) ::
  {:ok, handle} |
  exm_error

Loads an image into the handler. You may provide a file path or a tuple {:blob, ...} which the second argument is the blob to load.

image_load!(handle, path_or_blob)

Specs

image_load!(handle, Path.t | {:blob, binary}) :: handle

Refer to image_load!/2

init()

Creates a new image handle with default values.

Image attributes may be tuned by using the attr/3 function.

init!()

Specs

init! :: handle

Refer to init/0

size(handle)

Specs

size(handle) ::
  {:ok, %{width: non_neg_integer, height: non_neg_integer}} |
  exm_error

Queries the image size

size(handle, width, height)

Specs

size(handle, non_neg_integer, non_neg_integer) ::
  {:ok, handle} |
  exm_error

Resizes the image.

size!(handle)

Specs

size!(handle) :: %{width: non_neg_integer, height: non_neg_integer}

Refer to size/1.

size!(handle, width, height)

Specs

size!(handle, non_neg_integer, non_neg_integer) :: handle

Refer to size/3

thumb(handle, width, height)

Specs

thumb(handle, non_neg_integer, non_neg_integer) ::
  {:ok, handle} |
  exm_error

Generates a thumbnail for image.

Note that this method resizes the image as quickly as possible, with more concern for speed than resulting image quality.

thumb!(handle, width, height)

Specs

thumb!(handle, non_neg_integer, non_neg_integer) :: handle