/imgproxy

Elixir module to generate imgproxy URLs

Primary LanguageElixirMIT LicenseMIT

Imgproxy

Build Status Module Version Hex Docs

Imgproxy is an Elixir library that helps generate imgproxy URLs. Before using this library, you should have a running imgproxy server.

Note: As of version 3.0, OTP >= 22.1 and imgproxy >= 2.0.0 are required.

Installation

To install Imgproxy, just add an entry to your mix.exs:

def deps do
  [
    {:imgproxy, "~> 3.0"}
  ]
end

(Check Hex to make sure you're using an up-to-date version number.)

Configuration

In your config/config.exs you can set a few options:

config :imgproxy,
  prefix: "https://imgcdn.example.com",
  key: "cdf104fc78b7d7f6f0158c253612f5dsecretsecret...",
  salt: "aad7034f611b7fc28c6d344f72ea19secretsecret..."

The prefix should be the location of the imgproxy server. key and salt are only necessary if you are using URL signatures. To generate the key a key and salt, you can use:

$ mix imgproxy.gen.secret

You can use the output as your key or salt (ideally, just run the command twice, use the first output for your key and the second output for your salt).

Usage

Usage is simple - first generate an Imgproxy struct via Imgproxy.new/1, add any options you'd like, then convert to a string.

Example:

# Generate URL for an image, using defaults
Imgproxy.new("https://placekitten.com/200/300") |> to_string()

# Resize to 123x321
"https://placekitten.com/200/300"
|> Imgproxy.new()
|> Imgproxy.resize(123, 321, type: "fill")
|> to_string()


# Crop and return a jpg
"https://placekitten.com/200/300"
|> Imgproxy.new()
|> Imgproxy.crop(100, 100)
|> Imgproxy.set_extension("jpg")
|> to_string()

Running Tests

To run tests:

$ mix test

Reporting Issues

Please report all issues on github.

Copyright and License

Copyright (c) 2019 Brian Muller

This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See the LICENSE.md file for more details.