php 获取远程图片

来源:互联网 发布:xperia xzs知乎 编辑:程序博客网 时间:2024/05/16 14:17
<?php//采集图片// 判断目录是否存在 不存在的话则创建function make_dir($path){     if(!file_exists($path)){//不存在则建立         $mk=@mkdir($path,0777); //权限         @chmod($path,0777);     }     return true; }  //函数read_filetext()取得图片内容。使用fopen打开图片文件,然后fread读取图片文件内容。function read_filetext($filepath){     $filepath=trim($filepath);     $htmlfp=@fopen($filepath,"r");     //远程     if(strstr($filepath,"://")){         while($data=@fread($htmlfp,500000)){             $string.=$data;         }     }     //本地     else{         $string=@fread($htmlfp,@filesize($filepath));     }     @fclose($htmlfp);     return $string; } //函数write_filetext()写文件,将图片内容fputs写入文件中,即保存图片文件。function write_filetext($filepath,$string){     //$string=stripSlashes($string);     $fp=@fopen($filepath,"w");     @fputs($fp,$string);     @fclose($fp); } //函数get_filename()获取图片名称,也可以自定义要保存的文件名。function get_filename($filepath){     $fr=explode("/",$filepath);     $count=count($fr)-1;     return $fr[$count]; } //组合函数function save_pic($url,$savepath=''){     //处理地址     $url=trim($url);     $url=str_replace(" ","%20",$url);     //读文件     $string=read_filetext($url);     if(empty($string)){         echo '读取不了文件';exit;     }     //文件名     $filename = get_filename($url);     //存放目录     make_dir($savepath); //建立存放目录     //文件地址     $filepath = $savepath.$filename;     //写文件     write_filetext($filepath,$string);     return $filepath; } $pic = "http://xxxx.com/img/sns/emotion/0.gif"; //保存目录 $savepath = "q/"; //echo save_pic($pic,$savepath); for ($i=1;$i<=97;$i++){//$pic = "http://xxxx.com/img/sns/emotion/$i.gif"; //echo save_pic($pic,$savepath); echo "<br>";}?>

0 0
原创粉丝点击