Working Around Fatal Exceptions With IMagick

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
merrydown
Posts: 4
Joined: 2012-09-14T13:28:27-07:00
Authentication code: 67789

Working Around Fatal Exceptions With IMagick

Post by merrydown »

I am writing an application which handles and processes several filetypes eventually sandwiching three together. The complete application uses a combination of GD and imagemagick/Imagick, but it is the Imagick portion of the code which is causing the issue. I am having a problem with fatal errors when an image/file is corrupt.

For example, here's a portion which converts a pdf to png. However if the pdf isn't understood it causes a fatal exception:
----------------------------------------
if(strtolower(end(explode('.',$_SESSION['FirstImage'])))=="pdf"){
if(!file_exists("fileUpload/server/php/files/".(basename($_SESSION['FirstImage'],".pdf")).".png")){
$myurl = "fileUpload/server/php/files/".$_SESSION['FirstImage'];
$image = new Imagick();
$image->setResolution( 300, 300 );
$image->readImage($myurl."[0]");
$image->setImageFormat( "png" );
$image->writeImage("fileUpload/server/php/files/".(basename($_SESSION['FirstImage'],".pdf")).".png");
$_SESSION['FirstImage']=(basename($_SESSION['FirstImage'],".pdf")).".png";
}
-------------------------------------------
All I want to do is either test the pdf to see if it is valid before processing, or ignore the fatal error and give the session variable a fixed error image to work on if the pdf cant be processed.

I can't find a way around this, can someone shed some light for a tired and frustrated hack please :)

Many thanks,

Jim
Last edited by merrydown on 2012-09-14T13:39:56-07:00, edited 1 time in total.
merrydown
Posts: 4
Joined: 2012-09-14T13:28:27-07:00
Authentication code: 67789

Re: Working Around Fatal Exceptions With IMagick

Post by merrydown »

Sure, there is no problem with the imagemagick/imagick installation or the GD installation. I have edited the OP in case this wasn't made clear.

It all works fine when the file is a valid pdf.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Working Around Fatal Exceptions With IMagick

Post by fmw42 »

I do not know if there is an Imagick equivalent. But see -regard-warnings at http://www.imagemagick.org/Usage/basics/#controls
merrydown
Posts: 4
Joined: 2012-09-14T13:28:27-07:00
Authentication code: 67789

Re: Working Around Fatal Exceptions With IMagick

Post by merrydown »

Thanks for the reply. From what I read though, that makes even more warnings fatal. I can't find a way to catch the error and avert fatality...

I have been known to be slow on the uptake - but I've tried try and catch to try to avert disaster but with no dice. I'd be happy to simply validate the pdf before attempting conversion, but can't find a way of doing that either...

I guess I might benefit from finding if identifyImage only throws a handleable exception...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Working Around Fatal Exceptions With IMagick

Post by fmw42 »

using -regard-warnings -quiet should return a true/false that can be caught by an if test, though I have not tested with an image that is so corrupt as to be unreadable.

I use that in all my scripts to send a user readable error message to the user rather than the full error message from IM.
merrydown
Posts: 4
Joined: 2012-09-14T13:28:27-07:00
Authentication code: 67789

Re: Working Around Fatal Exceptions With IMagick

Post by merrydown »

That sounds interesting, thanks! I'll give it a whirl. Am also looking into exec( to command line it so I can use imagemagick's identify command to validate the file without trying to process it.
Post Reply