dsa88886666 2014-08-23 12:44
浏览 279

PHP函数GETIMAGESIZE在60秒内超时检查远程文件

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?

  • 写回答

1条回答 默认 最新

  • douchui3933 2014-11-16 13:36
    关注

    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. :)

    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端