Link to home
Start Free TrialLog in
Avatar of Ludwig Diehl
Ludwig DiehlFlag for Peru

asked on

Imagick Fatal error

I am trying to open a corrupted pdf file (on purpose) so I can check if it is ok or not, however I get this error:
Fatal error: Uncaught ImagickException: unable to open image `asasdasd': No such file or directory @ error/blob.c/OpenBlob/2853 in ....... Stack trace: #0 ...: Imagick->readimage('asasdasd') #1 ...: Validator\Validators\PdfValidator->isValid() #2 {main} thrown in... on line 14

Open in new window


instead of and exception.
I am using php 7.2 and imagick 3.4.3

Does anyone have an idea to solve this?
SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Arana (G.P.)
Arana (G.P.)

Are you able to open a non corrupt PDF using the same code?
 
Also imagisk uses ghostscript, are ghostscript bins accessible to apache?, check the apache environment paths
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ludwig Diehl

ASKER

THank you everyone for the comments. Unfortunately I cannot solve this yet. It is not possible to catch this fatal exception using try catch. I also tried using Throwabe instead of Exception to try to catch all possible exceptions / Errors but had no luck.
This is my imagick module information:

imagick

imagick module => enabled
imagick module version => 3.4.3
imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
Imagick compiled with ImageMagick version => ImageMagick 6.9.9-50 Q16 x86_64 2018-06-04 https://www.imagemagick.org
Imagick using ImageMagick library version => ImageMagick 6.9.10-28 Q16 x86_64 2019-02-18 https://imagemagick.org
ImageMagick copyright => © 1999-2019 ImageMagick Studio LLC
ImageMagick release date => 2019-02-18

Open in new window


are ghostscript bins accessible to apache?
is it reale necessary?. I mean I can open pdf files without problema, the thing is that I want to know if a given pdf file is corrupted using php and imagick
You asked, "... I want to know if a given pdf file is corrupted using php and imagick".

If this is what you're trying to determine, ImageMagick is a poor tool for this type of check, because any file corruption will crash ImageMagick.

Make a call to the system file command like this...

imac> file download.pdf
download.pdf: PDF document, version 1.7

Open in new window


If you get an error return from file or some other response different than PDF document then you know you have a corrupted PDF file.
Thank you everyone for your Help. I was finally able to get it working. The problem was the namespace. I didn't add "\" at the beginning of the Exception Class at the catch block.
Now it works even with corrupted files, just wondering if readImage is the fastest method to do it?.



try
        {
            $img = new \Imagick();
            $img->readImage($this->fileName);
            $this->isValid = $img->valid();
        }
        catch(\ImagickException $e)
        {            
        }
       catch(\Exception $e)
        {            
        }
        finally
        {
            if($img)
            {
                $img->clear();
            }
        }
@Ludwig
     are ghostscript bins accessible to apache?

is it reale necessary?. I mean I can open pdf files without problema, the thing is that I want to know if a given pdf file is corrupted using php and imagick


Well I asked before if you were able to open non corrupt pdf files, and since you didn't confirm you could, then maybe was a ghostscript path problem.
Better to use http://php.net/manual/en/ref.fileinfo.php for this, as this is the interface to the OS level magic number system.

If you only have the occasional file to check, then ImageMagick is fine to use.

If you have many repeated tests, then fileinfo() is very light weight, compared to ImageMagick.
I thought of fileinfo however it does not check file consistency.