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

前端 未结 8 1541
迷失自我
迷失自我 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 '<pre>';
    echo $width.'<br>';
    echo $height.'<br>';
    echo $type.'<br>';
    echo $attr.'<br>';
    echo '</pre>';
    

    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)

    0 讨论(0)
  • 2020-12-30 04:34

    onerror Event

    Execute a JavaScript if an error occurs when loading an image:

    The onerror event is triggered if an error occurs while loading an external file (e.g. a document or an image).

    Example:

    <!DOCTYPE html>
    <html>
    <body>
    
    <img src="image.gif" onerror="myFunction()">
    
    <p>A function is triggered if an error occurs when loading the image. The function shows an alert box with a text.
    In this example we refer to an image that does not exist, therefore the onerror event occurs.</p>
    
    <script>
    function myFunction() {
      alert('The image could not be loaded.');
    }
    </script>
    
    </body>
    </html>

    0 讨论(0)
  • 2020-12-30 04:35

    The only really reliable way is to request the image using file_get_contents(), and finding out its image type using getimagesize().

    Only if getimagesize() returns a valid file type, can you rely that it is in fact a valid image.

    This is quite resource heavy, though.

    You could consider not doing any server-side checks at all, and adding an onerror JavaScript event to the finished image resource:

    <img src="..." onerror="this.style.display = 'none'">
    
    0 讨论(0)
  • 2020-12-30 04:37

    Fast Solution for broken or not found images link
    i suggest you that don't use getimagesize() because it will 1st download image then it will check images size+if this will not image then it will throw exception so use below code

    if(checkRemoteFile($imgurl))
    {
    //found url, its mean
    echo "this is image";
    }
    
    function checkRemoteFile($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        // don't download content
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if(curl_exec($ch)!==FALSE)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    

    Note: this current code help you to identify broken or not found url image this will not help you to identify image type or headers

    0 讨论(0)
  • 2020-12-30 04:38

    did you try file_get_contents() method?

    http://php.net/manual/en/function.file-get-contents.php

    0 讨论(0)
  • 2020-12-30 04:39

    use file_exists function in php, you can check urls with it.

    See documentation below, shows how to check img... exactly what you need

    FILE EXISTS - http://www.php.net/manual/en/function.file-exists.php#93572

    URL EXISTS - http://www.php.net/manual/en/function.file-exists.php#85246


    Here is alternative code for checking the url. If you will test in browser replace \n with <br/>

    <?php
    
    $urls = array('http://www.google.com/images/logos/ps_logo2.png', 'http://www.google.com/images/logos/ps_logo2_not_exists.png');
    
    foreach($urls as $url){
       echo "$url - ";
       echo url_exists($url) ? "Exists" : 'Not Exists';
       echo "\n\n";
    }
    
    
    function url_exists($url) {
        $hdrs = @get_headers($url);
    
        echo @$hdrs[1]."\n";
    
        return is_array($hdrs) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/',$hdrs[0]) : false;
    }
    ?>
    

    Output is as follows

    http://www.google.com/images/logos/ps_logo2.png - Content-Type: image/png
    Exists
    
    http://www.google.com/images/logos/ps_logo2_not_exists.png - Content-Type: text/html; charset=UTF-8
    Not Exists
    
    0 讨论(0)
提交回复
热议问题