Wednesday, June 10, 2026
HomeLanguagesPHP | Imagick setProgressMonitor() Function

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:




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

RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7018 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS