Image Hasher
Generate Image Hashes with Standalone Image Hasher
2 Downloads / Month
Open Source MIT License
2 Downloads / Month
Open Source MIT License
Learn how to create perceptual image hashes using the ImageHasher class.
Intervention ImageHash offers two ways to generate perceptual image hashes: use the ImageHasher class as a standalone tool, or integrate hashing into an existing Intervention Image workflow using the analyzer interface.
The ImageHasher class provides several static factory methods for creating instances.
public ImageHasher::__construct(string|DriverInterface $driver, StrategyInterface $strategy = new Difference())
The ImageHasher class is your starting point for all hashing operations. You need a driver that matches your PHP image extension (GD, Imagick, or libvips) and a hashing strategy.
| Name | Type | Description |
|---|---|---|
| driver | string or DriverInterface | Image manipulation driver (GD, Imagick, or libvips) |
| strategy | StrategyInterface | Hashing strategy to use (defaults to Difference) |
use Intervention\Image\Drivers\Gd\Driver as GdDriver; use Intervention\ImageHash\ImageHasher; use Intervention\ImageHash\Strategies\Difference; // create hasher with driver and strategy $hasher = new ImageHasher(new GdDriver(), new Difference()); // generate hash from image path $hash = $hasher->hash('path/to/image.jpg');
public static ImageHasher::create(string|DriverInterface $driver, StrategyInterface $strategy = new Difference()): ImageHasher
Create a new hasher instance with the given driver and strategy.
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver; use Intervention\ImageHash\ImageHasher; use Intervention\ImageHash\Strategies\Average; // create hasher using static method $hasher = ImageHasher::create(ImagickDriver::class, new Average());
public static ImageHasher::usingDriver(string|DriverInterface $driver): ImageHasher
Create a hasher instance with the specified driver using the default Difference strategy.
use Intervention\Image\Drivers\Gd\Driver as GdDriver; use Intervention\ImageHash\ImageHasher; // create hasher with driver (uses default Difference strategy) $hasher = ImageHasher::usingDriver(GdDriver::class);
You can create new hasher instances from existing ones with modified configuration.
public ImageHasher::withDriver(string|DriverInterface $driver): ImageHasher
Create a new hasher instance with a different driver, keeping the current strategy.
use Intervention\Image\Drivers\Gd\Driver as GdDriver; use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver; $hasher = ImageHasher::usingDriver(GdDriver::class); // create new hasher with different driver $imagickHasher = $hasher->withDriver(ImagickDriver::class);
public ImageHasher::withStrategy(StrategyInterface $strategy): ImageHasher
Create a new hasher instance with a different strategy, keeping the current driver.
use Intervention\ImageHash\Strategies\Difference; use Intervention\ImageHash\Strategies\Perceptual; $hasher = ImageHasher::usingDriver(GdDriver::class); // create new hasher with different strategy $perceptualHasher = $hasher->withStrategy(new Perceptual());
public ImageHasher::hash(mixed $image): HashInterface
Generate a perceptual hash from various image sources. This method accepts the same image sources as Intervention Image's decode() method.
| Name | Type | Description |
|---|---|---|
| image | mixed | Image source (path, binary data, SplFileInfo, base64, data URI, stream, ImageInterface, etc.) |
The hash() method accepts all image sources supported by Intervention Image:
SplFileInfo objectDataUriInterfaceImageInterfaceEncodedImageInterfaceuse Intervention\Image\Drivers\Gd\Driver as GdDriver; use Intervention\ImageHash\ImageHasher; use Intervention\ImageHash\Strategies\Difference; $hasher = new ImageHasher(new GdDriver(), new Difference()); // hash from file path $hash1 = $hasher->hash('images/photo.jpg'); // hash from binary data $hash2 = $hasher->hash(file_get_contents('images/photo.jpg')); // hash from data URI $hash3 = $hasher->hash('data:image/png;base64,iVBORw0KG...'); // hash from stream resource $stream = fopen('images/photo.jpg', 'r'); $hash4 = $hasher->hash($stream);