cleberkr
cleberkr

Reputation: 1

PHP function GETIMAGESIZE timeout in 60 seconds to check remote files

I'm using getimagesize to check if an image exists or not.

The image is in a remote URL, so i check a link.

If the image exists, the response is given in less then 2 seconds.

If the image doesn't exists e also there is no link of image error, the response is given in less then 2 seconds.

The problem is when the image doesn't exists and there is a link saying (image not found) or something like that.... the getimagesize keeps trying to locate the image for exactly 60 seconds ( i checked with php microtime ).

Other methods also happens the same thing, takes 60 seconds for response... i've tryed with curl, with file_get_contens, get_headers, imagecreatefromjpeg.... all of them take 60 seconds to return false.

Any idea how to reduce that time?

Upvotes: 0

Views: 2521

Answers (1)

Davide
Davide

Reputation: 1703

Try to use this function with CURLOPT_TIMEOUT:

function checkRemoteFile($url)
{
  $timeout = 5; //timeout seconds

  $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);
  curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);

  return (curl_exec($ch)!==FALSE);
}


$image = "/img/image.jpg";

if ( checkRemoteFile($image) )
{
  $info = getimagesize($image);
  print_r($info); //Print image info
  list($width, $height, $type, $attr) = $info; //Store image info
}
else
  echo "Timeout";

You can also use CURLOPT_CONNECTTIMEOUT that is a little different.

Hope it can be helpful. :)

Upvotes: 1

Related Questions