PHP Detecting if source image url link leads to a “broken” image?

前端 未结 8 1586
迷失自我
迷失自我 2020-12-30 03:56

Suppose you have a thumbnail generator script that accepts source images in the form of a URL. Is there a way to detect if the source URL is \"broken\" - whether nonexistent

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-30 04:33

    I have used the following to detect attributes for remote images

    $src='http://example.com/image.jpg';
    list($width, $height, $type, $attr) = @getimagesize($src);
    

    example (checking stackoverflows "Careers 2.0" image)

    $src='http://sstatic.net/ads/img/careers2-ad-header-so.png';
    list($width, $height, $type, $attr) = @getimagesize($src);
    
    echo '
    ';
    echo $width.'
    '; echo $height.'
    '; echo $type.'
    '; echo $attr.'
    '; echo '
    ';

    If $height, $width etc is null the image is obvious not an image or the file does not exists. Using cURL is overkill and slower (even with CURLOPT_HEADER)

提交回复
热议问题