2012-09-25 186 views
1

這是我的php代碼,它僅從9中獲取第一個圖像,但是當我手動檢查URL時,所有9圖像URL都正確形成,我試着用CURL代碼,但循環是不工作,讓我知道我做錯了什麼?使用file_get_contents從url中獲取圖像

<?php 

for($i=2 ; $i <= 10 ; $i++){ 
header('Content-type: image/jpeg;'); 
$url = "http://www.lafourchette.com/p-3.3.0/default/rate-bar-bg-".$i.".jpg"; 
$mycontent = file_get_contents($url); 
echo $mycontent; 
} 

回答

4

@ h2ooooooo其實兄弟我不是在尋找一個單一的形象......我只是想保存這9張圖像文件夾中

<?php 
    for ($i = 2 ; $i <= 10; $i++) { 
     $imageName = "rate-bar-bg-" . $i . ".jpg"; 
     $imageContent = file_get_contents("http://www.lafourchette.com/p-3.3.0/default/" . $imageName); 
     file_put_contents($imageName, $imageContent); 
    } 
?> 
+0

Thx再次:) – swapnesh

0

設置內容lenght以及

header('Content-Length: ' . $size);

+0

沒有幫助:(不能正常工作:( – swapnesh

1

這是因爲它無法串連JPEG圖像那樣。客戶端將讀取第一張圖片並將其他圖片視爲尾隨垃圾。

如果你想創建一個大的圖像,你應該使用GDimagick

或者,你可以在頁面上創建多個圖像的每一個點rate-bar-bg-2.jpg10。這可能會更有效率,除非您有很好的理由不直接鏈接到該網站。

或者,只做一次工作並手動創建一個精靈。

+0

然後我怎麼能做出這樣顯示單頁上的所有9個圖像?? – swapnesh

+0

@swapnesh難道你不能只使用9'''元素引用一個HTML文件輸出這些圖像? – h2ooooooo

+0

@ h2ooooooo即使我可以保存所有9張圖片只是通過改變網址的一點點.....它不是什麼我看....我認爲它更好,如果你提供了一些代碼,而不是指着我我可以做或不做(對不起,用這些詞粗魯) – swapnesh

0

隨着@ h2ooooooo的幫助下,我想出了一個解決方案---

<?php 

for($i=2 ; $i <= 10 ; $i++){ 
$filename = "rate-bar-bg-".$i.".jpg"; 
header('Content-type: image/jpeg;'); 
$url = "http://www.lafourchette.com/p-3.3.0/default/rate-bar-bg-".$i.".jpg"; 
$fileContent = file_get_contents($url); 
file_put_contents($filename, $fileContent); 
} 
0
for ($i = 2 ; $i <= 10; $i++) { 
     $imageName = "rate-bar-bg-" . $i . ".jpg"; 
     copy("http://www.lafourchette.com/p-3.3.0/default/" . $imageName,$imageName); 
}