RGS
RGS

Reputation: 4253

Function to check if website is an image

I have a function to check if url is an image or not. The problem is that when a user posted a gif image it didn't work. It shows an X instead of the image. The website is in html but it gets it as a image/gif.

any ideas?

what user posted: https://media.giphy.com/media/CwbErAWpTQDxS/giphy.gif it is an html page not a gif.

my function:

function isImage($url){
$url_headers=get_headers($url, 1);

if($url_headers[0] == 'HTTP/1.1 404 Not Found') {
   $url_exists = false;
} 
else {
    $url_exists = true;
}

if($url_exists){
    if(isset($url_headers['Content-Type'])){
        $type=strtolower($url_headers['Content-Type']);

        $valid_image_type=array();
        $valid_image_type['image/png']='';
        $valid_image_type['image/jpg']='';
        $valid_image_type['image/jpeg']='';
        $valid_image_type['image/jpe']='';
        $valid_image_type['image/gif']='';

        if(isset($valid_image_type[$type])){
            return true;
        }
        else{
            return false;
        }
    }
}
}

Upvotes: 2

Views: 92

Answers (1)

Tim G
Tim G

Reputation: 1822

I believe giphy is sniffing the http header "accept" - or perhaps the user agent string. In any case, when I used a tool to show headers for that url I see "image/gif", but chrome inspector reveals a web page being delivered with text/html as content type.

giphy has a web page talking about how to embed. There may be a way around this with ajax, but that's out of scope for your question. As is, it appears that giphy does not want hotlinking.

Giphy Article on Embedding Images

Upvotes: 3

Related Questions