Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if imagick throws an error - PHP

Tags:

php

imagick

I am currently using imagick for image processing on my webssite. I have it correctly installed and is working great. Heres my starting code -:

$image = new imagick($filename); $geo=$image->getImageGeometry();  
$image->setImageInterlaceScheme(2);  $image->setImageCompressionQuality(85);
$image->setImageBackgroundColor('white'); $image = $image->flattenImages(); 
$image->setImageFormat('jpg');   $image->stripImage();

After this i do the rest of the part. Now suppose it throws an error, how do i code for that. I tried adding if(!$image){ echo 'error' exit(); } after $image = new imagick($filename) but in vain.

Please help... Thanks all :)

like image 503
sanchitkhanna26 Avatar asked Apr 08 '13 19:04

sanchitkhanna26


People also ask

What does PHP imagick do?

Imagick is a PHP extension to create and modify images using the ImageMagick library. There is also a version of Imagick available for HHVM. Although the two extensions are mostly compatible in their API, and they both call the ImageMagick library, the two extensions are completely separate code-bases.

What is the use of imagick?

Use ImageMagick®to create, edit, compose, or convert digital images. It can read and write images in a variety of formats (over 200) including PNG, JPEG, GIF, WebP, HEIC, SVG, PDF, DPX, EXR and TIFF.

What is imagick module?

Imagick is a PHP extension which provides better image quality for media uploads, smarter image resizing (for smaller images) and PDF thumbnail support, when Ghost Script is also available.


1 Answers

If an error occurs, Imagick will throw an ImagickException which you can catch:

    try {
        $image = new Imagick($filename);
        $geo = $image->getImageGeometry();  
        $image->setImageInterlaceScheme(2);  
        $image->setImageCompressionQuality(85);
        $image->setImageBackgroundColor('white'); 
        $image = $image->flattenImages(); 
        $image->setImageFormat('jpg');   
        $image->stripImage();
    } catch (ImagickException $e) 
{
        var_dump($e);
    }

for further reference see here.

like image 104
user1909426 Avatar answered Oct 13 '22 01:10

user1909426