ab01faz101/laravel-image-resizer
Installation
composer require ab01faz101/laravel-image-resizer
Publish the config file (if needed):
php artisan vendor:publish --provider="Ab01faz101\LaravelImageResizer\ImageResizerServiceProvider"
Basic Usage Resize an image stored in storage to predefined sizes:
use Ab01faz101\LaravelImageResizer\Facades\ImageResizer;
$path = 'images/original.jpg';
$sizes = ['small', 'medium', 'large']; // Defined in config
ImageResizer::resize($path, $sizes);
First Use Case
$request->file('image')->store('uploads');
$path = $request->file('image')->hashName();
ImageResizer::resize($path, ['thumbnail', 'featured']);
Dynamic Size Generation
Define custom sizes in config/laravel-image-resizer.php:
'sizes' => [
'thumbnail' => ['width' => 150, 'height' => 150, 'fit' => 'crop'],
'featured' => ['width' => 800, 'height' => 450, 'fit' => 'contain'],
],
Use them in code:
ImageResizer::resize($path, ['thumbnail', 'featured']);
Integration with Eloquent
Add a resizeImages() method to your model:
public function resizeImages()
{
$this->update([
'thumbnail' => ImageResizer::resize($this->image_path, ['thumbnail']),
'featured' => ImageResizer::resize($this->image_path, ['featured']),
]);
}
Batch Processing Resize all images in a directory:
$files = Storage::files('uploads');
foreach ($files as $file) {
ImageResizer::resize(basename($file), ['small', 'medium']);
}
Queueing for Large Files Dispatch a job to avoid timeouts:
ResizeImageJob::dispatch($path, ['large']);
default disk in config/filesystems.php is writable.ImageResizer::resize($path, ['small'], ['cache' => false]);
ImageResizer::resize($path, ['small'], ['filters' => function ($image) {
return $image->filters()->sepia();
}]);
File Permissions
755 permissions. Silent failures may occur if the package can’t write resized images.Missing Config
config/laravel-image-resizer.php is missing, the package falls back to defaults but may throw errors for undefined sizes.Image Validation
is_image() and mimes('image/*') in your controller.Overwriting Files
ImageResizer::resize($path, ['small'], ['suffix' => '_resized']);
ImageResizer::setDebug(true);
Custom Resize Logic
Extend the ResizeImage class to add pre/post-processing:
ImageResizer::extend('custom', function ($path, $options) {
// Custom logic here
return $resizedPath;
});
Event Listeners Trigger events for resizing:
ImageResizer::resize($path, ['small'])
->then(function ($result) {
// Post-resize logic
});
Fallback for Unsupported Formats Add a fallback for non-JPEG/PNG images (e.g., SVG):
ImageResizer::resize($path, ['small'], ['fallback' => 'default.svg']);
'cache' => false in config to avoid stale resized images during development.fit Wisely
fit: 'crop' may degrade quality. Prefer contain for better results when possible.How can I help you explore Laravel packages today?