f-bahesna / intervention-image-wrapper
Simple Laravel wrapper for Intervention Image v3.
Package info
github.com/f-bahesna/intervention-image-wrapper
pkg:composer/f-bahesna/intervention-image-wrapper
v1.0.6
2025-12-02 01:10 UTC
Requires
- php: ^8.1
- illuminate/support: ^9.0|^10.0|^11.0
- intervention/image: ^3.0
Requires (Dev)
- orchestra/testbench: ^7.0
- phpunit/phpunit: ^9.5
README
A lightweight Laravel wrapper around Intervention Image v3 that provides a fluent, clean API to upload, resize, crop, convert, and store images easily across multiple disks (local, S3, OSS, etc.).
It is designed sometimes to remember me about simplify image handling tasks in Laravel projects while keeping performance and code clarity.
Installation
composer require f-bahesna/intervention-image-wrapper # Publish config (optional) php artisan vendor:publish --provider="Fbahesna\InterventionImageWrapper\ImageWrapperServiceProvider" --tag="imagewrapper-config"
Configuration (config/imagewrapper.php)
return [ 'disk' => env('IMAGE_WRAPPPER_DISK', 'public'), 'quality' => env('IMAGE_WRAPPER_QUALITY', 85), 'tmp_dir' => env('IMAGE_WRAPPER_TMP', sys_get_temp_dir()), 'intervention' => [ 'driver' => env('IMAGE_WRAPPER_DRIVER', 'gd'), // or 'imagick' ], ];
1) Manually Upload & Resize ❌
use Intervention\Image\ImageManager; use Illuminate\Support\Facades\Storage; $manager = new ImageManager(['driver' => 'gd']); $image = $manager->make($request->file('avatar')->getPathname()) ->resize(300, 300); $filename = 'avatars/1.jpg'; Storage::disk('public')->put($filename, (string) $image->encode('jpg', 85)); $url = Storage::disk('public')->url($filename);
Pain points:
- Lots of boilerplate code (managing
$manager, encoding, storage). - Manual handling of
resize,encode, anddisk.
2) Upload & Resize using Intervention Image Wrapper ✅
use Fbahesna\InterventionImageWrapper\Facades\ImageWrapper; $url = ImageWrapper::load($request->file('avatar')) ->resize(300, 300) ->store('avatars/1.jpg');
Advantages:
- Fluent & chainable API (
load()->resize()->store()). - Automatically handles storage disk and encoding.
- Easier unit testing with
Orchestra\Testbench. - Optional helper function
imagewrap()for even cleaner syntax. - Consistent quality & file naming across the app.
2. a) Using helper for even cleaner code ✅ ✅
$url = imagewrap()->load($request->file('avatar')) ->fit(200, 200) ->store('avatars/photo.webp');
- Quick one-liner without repeating
$manageror storage logic.
2. b) Using DI (dependency injection) ✅ ✅
use Fbahesna\InterventionImageWrapper\Services\ImageWrapperService; class avatarController { public function __construct(private ImageWrapperService $imageWrapper) { } public function upload(Request $request) { $url = $this->imageWrapper ->load($request->file('file')) ->resize(400, 400) ->store('avatars/image.jpg'); return response()->json(['url' => $url]); } }
Usage Examples
Basic upload & resize
use Fbahesna\InterventionImageWrapper\Facades\ImageWrapper; $url = ImageWrapper::load($request->file('avatar')) ->resize(300, 300) ->store('avatars/1.jpg');
Resize Image
$image->resize(800, 600);
Crop
$image->crop(300, 300, x: 50, y: 20);
Rotate
$image->rotate(90);
Blur
$image->blur(10);
Brightness
$image->brightness(20);
Delete an image
ImageWrapper::delete('images/photo.jpg');
Advantages
- Fluent & intuitive API: Chain operations like resize, fit, crop, rotate, and store.
- Supports multiple filesystems: Works with
local,s3,oss, or any Flysystem disk. - Lightweight: Minimal wrapper over Intervention Image v3 — no heavy dependencies.
- Reusable & testable: Can be easily tested with PHPUnit & Testbench.
- Laravel integration: Auto-discovered service provider and optional helper function.
- Flexible configuration: Set default disk, quality, temporary directory, and driver in config.
Testing
Use Orchestra Testbench to run PHPUnit tests:
vendor/bin/phpunit
Contribution
PRs welcome. Please follow standard Laravel package conventions:
- PSR-4 autoloading:
Fbahesna\InterventionImageWrapper\ - Run PHPUnit tests for every feature added.
- Keep the package lightweight and simple.
Summary Table:
| Feature | Raw Intervention Image | Using ImageWrapper |
|---|---|---|
| Setup Manager | ✅ manual | ✅ auto |
| Resize / Fit | ✅ manual | ✅ chainable |
| Encode & quality | ✅ manual | ✅ automatic |
| Store to disk | ✅ manual | ✅ automatic |
| Fluent API | ❌ | ✅ |
| Reusable in controllers | ❌ harder | ✅ easy |
| Unit Testing | ❌ harder | ✅ supported with Testbench |
| Helper function | ❌ | ✅ optional imagewrap() |