Open In App

PHP | Imagick getImageDistortion() Function

Last Updated : 21 Nov, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The Imagick::getImageDistortion() function is an inbuilt function in PHP which is used to compare an image with reconstructed image and returns the specified distortion metric. Syntax:
float Imagick::getImageDistortion(Imagick $reference, int $metric)
Parameters: This function accepts two parameters as mentioned above and described below:
  • $reference: It specifies the Imagick object which need to compare.
  • $metric: It specifies one of the metric type constants. List of METRIC constants are given below:
    • imagick::METRIC_UNDEFINED (0)
    • imagick::METRIC_MEANABSOLUTEERROR (1)
    • imagick::METRIC_MEANSQUAREERROR (2)
    • imagick::METRIC_PEAKABSOLUTEERROR (3)
    • imagick::METRIC_PEAKSIGNALTONOISERATIO (4)
    • imagick::METRIC_ROOTMEANSQUAREDERROR (5)
Exceptions: This function throws ImagickException on error. Return Value: This function returns the distortion metric used on the image. Below programs illustrate the Imagick::getImageDistortion() function in PHP: Program 1: php
<?php

// Create a new imagick object
$imagick1 = new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png');

$imagick2 = new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/20190918234528/colorize1.png');

// Get the distortion with METRIC constant
// as imagick::METRIC_ROOTMEANSQUAREDERROR
$distortion = $imagick1->getImageDistortion($imagick2, 5);

echo $distortion;
?>
Output:
0.97254902124405
Program 2: php
<?php

// Create a new imagick object
$imagick1 = new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png');

$imagick2 = new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/20190918234528/colorize1.png');

// Get the distortion with METRIC constant
// as imagick::METRIC_PEAKSIGNALTONOISERATIO
$distortion = $imagick1->getImageDistortion($imagick2, 4);

echo $distortion;
?>
Output:
0.020707619325613
Program 3: php
<?php

// Create a new imagick object
$imagick1 = new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png');

$imagick2 = new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png');

// Get the distortion with METRIC constant
// as imagick::METRIC_ROOTMEANSQUAREDERROR
$distortion = $imagick1->getImageDistortion($imagick2, 4);

echo $distortion;
?>
Output:
0 because both images are same.
Reference: https://www.php.net/manual/en/imagick.getimagedistortion.php

Similar Reads