Working Around Fatal Exceptions With IMagick
Posted: 2012-09-14T13:33:37-07:00
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
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