dongzhi4470 2010-04-18 01:42
浏览 170
已采纳

快速远程PHP技术检测图像404

What PHP script technique runs the fastest in detecting if a remote image does not exist before I include the image? I mean, I don't want to download all the bytes of the remote image -- just enough to detect if it exists.

And while on the subject but with just a slight deviation, I'd like to download just enough bytes to determine a JPEG's width and height information.

Speed is very important in my concern here on this system design I'm working on.

  • 写回答

6条回答 默认 最新

  • dongsaolian8786 2011-01-05 17:29
    关注

    I've modified the @Volomike's code to get width too. Here you go...

    
    function get_image_dim($sURL) {
      // note that for jpeg you may need to change 300 to a larger value,
      // as some height/width info is farther out in the header
      try {
        $hSock = @ fopen($sURL, 'rb');
        if ($hSock) {
          while(!feof($hSock)) {
            $vData = fread($hSock, 300);
            break;
          }
          fclose($hSock);
          if (strpos(' ' . $vData, 'JFIF')>0) {
            $vData = substr($vData, 0, 300);
            $asResult = unpack('H*',$vData);        
            $sBytes = $asResult[1];
            $width = 0;
            $height = 0;
            $hex_width = '';
            $hex_height = '';
            if (strstr($sBytes, 'ffc2')) {
              $hex_height = substr($sBytes, strpos($sBytes, 'ffc2') + 10, 4);
              $hex_width = substr($sBytes, strpos($sBytes, 'ffc2') + 14, 4);
            } else {
              $hex_height = substr($sBytes, strpos($sBytes, 'ffc0') + 10, 4);
              $hex_width = substr($sBytes, strpos($sBytes, 'ffc0') + 14, 4);
            }
            $width = hexdec($hex_width);
            $height = hexdec($hex_height);
            return array('width' => $width, 'height' => $height);
          } elseif (strpos(' ' . $vData, 'GIF')>0) {
            $vData = substr($vData, 0, 300);
            $asResult = unpack('h*',$vData);
            $sBytes = $asResult[1];
            $sBytesH = substr($sBytes, 16, 4);
            $height = hexdec(strrev($sBytesH));
            $sBytesW = substr($sBytes, 12, 4);
            $width = hexdec(strrev($sBytesW));
            return array('width' => $width, 'height' => $height);
          } elseif (strpos(' ' . $vData, 'PNG')>0) {
            $vDataH = substr($vData, 22, 4);
            $asResult = unpack('n',$vDataH);
            $height = $asResult[1];        
            $vDataW = substr($vData, 18, 4);
            $asResult = unpack('n',$vDataW);
            $width = $asResult[1];        
            return array('width' => $width, 'height' => $height);
          }
        }
      } catch (Exception $e) {}
      return FALSE;
    }
    
    

    So, using it we have...

    
    // jpeg
    $url = 'http://upload.wikimedia.org/wikipedia/commons/thumb/c/ce/Quality_comparison_jpg_vs_saveforweb.jpg/250px-Quality_comparison_jpg_vs_saveforweb.jpg';
    // png
    //$url = 'http://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png';
    // gif
    //$url = 'http://upload.wikimedia.org/wikipedia/commons/e/e2/Sunflower_as_gif_small.gif';
    
    $dim = get_image_dim($url);
    print_r($dim);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装