How to handle Imagine exception in CakePHP 3

214 views Asked by At

I suppose I don't manage correctly the exception using Imagine libray.

My code is:

use ....
use Imagine\Exception;
....

try {

    $imagine = new Imagine();

    $image = $imagine->open($img_path . DS . "tmpfile." . $extension)
        ->resize(new Box($cwidth, $cheight))
        ->crop(new Point($offsetx, $offsety), new Box(500, 500));

    ...

} catch (Imagine\Exception\Exception $e) {

    die("catch Imagine\Exception\Exception");
    $file = new File($img_path . DS . "tmpfile." . $extension);
    if ($file->exists()) {
        $file->delete();
    }

}

but on Imagine Exception, I don't catch it and my script stops.

Where is my mistake?

1

There are 1 answers

0
ndm On BEST ANSWER

You are using a qualified name, causing it to resolve with respect to the current namespace, ie Imagine\Exception\Exception will resolve to \CurrentNamespace\Imagine\Exception\Exception, and since that doesn't exist, you're not catching anything.

Either use the imported namespace, which is Exception, ie Exception\Exception, which will resolve to \Imagine\Exception\Exception, or use a proper fully qualified name, that is, a name starting with a \, ie \Imagine\Exception\Exception.

See also PHP Manual > Language Reference > Namespaces > Using namespaces: Basics