exmagick v0.0.2 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

A handle used by the GraphicsMagick API

Functions

Queries attribute on image

Changes image attributes

Saves an image to one or multiple files

Loads into the handler an image from a file

Creates a new image handle with default values

Queries the image size

Resizes the image

Generates a thumbnail for image

Types

image

A handle used by the GraphicsMagick API

It’s internal structure should never be assumed, instead, it should be used with this module’s functions

Functions

attr(handle, attribute)

Specs

attr(image, atom) ::
  {:ok, attr_value :: term} |
  {:error, reason :: term}

Queries attribute on image.

In addition to the attributess defined in attr/3, the following are available:

  • :magick - Image encoding format (e.g. “GIF”);
attr(handle, attribute, value)

Specs

attr(image, atom, term) ::
  {:ok, image} |
  {:error, reason :: term}

Changes image attributes.

Currently the following attributes are available:

  • :adjoin (defaults to true) - set to false to produce different images for each frame;
attr!(handle, attribute)

Specs

attr!(image, atom) :: image
attr!(handle, attribute, value)

Specs

attr!(image, atom, term) :: image
crop(handle, x, y, width, height)

Specs

crop(image, non_neg_integer, non_neg_integer, non_neg_integer, non_neg_integer) ::
  {:ok, image} |
  {:error, reason :: term}

Crops the image.

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

Specs

crop!(image, non_neg_integer, non_neg_integer, non_neg_integer, non_neg_integer) :: image
image_dump(handle, path)

Specs

image_dump(image, Path.t) ::
  {:ok, image} |
  {:error, reason :: term}

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, path)

Specs

image_dump!(image, Path.t) :: image
image_load(handle, path)

Specs

image_load(image, Path.t) ::
  {:ok, image} |
  {:error, reason :: term}

Loads into the handler an image from a file.

image_load!(handle, path)

Specs

image_load!(image, Path.t) :: image
init()

Specs

init :: {:ok, image} | {:error, reason :: term}

Creates a new image handle with default values.

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

init!()

Specs

init! :: image
size(handle)

Specs

size(image) :: {:ok, %{height: pos_integer, width: pos_integer}}

Queries the image size

size(handle, width, height)

Specs

size(image, pos_integer, pos_integer) ::
  {:ok, image} |
  {:error, reason :: term}

Resizes the image.

size!(handle)

Specs

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

Specs

size!(image, pos_integer, pos_integer) :: image
thumb(handle, width, height)

Specs

thumb(image, pos_integer, pos_integer) ::
  {:ok, image} |
  {:error, reason :: term}

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!(image, pos_integer, pos_integer) :: image