PhxMediaLibrary - Spatie's Media Library, reimagined for Elixir/Phoenix

Hey, I recently picked up learning Elixir and Phoenix, and this is my first Open Source library, ever. I’ve 10+ years of web dev experience with PHP and Laravel, and I am so used to the human-friendly ecosystem that I tried to translate that same feeling into idiomatic Elixir. I’d love to hear your feedback on how I did!

I did use AI, conversing with it on structure and what would the “Elixir way” entail.

PhxMediaLibrary

Ecto-backed media management library for Elixir and Phoenix, with focus on Developer Experience (DX), inspired by Spatie’s Laravel Media Library.

Here’s how to use it, to store media:

# From a file path
post
|> PhxMediaLibrary.add("/path/to/image.jpg")
|> PhxMediaLibrary.to_collection(:images)

# From a Phoenix upload
post
|> PhxMediaLibrary.add(upload)
|> PhxMediaLibrary.using_filename("custom-name.jpg")
|> PhxMediaLibrary.with_custom_properties(%{"alt" => "My image"})
|> PhxMediaLibrary.to_collection(:images)

# From a URL
post
|> PhxMediaLibrary.add_from_url("https://example.com/image.jpg")
|> PhxMediaLibrary.to_collection(:images)

Retrieving media:

# Get all media in a collection
PhxMediaLibrary.get_media(post, :images)

# Get the first media item
PhxMediaLibrary.get_first_media(post, :images)

# Get URLs
PhxMediaLibrary.get_first_media_url(post, :images)
PhxMediaLibrary.get_first_media_url(post, :images, :thumb)
PhxMediaLibrary.get_first_media_url(post, :images, fallback: "/default.jpg")

# Get URL for a specific media item
PhxMediaLibrary.url(media)
PhxMediaLibrary.url(media, :thumb)

Features

  • Associate files with Ecto schemas - Polymorphic media associations

  • Collections - Organize media into named collections with validation

  • Image conversions - Generate thumbnails, previews, and custom sizes

  • Responsive images - Automatic srcset generation for optimal loading

  • Multiple storage backends - Local filesystem, S3, or custom adapters

  • Async processing - Background conversion processing with Oban support

  • Phoenix components - Ready-to-use view helpers for templates

5 Likes