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:



  • $evaluation_op: It holds the evaluation operator like add, subtracts, divides, mod, etc.
  • $constant: It holds the value of the operator with the evaluation will take place.
  • $channel: It provides any channel constant that is appropriate for required channel mode.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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();  
    
?>  

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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');
   
?>

chevron_right


Output:

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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();  
   
?>

chevron_right


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!!



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.