contao-components/mediabox
Mediabox integration for Contao CMS. Adds a lightweight lightbox/media overlay component to display images and other media in a modal viewer, with simple setup and frontend-friendly templates for galleries and linked media.
Installation
composer require contao-components/mediabox
Ensure contao-components/mediabox is listed in composer.json under require.
Basic Usage The package provides a MediaBox component for handling media (images, videos, files) with a clean API. Start by initializing it in a controller or service:
use ContaoComponents\MediaBox\MediaBox;
$mediaBox = new MediaBox();
First Use Case: Uploading an Image
$uploadedFile = $mediaBox->upload('image', $request->file('image'));
$mediaBox->save($uploadedFile); // Persist to storage
$url = $mediaBox->getUrl($uploadedFile); // Generate public URL
Key Files to Explore
src/MediaBox.php – Core class with methods for uploads, storage, and URL generation.config/mediabox.php (if auto-generated) – Default settings for storage paths, allowed MIME types, etc.Request Handling Validate and process uploads in a Laravel controller:
public function upload(Request $request)
{
$this->validate($request, ['file' => 'required|image']);
$mediaBox = new MediaBox();
$file = $mediaBox->upload('image', $request->file('file'));
$mediaBox->save($file);
return response()->json(['url' => $mediaBox->getUrl($file)]);
}
Storage Integration
Configure storage disks in config/mediabox.php:
'storage' => [
'disk' => 'public', // Default: 'public'
'path' => 'uploads/media', // Relative to storage path
],
Tagging & Categorization
Extend the MediaBox class to add custom metadata:
$mediaBox->setMetadata($file, ['tags' => ['newsletter', 'promo']]);
$tags = $mediaBox->getMetadata($file, 'tags', []);
Lazy Loading URLs Generate URLs on-demand to avoid pre-processing:
$url = $mediaBox->getUrl($file, ['width' => 800, 'height' => 600]); // Resize via Intervention Image (if supported)
Form Requests
Use Laravel’s FormRequest to validate uploads:
public function rules()
{
return [
'file' => 'required|mimes:jpg,png|max:2048',
];
}
Service Providers
Bind MediaBox to the container for dependency injection:
$this->app->singleton(MediaBox::class, function ($app) {
return new MediaBox(config('mediabox'));
});
Events & Observers Listen for upload events (extend the package or wrap calls):
event(new MediaUploaded($file, $request));
MIME Type Restrictions
'allowed_mimes' => ['image/jpeg', 'image/png', 'video/mp4', 'application/pdf'],
dd($request->file('file')->getMimeType()) to verify types.Storage Permissions
chmod -R 775 storage/app/public/uploads
local disk, symlink storage/app/public to public/storage.File Naming Collisions
hash.jpg). Overwrite with custom logic:
$mediaBox->setCustomName($file, 'custom-name.jpg');
Missing Intervention Image
intervention/image is installed:
composer require intervention/image
Class 'Intervention\Image\ImageManager' not found → Install the package.Log Uploads
Enable debug mode in config/mediabox.php:
'debug' => env('APP_DEBUG', false),
Logs will appear in storage/logs/mediabox.log.
Validate File Paths Check if files are saved to the correct path:
$mediaBox->getStoragePath($file); // Returns full path
Custom Storage Adapters
Implement ContaoComponents\MediaBox\Contracts\StorageAdapter for S3, Dropbox, etc.
Pre/Post-Process Hooks
Override methods like save() or getUrl() to add logic:
class CustomMediaBox extends MediaBox {
public function save($file) {
// Add watermark before saving
$this->addWatermark($file);
parent::save($file);
}
}
API Wrapper Expose a RESTful API for media management:
Route::post('/api/upload', [MediaController::class, 'upload']);
MediaBox::batchSave() for multiple files:
$mediaBox->batchSave([$file1, $file2]);
How can I help you explore Laravel packages today?