2012-06-09 21 views
0

我發現下面的函數搜索SOPHP中使用捲曲,但驗證它首先

function grab_image($url,$saveto){ 
    $ch = curl_init ($url); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); 
    $raw=curl_exec($ch); 
    curl_close ($ch); 
    if(file_exists($saveto)){ 
     unlink($saveto); 
    } 
    $fp = fopen($saveto,'x'); 
    fwrite($fp, $raw); 
    fclose($fp); 
} 

這應該保存圖像,但我怎麼能檢查圖像dimensionssizefiletype一是爲了節省圖像從網址驗證其有效?

回答

0

你火捲曲之前,你可以直接讀取使用

$url = 'http://exmaple.com/img/some.png'; 
$imgInfo = getimagesize($url); 
print_r($imgInfo); 
+0

但我如何獲取其文件大小?它是多少kb? – fxuser

+0

在捲曲設置中,將CURLOPT_HEADER設置爲true,以便標題具有Content-Length: –

2

這樣的事情?

$raw = curl_exec($ch); 
if ($raw === FALSE) { 
    die("Fetch failed"); 
} 

// get size 
$size = strlen($raw); 

// get dimensions 
$img = imagecreatefromstring($raw); 
$x = imagesx($img); 
$y = imagesy($img); 

// get mime type 
$finfo = new finfo(FILEINFO_MIME); 
$type = $finfo->buffer($raw); // e.g. image/jpeg 
+0

着我處理照片上傳過程相同的方式,我當我上傳它作爲一個文件做的形象呢?或者我需要使用上述過程創建一個新的函數? - 上面的strlen($ row)不能工作,因爲$ row是一個資源 – fxuser

+0

你可以使用'file_get_contents($ _ FILES ['file'] ['tmp_name'])'將上傳的文件寫入一個變量,然後使用基於字符串的驗證。或者將你的curl-fetched文件寫入磁盤,然後使用基於磁盤的驗證。 –

+1

但我不上傳圖片..我正在使用URL來獲取圖像並驗證它... – fxuser