Installation:
composer require camelot/arbitration
For Symfony bundles, add to config/bundles.php:
Camelot\ArbitrationBundle\ArbitrationBundle::class => ['all' => true],
Basic Middleware Registration:
Register the middleware in config/routes.php (Symfony) or app/Http/Kernel.php (Laravel):
// Laravel (Kernel.php)
protected $middleware = [
\Camelot\Arbitration\Middleware\ImageArbitration::class,
];
First Use Case:
POST /upload).public function upload(Request $request) {
$request->validate(['image' => 'required|image']);
$path = $request->file('image')->store('uploads');
return response()->json(['path' => $path]);
}
Configuration:
Override default settings in config/packages/camelot_arbitration.yaml (Symfony) or config/arbitration.php (Laravel):
# Symfony
arbitration:
default_driver: 'gd'
allowed_formats: ['jpg', 'png', 'webp']
max_width: 2000
max_height: 2000
Request Handling:
Route::post('/profile-pic', [ProfileController::class, 'upload'])
->middleware(ImageArbitration::class);
Custom Processing:
public function handle($request, Closure $next) {
if ($request->hasFile('image')) {
$img = $request->file('image');
$image = Image::make($img->getRealPath());
$image->fit(800, 600); // Custom resize
$image->save($img->path());
}
return $next($request);
}
Batch Processing:
public function uploadMultiple(Request $request) {
foreach ($request->file('images') as $image) {
$processedPath = $this->processImage($image);
// Store $processedPath
}
}
Integration with Storage:
Filesystem to handle processed files:
use Illuminate\Support\Facades\Storage;
$path = Storage::disk('public')->putFile('processed', $request->file('image'));
Conditional Processing: Use middleware groups or route middleware to apply arbitration only to specific routes.
Route::middleware(['arbitration'])->group(function () {
Route::post('/admin/images', [AdminController::class, 'upload']);
});
Dynamic Configuration: Override settings per route or controller:
public function upload(Request $request) {
config(['arbitration.max_width' => 1500]);
// Process image...
}
Event-Based Processing:
Listen for image.processed events to trigger post-processing (e.g., notifications, analytics):
event(new ImageProcessed($request->file('image'), $processedPath));
Missing Intervention Image:
Ensure intervention/image is installed (composer require intervention/image).
The bundle relies on it for core functionality.
File Validation: The middleware assumes files are validated before processing. Add validation rules to avoid errors:
$request->validate([
'image' => 'required|image|mimes:jpg,png|max:2048',
]);
Memory Limits:
Large images may hit PHP’s memory limit. Adjust memory_limit in php.ini or optimize image processing:
$image->resize(1000, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
Symlink Issues (Symfony):
If using Symfony, ensure the public/uploads directory is symlinked to var/uploads to avoid permission errors.
Middleware Order:
Place ImageArbitration after file upload middleware (e.g., ConvertEmptyStringsToNull) but before business logic.
Log Processing: Enable debug mode to log image operations:
arbitration:
debug: true
Check logs in storage/logs/laravel.log (Laravel) or var/log/dev.log (Symfony).
Common Errors:
Camelot\ArbitrationBundle is autoloaded (run composer dump-autoload).sudo apt-get install php-gd) or use imagick driver:
arbitration:
default_driver: 'imagick'
Performance:
Cache component.->encode() with optimal quality settings to reduce file size:
$image->encode('jpg', 80); // 80% quality
Extension Points:
\Camelot\Arbitration\Driver\DriverInterface to support new image libraries (e.g., Imagick).$image->filter(function ($img) {
$img->contrast(10);
$img->brightness(5);
});
Testing:
$middleware = new ImageArbitration();
$response = $middleware->handle($request, function () {
return new Response();
});
laravel-breeze or symfony/panther for end-to-end upload tests.Security:
$filename = Str::random(40).'.'.$request->file('image')->extension();
Laravel-Specific:
php artisan vendor:publish --tag=arbitration-config
HandleUploadedFile trait for seamless file handling:
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\WithSymfonyRequest;
class UploadController extends Controller implements ShouldQueue
{
use DispatchesJobs, WithSymfonyRequest;
// ...
}
How can I help you explore Laravel packages today?