2012-09-25 199 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); 
}