dialloibrahima / intervention-image-mask
Adds mask() and opacity() modifiers to Intervention Image v3
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/dialloibrahima/intervention-image-mask
Requires
- php: ^8.3
- intervention/image: ^3.0
Requires (Dev)
- laravel/pint: ^1.14
- pestphp/pest: ^4.0
- phpstan/phpstan: ^1.10
README
๐ญ Restore the beloved
mask()andopacity()methods removed in Intervention Image v3
๐ Why This Package?
With the release of Intervention Image v3, the popular mask() and opacity() methods were removed. This package brings them back as clean, framework-agnostic modifiers that work seamlessly with both GD and Imagick drivers.
โจ Features
- ๐ญ Apply masks to images using alpha channels
- ๐ฎ Control opacity with precise float values (0.0 to 1.0)
- ๐ผ๏ธ Dual driver support - Works with both GD and Imagick
- โก Zero configuration - Just install and use
- ๐งช Fully tested - 17 tests with 25 assertions
- ๐ฆ Lightweight - No extra dependencies beyond Intervention Image
๐ฆ Installation
composer require dialloibrahima/intervention-image-mask
Requirements
- PHP 8.3 or higher
- Intervention Image 3.0 or higher
- GD or Imagick PHP extension
๐ฏ Quick Start
Apply a Mask
Use a mask image to define transparency. The mask's alpha channel determines which parts of the original image become transparent.
use DialloIbrahima\InterventionMask\ApplyMask; use Intervention\Image\ImageManager; use Intervention\Image\Drivers\Gd\Driver; $manager = new ImageManager(new Driver()); // Load your image and mask $image = $manager->read('photo.jpg'); $mask = $manager->read('mask.png'); // Apply the mask $result = $image->modify(new ApplyMask($mask)); // Save the result $result->toPng()->save('masked-photo.png');
Set Opacity
Control the transparency level of an entire image with a simple float value.
use DialloIbrahima\InterventionMask\SetOpacity; $image = $manager->read('photo.png'); // 50% opacity $result = $image->modify(new SetOpacity(0.5)); $result->toPng()->save('semi-transparent.png');
๐ API Reference
ApplyMask
Applies a mask image to define transparency regions.
new ApplyMask(ImageInterface $mask)
| Parameter | Type | Description |
|---|---|---|
$mask |
ImageInterface |
The mask image. Its alpha channel defines transparency. |
How it works:
- White areas (or opaque) in the mask โ Image is visible
- Black areas (or transparent) in the mask โ Image becomes transparent
- Gray areas โ Partial transparency
SetOpacity
Sets the overall opacity level of an image.
new SetOpacity(float $opacity)
| Parameter | Type | Description |
|---|---|---|
$opacity |
float |
Opacity level from 0.0 (fully transparent) to 1.0 (fully opaque) |
Examples:
0.0โ Completely invisible0.5โ 50% transparent1.0โ Fully opaque (no change)
๐ก Use Cases
๐งฉ Puzzle Piece Effect
$piece = $manager->read('puzzle-background.jpg'); $shape = $manager->read('puzzle-piece-shape.png'); $puzzlePiece = $piece->modify(new ApplyMask($shape));
๐ผ๏ธ Image Watermark with Transparency
$watermark = $manager->read('logo.png'); $fadedWatermark = $watermark->modify(new SetOpacity(0.3));
๐ธ Vignette Effect
$photo = $manager->read('portrait.jpg'); $vignette = $manager->read('vignette-mask.png'); $vintagePhoto = $photo->modify(new ApplyMask($vignette));
๐จ Gradient Fade
$image = $manager->read('landscape.jpg'); $gradientMask = $manager->read('horizontal-gradient.png'); $fadedImage = $image->modify(new ApplyMask($gradientMask));
๐ง Using with Imagick
The package automatically detects and uses the appropriate driver:
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver; $manager = new ImageManager(new ImagickDriver()); // Same API - driver is detected automatically $result = $image->modify(new ApplyMask($mask));
๐งช Testing
composer test
Run static analysis:
composer analyse
Format code with Pint:
composer format
๐ License
The MIT License (MIT). Please see License File for more information.
๐ Credits
- Ibrahima Diallo
- Inspired by the original Intervention Image v2 implementation
Made with โค๏ธ for the PHP community