rdcstarr/laravel-media

Flexible media management for Laravel with auto-optimization, multi-format images (WebP/AVIF), video support, and dynamic collections.

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/rdcstarr/laravel-media

v1.1.0 2025-11-24 15:46 UTC

This package is auto-updated.

Last update: 2025-11-24 15:47:45 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A flexible and powerful media management package for Laravel that handles images, videos, audio files, and documents with ease. Features automatic image optimization, multiple format support (WebP, AVIF), dynamic collections, and seamless Eloquent integration.

Features

  • ๐Ÿ–ผ๏ธ Image Processing: Automatic resizing, cropping, and format conversion (WebP, AVIF, PNG, JPG)
  • ๐Ÿ“ Multiple File Types: Support for images, videos, audio, and generic files
  • ๐Ÿ”— Model-Centric Design: Tightly integrated with Eloquent models
  • ๐Ÿ“ฆ Collections: Organize media files into named collections
  • ๐ŸŽฏ Flexible Configuration: Per-collection settings for disk, path, dimensions, and more
  • ๐Ÿ—‘๏ธ Automatic Cleanup: Files are deleted when models or media records are removed
  • ๐Ÿ”„ Soft Delete Support: Respects soft deletes on parent models
  • ๐Ÿ“ค URL Support: Upload files directly from URLs
  • ๐Ÿท๏ธ Metadata: Store custom metadata with each media file
  • โšก Events: Listen to media created, updated, and deleted events

Installation

You can install the package via composer:

composer require rdcstarr/laravel-media

You can publish and run the migrations with:

php artisan vendor:publish --tag="laravel-media-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="laravel-media-config"

Usage

Setup Model

use Rdcstarr\Media\Traits\HasMedia;
use Rdcstarr\Media\Enums\{MediaType, ImageExtension};

class Product extends Model
{
    use HasMedia;

    public function mediaCollection(): array
    {
        return [
            'thumbnail' => [
                'type' => MediaType::Image,
                'width' => 300,
                'height' => 300,
                'extensions' => [ImageExtension::WEBP => 90, ImageExtension::JPG => 85],
            ],
            'gallery' => [
                'type' => MediaType::Image,
                'width' => 1200,
                'extensions' => [ImageExtension::WEBP => 90, ImageExtension::AVIF => 80],
            ],
        ];
    }
}

Upload & Retrieve

// Upload
$product->attachMedia()->file($request->file('image'))->addToCollection('gallery');
$product->attachMedia()->file('https://example.com/image.jpg')->replaceExisting()->addToCollection('thumbnail');

// Retrieve
$url = $product->getMediaUrl('thumbnail', 'webp');
$product->getMediaThumbnailUrl('webp');  // Magic method
$product->clearMediaCollection('gallery', ['jpg']);  // Delete

Configuration

Collection Options

[
    'type' => MediaType::Image,           // Media type (Image|Video|Audio|File)
    'disk' => 'public',                   // Storage disk
    'path' => '{model}/{collection}',     // Path with placeholders: {ulid}, {uuid}, {model}, {collection}
    'name' => '{ulid}',                   // Filename pattern
    'visibility' => 'public',             // File visibility
    'width' => 1200,                      // Image width (px)
    'height' => 800,                      // Image height (px)
    'fit' => 'contain',                   // Fit mode (contain|cover|fill)
    'extensions' => [                     // Formats with quality
        ImageExtension::WEBP => 90,
        ImageExtension::AVIF => 80,
    ],
]

Enums (Type-Safe)

  • MediaType: Image, Video, Audio, File
  • ImageExtension: JPEG, JPG, PNG, WEBP, AVIF, GIF, SVG, BMP, TIFF
  • VideoExtension: MP4, WEBM, OGG, AVI, MOV, WMV, FLV, MKV, M4V
  • AudioExtension: MP3, WAV, OGG, M4A, AAC, FLAC, WMA, OPUS

Events & Architecture

Events: MediaCreated, MediaUpdated, MediaDeleted

Model-centric design: All operations require a model instance, ensuring type safety, clear ownership, and automatic cleanup of files when models or media are deleted.

๐Ÿงช Testing

composer test

๐Ÿ“– Resources

  • Changelog for more information on what has changed recently. โœ๏ธ

๐Ÿ‘ฅ Credits

๐Ÿ“œ License

  • License for more information. โš–๏ธ