2011-09-20 52 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 –

回答