惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
A
About on SuperTechFans
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
H
Help Net Security
月光博客
月光博客
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
T
Tailwind CSS Blog
Jina AI
Jina AI
有赞技术团队
有赞技术团队
博客园_首页

Inside Nutrient

A guide to the invisible work behind documents Introducing Nutrient Documents for Salesforce: Native document generation and signing Document AI vs. traditional OCR: Choosing between OCR, AI, and hybrid pipelines PDF SDK compliance and security evaluation checklist for enterprise teams (2026) Invariant Corp replaces paper processes with Nutrient Workflow and scales without limits What is process mapping? A complete guide Nutrient vs. Conga Composer for Salesforce document generation (2026) Document routing: How to automate document distribution The CTO’s AI playbook: Why accountability architecture beats orchestration Compliance workflow automation: Why built-in compliance is table stakes Workflow diagrams: Examples, symbols, and how to build one that actually runs Digital forms: Replace paper forms with automated workflows Approval workflow software: How to automate approvals Why document-centric automation is different The CEO’s AI playbook: Why decision architecture beats model selection Nutrient SDK product updates for Q1 2026 PDF redaction verification: How to prove sensitive data is permanently removed What is a VPAT? The complete guide to accessibility conformance reports What is PDF/UA? The accessible PDF standard explained Salesforce eSignatures: Generate, sign, and track documents in one flow Online document viewer: Options, tradeoffs, and how to embed one Document viewer for web apps: React, Vue, Angular (2026) Best document viewers in 2026: A buyer’s guide How to edit a PDF in Python: Add text, images, and annotations Nutrient advances Workflow platform with agentic AI for enterprise-grade speed and consistency in document-heavy operations How to create a Salesforce quote template from opportunity data The business case for accessibility: Five ways it drives enterprise value Python PDF library comparison (2026): 7 libraries for developers Why your AI agent hallucinates PDF table data PDF.js limitations: When to upgrade to a commercial PDF SDK
Random image generation API with Elixir and Phoenix
Oghenerukevwe Henrietta Kofi · 2025-08-21 · via Inside Nutrient

Table of contents

    Random image generation API with Elixir and Phoenix

    TL;DR

    This tutorial walks you through building a simple Elixir + Phoenix API that generates random PNG images using Erlang’s EGD library. You’ll set up your environment with asdf, add dependencies, implement an image service, create a controller and endpoint, and serve images directly from your API. By the end, you’ll have a working foundation you can extend for dynamic charts, placeholders, or PDF assets.

    Until recently, I wasn’t aware of any tooling for generating images in Elixir, but while building internal tools that required millions of image attachments for PDFs, I had to figure it out. I learned a lot from that project, and decided to write a tutorial covering how to build an API for generating random images using Elixir and Phoenix.

    Document Engine helps automate creation, delivery, and management of large volumes of assets.

    Setup

    For this tutorial, use the asdf version manager to install the required versions of Elixir and Erlang.

    Next, run these commands to create and navigate to a new directory:

    $ mkdir image_generator

    $ cd image_generator

    Then, create a file named .tool-versions in the image_generator, and add the following to the file:

    elixir 1.18.4-otp-26

    erlang 26.2.2

    From within the image_generator directory in the terminal, run asdf install. This will set up the required Elixir and Erlang versions on your machine.

    Building the API

    Run these commands to set up the Phoenix project:

    $ mix archive.install hex phx_new

    $ mix phx.new image_generator_api --no-html --no-assets --no-ecto # Agree to install dependencies

    $ cd image_generator_api

    $ mix deps.get

    Modify the mix.exs file in the new image_generator_api project to include the egd dependency:

    defmodule ImageGeneratorApi.MixProject do

    #... Omitted for brevity.

    # Type `mix help deps` for examples and options.

    defp deps do

    [

    #...

    {:bandit, "~> 1.5"},

    {:egd, github: "erlang/egd"}

    ]

    end

    end

    Run mix deps.get again to install the new dependency.

    Create a new image_generator_api/lib/image_generator_api/image_service.ex file and add the following content:

    defmodule ImageGeneratorApi.ImageService do

    @moduledoc """

    Core image generation service using Erlang/EGD library

    """

    def get_random_image() do

    length = Enum.random(150..300)

    {point1, point2, color} = random_image_specs(length)

    image = :egd.create(length, length)

    color = :egd.color(color)

    shape = Enum.random([:filledRectangle, :filledEllipse])

    Kernel.apply(:egd, shape, [image, point1, point2, color])

    :egd.render(image)

    end

    def random_image_specs(points_upper_limit \\ 140) do

    # Generate 2D points 1 and 2.

    p1_x = Enum.random(50..points_upper_limit)

    p1_y = Enum.random(50..points_upper_limit)

    p2_x = Enum.random(50..points_upper_limit)

    p2_y = Enum.random(50..points_upper_limit)

    # Generate random color.

    color = {Enum.random(0..256), Enum.random(0..256), Enum.random(0..256)}

    {

    {p1_x, p1_y},

    {p2_x, p2_y},

    color

    }

    end

    end

    Then create a new image_generator_api/lib/image_generator_api_web/controllers/image_controller.ex file and add the following content:

    defmodule ImageGeneratorApiWeb.ImageController do

    use ImageGeneratorApiWeb, :controller

    alias ImageGeneratorApi.ImageService

    require Logger

    @doc """

    Generates a random image

    """

    def image(conn, _params) do

    image_data = ImageService.get_random_image()

    send_image_response(conn, image_data)

    rescue

    e ->

    Logger.error("Image generation failed: #{inspect(e)}")

    send_error_response(conn, "An error occurred while generating the image")

    end

    defp send_image_response(conn, image_data) do

    conn

    |> put_resp_content_type("image/png")

    |> put_resp_header("cache-control", "no-cache, no-store, must-revalidate")

    |> put_resp_header("pragma", "no-cache")

    |> put_resp_header("expires", "0")

    |> send_resp(200, image_data)

    end

    defp send_error_response(conn, reason) do

    conn

    |> put_status(:internal_server_error)

    |> json(%{

    error: "Image generation failed",

    reason: reason,

    timestamp: DateTime.utc_now()

    })

    end

    end

    Next, modify the router at image_generator_api/lib/image_generator_api_web/router.ex by including a new GET /api/image, like so:

    defmodule ImageGeneratorApiWeb.Router do

    use ImageGeneratorApiWeb, :router

    pipeline :api do

    plug :accepts, ["json"]

    end

    scope "/api", ImageGeneratorApiWeb do

    pipe_through :api

    get "/image", ImageController, :image

    end

    #...

    end

    Finally, start the Phoenix server locally with mix phx.server. After starting the server, in your browser, navigate to http://localhost:4000/api/image to see a new image. Reloading the page should generate a new image every time.

    Document Engine makes it easy to automate large-scale generation and delivery.

    Conclusion

    This project showed how to build a simple API for random image generation using Elixir, Phoenix, and Erlang’s EGD library(opens in a new tab). While the example was straightforward, the same approach can scale to more advanced use cases — such as generating charts, placeholders, or dynamic assets for PDFs and web applications.

    If you’re new to Elixir, this is also a great way to get hands-on experience with Phoenix and explore how Elixir can power APIs beyond the typical web or data processing workloads.

    For developers looking to take image generation further, Document Engine can help you automate the creation and delivery of large-scale image and PDF assets. Whether you’re generating millions of attachments for reports, invoices, or dynamic marketing materials, Document Engine streamlines the workflow so you can focus on building your app, not managing files.

    Want to experiment more? Try extending this project by adding parameters for image size, colors, or shapes, or integrating it into a larger Elixir system — and see how Document Engine can help scale your output seamlessly.

    Explore related topics

    Try for free Ready to get started?

    Related Development articles

    Explore more