PHP's getimagesize not detecting image from URL

d3wannabe picture d3wannabe · Mar 31, 2016 · Viewed 11.4k times · Source

I'm using the following code to quickly verify whether a link is an image or not...

if (getimagesize($imageLink)) {
} else {
    echo "notImage";
    exit();
}

It works in practically all cases (i.e. if the browser returns an image, then getimagesize will return something), but I've found some cases where it doesn't work - e.g. for this link...

http://s4.reutersmedia.net/resources/r/?m=02&d=20160330&t=2&i=1128905435&w=&fh=545px&fw=&ll=&pl=&sq=&r=LYNXNPEC2T0YW

This applies generally to the reuters website and I'm sure a few others, but I'm really struggling to understand why there's this problem since the browser manages to return an image. I originally thought that getimagesize needed the path to the file explicitly stated (e.g. ending in .jpg, .png etc), but then again a link like this works fine...

https://d2sh4fq2xsdeg9.cloudfront.net/contentAsset/image/f9b79b5e-1986-4376-b9ed-0b153d6deb14/image/byInode/1/filter/Resize,Jpeg/jpeg_q/69/resize_w/434

Would really appreciate any thoughts from anyone having any ideas why getimagesize doesn't return an image in the first example but does in the second - and any suggestions on how to adapt the code to account for the reuters example would also be fantastic!!

Answer

Avinash Sinha picture Avinash Sinha · Mar 31, 2016

use as it

<?php 
list($width, $height) = getimagesize("http://s4.reutersmedia.net/resources/r/?m=02&d=20160330&t=2&i=1128905435&w=&fh=545px&fw=&ll=&pl=&sq=&r=LYNXNPEC2T0YW"); 
$arr = array('h' => $height, 'w' => $width );
print_r($arr); //output - Array ( [h] => 545 [w] => 968 ) 
?>