4

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!!

d3wannabe
  • 1,207
  • 2
  • 19
  • 39
  • 2
    Possible duplicate of: http://stackoverflow.com/questions/10035954/php-get-all-the-images-from-url-which-width-and-height-200-more-quicker – Vedant Terkar Mar 31 '16 at 12:32
  • 1
    thanks for the link Vedant. Reading through it I think you're suggesting I download the image locally first before running getimagesize? But I was hoping not to download anything until I knew it was an image for security reasons - that make sense? Let me know if I'm not understanding the point correctly – d3wannabe Mar 31 '16 at 12:37

2 Answers2

10

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 ) 
?>
Avinash Sinha
  • 534
  • 3
  • 16
  • but this returns nothing for the reuters link? – d3wannabe Mar 31 '16 at 12:36
  • Sorry, that does work perfectly - thanks so much - I don't know why, but for this URL the value I was passing to my PHP was getting trimmed after the ?m=02. Hmmmmm – d3wannabe Mar 31 '16 at 12:48
2

Use it

 list($width, $height) = getimagesize('path_to_image');

Make sure that:

  1. You specify the correct image path there
  2. The image has read access
  3. Chmod image dir to 755

Also try to prefix path with $_SERVER["DOCUMENT_ROOT"], this helps sometimes when you are not able to read files.

ref:Get Image Height and Width as integer values?

Bhargav Variya
  • 725
  • 1
  • 10
  • 18