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
Requires
- php: ^8.3
- illuminate/contracts: ^11.0||^12.0
- spatie/image: ^3.8
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
README
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
- Rdcstarr ๐
๐ License
- License for more information. โ๏ธ