2011-09-25 64 views
3

我試圖用PHP下載this圖像以用GD編輯它。我發現了許多用於圖像鏈接的解決方案,但這是一個下載鏈接。如何從PHP中的301重定向下載鏈接獲取圖像?

編輯:

$curl = curl_init("http://minecraft.net/skin/Notch.png"); 
$bin = curl_exec($curl); 
curl_close($curl); 
$img = @imagecreatefromstring($bin); 

這是我當前的代碼。它顯示「301永久移動」。是否有CURLOPT我必須設置?

+0

有一個下載鏈接,圖片鏈接沒有什麼區別。 –

回答

5
$curl = curl_init("http://minecraft.net/skin/Notch.png"); 
// Moved? Fear not, we'll chase it! 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
// Because you want the result as a string 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
$bin = curl_exec($curl); 
curl_close($curl); 
$img = @imagecreatefromstring($bin); 
+0

是的,現在有用,謝謝。 =) – Aich

-1

fopen - 取決於你的php設置,如果url fopen是允許的。

curl

看到精細PHP手冊。

0

這裏有一個選項可將圖像直接保存到一個文件(而不是使用imagecreatefromstring):

<?php 
$fileName = '/some/local/path/image.jpg'; 
$fileUrl = 'http://remote.server/download/link'; 
$ch = curl_init($fileUrl); // set the url to open and download 
$fp = fopen($fileName, 'wb'); // open the local file pointer to save downloaded image 
curl_setopt($ch, CURLOPT_FILE, $fp); // tell curl to save to the file pointer 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // tell curl to follow 30x redirects 
curl_exec($ch); // fetch the image and save it with curl 
curl_close($ch); // close curl 
fclose($fp); // close the local file pointer