Open In App

PHP | Imagick setProgressMonitor() Function

Last Updated : 13 Dec, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




<?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;
}
?>


Output:

Callback is called

Program 2:




<?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;
}
  
?>


Output:

Callback function wasn't called.

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads