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

v1.0.1 2025-12-10 17:29 UTC

This package is auto-updated.

Last update: 2025-12-10 22:28:50 UTC


README

Latest Version on Packagist Tests PHP Version License

๐ŸŽญ Restore the beloved mask() and opacity() 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 invisible
  • 0.5 โ†’ 50% transparent
  • 1.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