2011-09-20 47 views
0

我写了一个PHP脚本,可以让我远程托管的JPG的尺寸(宽度和高度),而无需下载它在满(只是第10K)。获取从部分提取JPG尺寸没有写入磁盘

的问题,这是我写的部分下载到文件,然后读取该文件(使用getImageSize)提取我需要的信息。

我知道这可以下来,而不写入磁盘,但我不知道如何。

任何人有建议/解决方案?

这里是我的原代码:

function remoteImage($url){ 
    $ch = curl_init ($url); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); 
    curl_setopt($ch, CURLOPT_RANGE, "0-10240"); 

    $fn = "partial.jpg"; 
    $raw = curl_exec($ch); 
    $result = array(); 

    if(file_exists($fn)){ 
     unlink($fn); 
    } 

    if ($raw !== false) { 

     $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 

     if ($status == 200 || $status == 206) { 

      $result["w"] = 0; 
      $result["h"] = 0; 

      $fp = fopen($fn, 'x'); 
      fwrite($fp, $raw); 
      fclose($fp); 

      $size = getImageSize($fn); 

      if ($size===false) { 
      // Cannot get file size information 
      } else { 
      // Return width and height 
       list($result["w"], $result["h"]) = $size; 
      } 

     } 
    } 

    curl_close ($ch); 
    return $result; 
} 

我原来的问题,从而导致这一点,是here - and might be helpful

+0

假如你尝试过这个人的解决方案? http://www.php.net/manual/en/function.getimagesize.php#88793 –

回答