How to know if imagick throws an error - PHP

╄→尐↘猪︶ㄣ 提交于 2019-12-23 10:08:37

问题


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 :)


回答1:


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.



来源:https://stackoverflow.com/questions/15887228/how-to-know-if-imagick-throws-an-error-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!