Open In App

PHP | ImagickPixel isPixelSimilarQuantum() function

Improve
Improve
Like Article
Like
Save
Share
Report

The ImagickPixel::isPixelSimilarQuantum() function is an inbuilt function in PHP which is used to check if the distance between two colors is less than the specified distance. The fuzz value should be in the range 0-65535. This function is different from isPixelSimilar() because it accepts the fuzz in quantum range and actually compares a pixel with a color, not with another pixel.

Syntax:

bool ImagickPixel::isPixelSimilarQuantum( string $color, string $fuzz )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $color: It specifies the color to compare to.
  • $fuzz: It specifies the fuzz value.

Return Value: This function returns an bool value which tells whether it is similar (true) or not (false).

Exceptions: This function throws ImagickException on error.

Below given programs illustrate the ImagickPixel::isPixelSimilarQuantum() function in PHP:

Program 1:




<?php
// Create a new imagick object
$imagickPixel = new ImagickPixel('green');
  
// Check if the pixel color is green
$isSimilar = $imagickPixel->isPixelSimilarQuantum('green', 10);
  
if ($isSimilar) {
    echo 'Similar';
} else {
    echo 'Not Similar';
}
?>


Output:

Similar

Program 2:




<?php
// Create a new imagick object
$imagick = new Imagick(
  
// Get the image histogram
$histogramElements = $imagick->getImageHistogram();
  
// Get the 501th pixel
$imagickPixel1 = $histogramElements[500];
  
// Check if the pixel color is red
$isSimilar = $imagickPixel1->isPixelSimilarQuantum('red', 10);
  
if ($isSimilar) {
    echo 'Similar';
} else {
    echo 'Not Similar';
}
?>


Output:

Not Similar

Reference: https://www.php.net/manual/en/imagickpixel.ispixelsimilarquantum.php



Last Updated : 16 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads