Handle error when getimagesize can't find a file

前端 未结 4 462
挽巷
挽巷 2021-02-05 06:53

when I\'m trying to getimagesize($img) and the image doesn\'t exist, I get an error. I don\'t want to first check whether the file exists, just handle the error.

4条回答
  •  一个人的身影
    2021-02-05 07:14

    I'm sorry that raise such old topic. Recently encountered a similar problem and found this topic instead a solution. For religious reasons I think that '@' is bad decision. And then I found another solution, it looks something like this:

    function exception_error_handler( $errno, $errstr, $errfile, $errline ) {
        throw new Exception($errstr);
    }
    set_error_handler("exception_error_handler");
    
    try {
        $imageinfo = getimagesize($image_url);
    } catch (Exception $e) {
        $imageinfo = false;
    }
    

提交回复
热议问题