Installation
composer require comensee/gallery
Publish the package config (if needed):
php artisan vendor:publish --provider="Comensee\Gallery\GalleryServiceProvider"
Configuration
Check config/gallery.php for default settings (e.g., storage paths, allowed extensions). Customize as needed:
'storage' => [
'disk' => 'public',
'path' => 'uploads/gallery',
],
First Use Case: Uploading an Image
Inject the Gallery facade or service into a controller:
use Comensee\Gallery\Facades\Gallery;
public function upload(Request $request) {
$file = $request->file('image');
$upload = Gallery::upload($file, [
'resize' => [800, 600], // Optional: Resize dimensions
'thumbnail' => true, // Optional: Generate thumbnail
]);
return response()->json($upload);
}
Routes & Middleware
Register routes in routes/web.php or routes/api.php:
use Comensee\Gallery\Facades\Gallery;
Route::post('/upload', [GalleryController::class, 'upload']);
Uploading Files
Gallery::upload() with optional parameters:
$result = Gallery::upload($file, [
'resize' => [1200, 800],
'thumbnail' => true,
'watermark' => 'path/to/watermark.png',
]);
path: Full storage path.url: Publicly accessible URL.thumbnail_url: If generated.Generating Thumbnails
thumbnail => true) or manually:
$thumbnail = Gallery::generateThumbnail('original-path.jpg', 200, 200);
Deleting Files
Gallery::delete('uploads/gallery/123.jpg');
Integration with Eloquent
class Product extends Model {
public function uploadGallery(Request $request) {
$this->image_path = Gallery::upload($request->file('image'))['path'];
$this->save();
}
}
Batch Processing
foreach ($request->file('images') as $file) {
Gallery::upload($file, ['resize' => [500, 500]]);
}
Custom Storage Disks Override the default disk in config or dynamically:
$upload = Gallery::upload($file, [], 's3');
Event Listeners Listen for upload events (if the package supports them):
// In EventServiceProvider
public function boot() {
Gallery::onUpload(function ($path) {
Log::info("Uploaded: {$path}");
});
}
API Responses Normalize responses for consistency:
return response()->json([
'success' => true,
'data' => Gallery::upload($file),
]);
File Validation
mimes:jpeg,png).$validated = $request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg|max:2048',
]);
Storage Permissions
storage/app/public/uploads/gallery directory is writable:
chmod -R 755 storage/app/public/uploads
Missing Thumbnails
php -m | grep gd).thumbnail is set to true.Resizing Issues
memory_limit in php.ini or resize smaller dimensions first.Database vs. Filesystem
Enable Logging
Add to config/gallery.php:
'debug' => env('GALLERY_DEBUG', false),
Check storage/logs/laravel.log for errors.
Check Uploaded Files
Verify files exist in storage/app/public/uploads/gallery after upload.
Test with Small Files Use small images (e.g., 100KB) to rule out size/memory issues.
Clear Cache If changes to config aren’t applying:
php artisan config:clear
php artisan cache:clear
Custom Processors Extend the package by adding processors (e.g., for PDFs or videos):
// In a service provider
Gallery::extend('pdf', function ($file) {
// Custom logic for PDFs
});
Override Storage Logic Bind a custom storage handler:
Gallery::extendStorage(function ($disk) {
return Storage::disk($disk)->putFileAs(...);
});
Add Metadata
Use Laravel’s File class to extract metadata:
$file = $request->file('image');
$metadata = exif_read_data($file->getPathname());
Webhook Integration Trigger external actions post-upload via events or queues:
event(new FileUploaded($upload['path']));
How can I help you explore Laravel packages today?