Imagick will not catch Notices or Warnings

When a Notice or Warning occurs, the image fails. However, I am unable to catch the notice or warning.

<?php

    $image = new Imagick($resource);

    try {
        $image->setImageCompressionQuality("Should be a Number Here not String");
    }
    catch ( ImagickException $e ) {
        echo "This is a catch"; // should catch here but nope!
    }
?>

The code above should catch because of the string passed when should be INT. The image fails but catch does not execute. I still get this message:

Notice: Use of undefined constant Should be a Number Here not String - assumed 'd' in /var/www/class.php on line 15 Warning: Imagick::setimagecompressionquality() expects parameter 1 to be long, string given in /var/www/class.php on line 15

I also tried ( Exception $e )

Upvotes: 0

Views: 512

Answers (2)

Vavilen T
Vavilen T

Reputation: 87

As said above, you should also check return value of $image->setImageCompressionQuality and can hide notices with @.

But also you can convert notices in your code to exceptions like described in this post. It is interesting solution, but i dont recommend to follow it. Checking correctness is better.

Upvotes: 1

motanelu
motanelu

Reputation: 4025

Because the method doesn't throw an exception in case on invalid input. You should do something like:

$result = @$image->setImageCompressionQuality("Should be a Number Here not String");
if (!$result) {
    throw new \Exception('Operation has failed');
}

Upvotes: 1

Related Questions