Wednesday, November 20, 2024
Google search engine
HomeLanguagesPHP | ImagickPixel isSimilar() function

PHP | ImagickPixel isSimilar() function

The ImagickPixel::isSimilar() function is an inbuilt function in PHP which is used to check the distance between the color described by this ImagickPixel object and that of the provided object, by plotting their RGB values on the color cube. If the distance between the two points is less than the fuzz value given, the colors are similar.

Syntax:

bool ImagickPixel::isSimilar( ImagickPixel $color, float $fuzz )

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

  • $color: It specifies the pixel containing the color to be compared with.
  • $fuzz: It specifies the fuzz value which tells the maximum distance within which to consider these colors as similar.

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

Exceptions: This function throws ImagickException on error.

Below given programs illustrate the ImagickPixel::isSimilar() function in PHP:
Program 1:




<?php
// Create a new imagickPixel object
$imagickPixel1 = new ImagickPixel('cyan');
   
// Create another new imagickPixel object
$imagickPixel2 = new ImagickPixel('pink');
   
// Check if similar or not
$isSimilar = $imagickPixel1->isSimilar($imagickPixel2, 0.1);
   
if($isSimilar) {
    echo 'Similar';
} else {
    echo 'Not Similar';
}
?>


Output:

Not Similar

Program 2:




<?php
// Create two new imagickPixel objects with same color
$imagickPixel1 = new ImagickPixel('orange');
$imagickPixel2 = new ImagickPixel('orange');
  
// Check if similar
$isSimilar = $imagickPixel1->isSimilar($imagickPixel2, 30);
  
if($isSimilar) {
    echo 'Similar';
} else {
    echo 'Not Similar';
}
?>


Output:

Similar

Program 3:




<?php
// Create a new imagick object
$imagick = new Imagick(
  
// Get the image histogram
$histogramElements = $imagick->getImageHistogram();
  
// Get the 1001th pixel
$imagickPixel1 = $histogramElements[1000];
  
// Get the 2001th pixel
$imagickPixel2 = $histogramElements[2000];
  
// Check if both pixels are similar
$isSimilar = $imagickPixel1->isSimilar($imagickPixel2, 400);
  
if ($isSimilar) {
    echo 'Similar';
} else {
    echo 'Not Similar';
}
?>


Output:

Not Similar

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments