您的位置主页 > PHP

保存远程图片到本地的办法

有些时候我们看到网络上有一些漂亮的图片,而又不想手动去保存下来,尤其是大量的这样的图片,那这个时候我们需要写一段程序来帮助我们完成这个工作。今天在整理资料的时候发现以前找到的一个函数,通过这个函数我们就可以实现上面的功能。
主要函数:
[color=green]function[/color]
[color=blue]GrabImage[/color][color=olive]([/color][color=#00008b]$url[/color][color=gray],[/color][color=#00008b]$filename[/color][color=gray]=[/color][color=#8b0000]""[/color][color=olive])[/color]
[color=olive]{[/color]
[color=green]if[/color][color=olive]([/color][color=#00008b]$url[/color][color=gray]==[/color][color=#8b0000]""[/color][color=olive])[/color]
[color=green]return[/color]
[color=green]false[/color][color=gray];

[/color][color=green]if[/color][color=olive]([/color][color=#00008b]$filename[/color][color=gray]==[/color][color=#8b0000]""[/color][color=olive])[/color]
[color=olive]{[/color]
[color=#00008b]$ext[/color][color=gray]=[/color][color=blue]strrchr[/color][color=olive]([/color][color=#00008b]$url[/color][color=gray],[/color][color=#8b0000]"[/color][color=red].[/color][color=#8b0000]"[/color][color=olive])[/color][color=gray];
[/color][color=green]if[/color][color=olive]([/color][color=#00008b]$ext[/color][color=gray]!=[/color][color=#8b0000]"[/color][color=red].gif[/color][color=#8b0000]"[/color][color=gray] && [/color][color=#00008b]$ext[/color][color=gray]!=[/color][color=#8b0000]"[/color][color=red].jpg[/color][color=#8b0000]"[/color][color=gray] && [/color][color=#00008b]$ext[/color][color=gray]!=[/color][color=#8b0000]"[/color][color=red].png[/color][color=#8b0000]"[/color][color=olive])[/color]
[color=green]return[/color]
[color=green]false[/color][color=gray];
[/color][color=#00008b]$filename[/color][color=gray]=[/color][color=blue]date[/color][color=olive]([/color][color=#8b0000]"[/color][color=red]YmdHis[/color][color=#8b0000]"[/color][color=olive])[/color][color=gray].[/color][color=#00008b]$ext[/color][color=gray];
[/color][color=olive]}[/color]
[color=blue]ob_start[/color][color=olive]()[/color][color=gray];
[/color][color=blue]readfile[/color][color=olive]([/color][color=#00008b]$url[/color][color=olive])[/color][color=gray];
[/color][color=#00008b]$img[/color][color=gray] = [/color][color=blue]ob_get_contents[/color][color=olive]()[/color][color=gray];
[/color][color=blue]ob_end_clean[/color][color=olive]()[/color][color=gray];
[/color][color=#00008b]$size[/color][color=gray] = [/color][color=blue]strlen[/color][color=olive]([/color][color=#00008b]$img[/color][color=olive])[/color][color=gray];

[/color][color=#00008b]$fp2[/color][color=gray]=@[/color][color=blue]fopen[/color][color=olive]([/color][color=#00008b]$filename[/color][color=gray], [/color][color=#8b0000]"[/color][color=red]a[/color][color=#8b0000]"[/color][color=olive])[/color][color=gray];
[/color][color=blue]fwrite[/color][color=olive]([/color][color=#00008b]$fp2[/color][color=gray],[/color][color=#00008b]$img[/color][color=olive])[/color][color=gray];
[/color][color=blue]fclose[/color][color=olive]([/color][color=#00008b]$fp2[/color][color=olive])[/color][color=gray];

[/color][color=green]return[/color]
[color=#00008b]$filename[/color][color=gray];
[/color][color=olive]}[/color]


获取一张图片的代码:
[color=#00008b]$img[/color][color=gray]=[/color][color=blue]GrabImage[/color][color=olive]([/color][color=#8b0000]"[/color][color=red]http://www.google.com/intl/en_ALL/images/logo.gif[/color][color=#8b0000]"[/color][color=gray],[/color][color=#8b0000]"[/color][color=red]logo.gif[/color][color=#8b0000]"[/color][color=olive])[/color][color=gray];
[/color][color=green]if[/color][color=olive]([/color][color=#00008b]$img[/color][color=olive]){[/color]
[color=green]echo[/color]
[color=#8b0000]'[/color][color=red]<img src="[/color][color=#8b0000]'[/color][color=gray].[/color][color=#00008b]$img[/color][color=gray].[/color][color=#8b0000]'[/color][color=red]">[/color][color=#8b0000]'[/color][color=gray];
[/color][color=olive]}[/color][color=green]else[/color][color=olive]{[/color]
[color=green]echo[/color]
[color=#8b0000]"[/color][color=red]false[/color][color=#8b0000]"[/color][color=gray];
[/color][color=olive]}[/color]


这是保存google的logo的例子,获取到的图片保存在同级目录下面。
获取一系列的有规律的图片(例如:以数字1-100命名的100张图片):
[color=green]for[/color]
[color=olive]([/color][color=#00008b]$i[/color][color=gray]=[/color][color=maroon]1[/color][color=gray];[/color][color=#00008b]$i[/color][color=gray]<=[/color][color=maroon]100[/color][color=gray];[/color][color=#00008b]$i[/color][color=gray]++[/color][color=olive]){[/color]
[color=#00008b]$img[/color][color=gray]=[/color][color=blue]GrabImage[/color][color=olive]([/color][color=#8b0000]"[/color][color=red]http://www.yourimagesite.com/images/[/color][color=#00008b]$i[/color][color=red].gif[/color][color=#8b0000]"[/color][color=gray],[/color][color=#8b0000]"[/color][color=red]images/[/color][color=#00008b]$i[/color][color=red].gif[/color][color=#8b0000]"[/color][color=olive])[/color][color=gray];
[/color][color=olive]}[/color]


上面的[url]www.yourimagesite.com[/url]是图片的网址,需要自己修改,程序执行完后,所有的图片将会保存到images目录下面。