如何在getimagesize()中处理超时

I am trying to find image/pixel size for an image before I use it. I see the following warning for an image. Warning: getimagesize(http://farm4.hv-static.flickr.com/3052/2330936325_ea1ddf9e7d_m.jpg): failed to open stream: Connection timed out
Any idea how one can handle this? I checked the manual but didnt find any option.

getimagesize returns FALSE if retrieving is failed. you can disable warnings output if you don't want to see them by

error_reporting(0);

As its manual page says :

If accessing the filename image is impossible, or if it isn't a valid picture, getimagesize() will generate an error of level E_WARNING.
On read error, getimagesize() will generate an error of level E_NOTICE.

There is not much you can do about that, except making sure the error message is not displayed to your users.


This can be done changing the display_errors directive:

  • in your php.ini file, for all your pages (good idea : it's never great to display errors on a production server)
  • or using ini_set('display_errors', 'off'); at the beginning of your script.


Or using the @ operator, to silence the error :

$result = @getimagesize(...);

Note : this is generally not quite a good idea : you'll totally loose the indication that an error occured !
It's better to not display the error, but still have it logged...