Handle error when getimagesize can't find a file

前端 未结 4 443
挽巷
挽巷 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:06

    Call me a dirty hacker zombie who will be going to hell, but I usually get around this problem by catching the warning output into an output buffer, and then checking the buffer. Try this:

    ob_start();
    $data = getimagesize('not-existing.png');
    $resize_warning = ob_get_clean();
    if(!empty($resize_warning)) {
      print "NOT OK";
      # We could even print out the warning here, just as PHP would do
      print "$resize_warning";
    } else {
      print "OK"
    }
    

    Like I said, not the way to get a cozy place in programmer's heaven, but when it comes to dysfunctional error handling, a man has to do what a man has to do.

    0 讨论(0)
  • 2021-02-05 07:07

    This solution has worked for me.

    try {
        if (url_exists ($photoUrl) && is_array (getimagesize ($photoUrl)))
        {
            return $photoUrl;
        }
    } catch (\Exception $e) { return ''; }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-02-05 07:22

    Like you said, if used on a non-existing file, getimagesize generates a warning :

    This code :

    if ($data = getimagesize('not-existing.png')) {
        echo "OK";
    } else {
        echo "NOT OK";
    }
    

    will get you a

    Warning: getimagesize(not-existing.png) [function.getimagesize]: 
      failed to open stream: No such file or directory 
    


    A solution would be to use the @ operator, to mask that error :

    if ($data = @getimagesize('not-existing.png')) {
        echo "OK";
    } else {
        echo "NOT OK";
    }
    

    As the file doesn't exist, $data will still be false ; but no warning will be displayed.


    Another solution would be to check if the file exists, before using getimagesize ; something like this would do :

    if (file_exists('not-existing.png') && 
        ($data = getimagesize('not-existing.png'))
       ) {
        echo "OK";
    } else {
        echo "NOT OK";
    }
    

    If the file doesn't exist, getimagesize is not called -- which means no warning

    Still, this solution is not the one you should use for images that are on another server, and accessed via HTTP (if you are in this case), as it'll mean two requests to the remote server.

    For local images, that would be quite OK, I suppose ; only problem I see is the notice generated when there is a read error not being masked.


    Finally :

    • I would allow errors to be displayed on your developpement server,
    • And would not display those on your production server -- see display_errors, about that ;-)
    0 讨论(0)
提交回复
热议问题