Open In App

PHP | Imagick evaluateImage() Function

The Imagick::evaluateImage() function is an inbuilt function in PHP which is used to apply an expression to the image. It can apply any arithmetic, logical or relational expression to an image and evaluate its result. This operator has many uses ranging from changing the brightness of an image i.e. lighten or darken the image, or changing the contrast of the image or to produce the negation of the image.

Syntax:



bool Imagick::evaluateImage( $evaluation_op, $constant, $channel = Imagick::CHANNEL_DEFAULT )

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

Return Value: It returns Boolean value true if function is successfully executed otherwise returns false.



Below programs illustrate the Imagick::evaluateImage() function in PHP:

Program 1:




<?php  
     
// Create an Imagick object  
$imagick = new Imagick(  
  
// Declare and initialize the value of variable
$evaluation_op = Imagick::EVALUATE_DIVIDE;
$constant = 3;
$channel = Imagick::CHANNEL_ALPHA;
  
// Use evaluateImage function  
$imagick->evaluateImage($evaluation_op, $constant, $channel);  
     
header("Content-Type: image/jpg");  
     
// Display the output image  
echo $imagick->getImageBlob();  
    
?>  

Output:

Program 2:




<?php
   
// Creating new Imagick object
 $image = new Imagick(__DIR__ . '\sample.png');
   
// Set the Alpha Channel to Opaque to facilitate Opaque operation
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_OPAQUE);
   
// Set the values of parameters 
$evaluation_op = Imagick::EVALUATE_DIVIDE;
$constant = 3;
$channel = Imagick::CHANNEL_ALPHA;
   
// Calling the function with the parameters
$image->evaluateImage( $evaluation_op, $constant, $channel );
   
header('Content-type: image/jpeg'); 
   
// Writing the new image to specified directory
$image->writeImage(__DIR__ . '\sample_with_33perc_opacity.png');
   
?>

Output:

Program 3:




<?php
  
// Function definition
function Evaluate_Image( $image, $evaluation_op, $constant, $channel )
{
    $image->evaluateImage( $evaluation_op, $constant, $channel );
      
    return $image;
}
   
// Set values of the parameters 
$final_image = Evaluate_Image(new Imagick(__DIR__ . '\sample.png'),
        Imagick::EVALUATE_MEAN, 3, Imagick::CHANNEL_BLUE);
  
header('Content-type: image/jpeg');
  
// Display the output image  
echo $final_image->getImageBlob();  
   
?>

Output:

Reference: https://www.php.net/manual/en/imagick.evaluateimage.php
Please comment if you find something wrong or want to add some more information. Happy coding!!


Article Tags :