PHP | Imagick setProgressMonitor() Function

The Imagick::setProgressMonitor() function is an inbuilt function in PHP which is used to set a callback function that will be called during the processing of the Imagick image if something goes wrong. You can use this function to pause a program before proceeding further.

Syntax:

bool Imagick::setProgressMonitor( callable $callback )

Parameters: This function accepts a single parameter $callback which holds the callback function.



Return Value: This function returns TRUE on success.

Exceptions: This function throws ImagickException on error.

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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new Imagick object
$imagick = new Imagick();
  
// Create a image in Imagick object
$imagick->newImage(640, 480, "blue");
  
$status = 'Not cancelled';
$text = '<br>';
  
// Callback function
$callback = function ($offset, $span) use (&$status, $text) {
    $status = "Callback is called" . $text;
    return false;
};
  
// Set the Progress Monitor
$imagick->setProgressMonitor($callback);
  
try {
    // $x and $y are undefined thus a call to
    // callback funcction is expected here
    $imagick->charcoalImage($x, $y);
    echo "Callback function wasn't called.";
} catch (\Exception $e) {
    echo $status;
}
?>

chevron_right


Output:

Callback is called

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new Imagick object
$imagick = new Imagick();
  
// Create a image in Imagick object
$imagick->newImage(600, 400, "white");
  
$status = 'Not cancelled';
$text = '<br>';
  
// Callback function
$callback = function ($offset, $span) use (&$status, $text) {
    $status = "Callback is called" . $text;
    return true;
};
  
// Set the Progress Monitor
$imagick->setProgressMonitor($callback);
  
try {
    // $x and $y are defined thus a call to
    // callback funcction is expected here
    $x = 20;
    $y = 5;
    $imagick->charcoalImage($x, $y);
    echo "Callback function wasn't called.";
} catch (\Exception $e) {
    echo $status;
}
  
?>

chevron_right


Output:

Callback function wasn't called.

Reference: https://www.php.net/manual/en/imagick.setprogressmonitor.php




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.