Page 1 of 1

Working Around Fatal Exceptions With IMagick

Posted: 2012-09-14T13:33:37-07:00
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

Re: Working Around Fatal Exceptions With IMagick

Posted: 2012-09-14T13:38:57-07:00
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.

Re: Working Around Fatal Exceptions With IMagick

Posted: 2012-09-14T13:44:41-07:00
by fmw42
I do not know if there is an Imagick equivalent. But see -regard-warnings at http://www.imagemagick.org/Usage/basics/#controls

Re: Working Around Fatal Exceptions With IMagick

Posted: 2012-09-14T13:55:47-07:00
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...

Re: Working Around Fatal Exceptions With IMagick

Posted: 2012-09-14T14:27:39-07:00
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.

Re: Working Around Fatal Exceptions With IMagick

Posted: 2012-09-14T14:35:49-07:00
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.