TP怎么把抓取的图片保存到public目录下 在线求救

浏览:1360 发布日期:2014/09/30 分类:求助交流 关键字: TP怎么把抓取的图片保存到public目录下 在线求救
<?php
namespace Home\Controller;
use Think\Controller;
class ZhuaquController extends Controller {
public function zhuaqu(){
//http://image.baidu.com/
//file_put_contents(time().'.html', file_get_contents('http://image.baidu.com/'))
$data = file_get_contents('http://image.baidu.com/');
$pre = '/<img.*src\s*=\s*[\"|\']?\s*([^>\"\'\s]*)/i';
preg_match_all($pre,str_ireplace("\\","",$data),$arr,PREG_PATTERN_ORDER);
//var_dump($arr);
foreach ($arr[0] as $v){
$ext = strrchr($v, ".");
if ($ext != ".gif" && $ext != ".jpg" && $ext != ".png"){
continue;
}
$url = explode('src="',$v);
var_dump($url[1]);echo '<br>';
$this->GrabImage($url['1'],"");

echo "<img src=".$url['1']." />";
}
$this->display();
}
function GrabImage($url, $filename = "") {
if ($url == ""):return false;endif;//如果$url地址为空,直接退出
if ($filename == "") {//如果没有指定新的文件名
$ext = strrchr($url, ".");//得到$url的图片格式
if ($ext != ".gif" && $ext != ".jpg" && $ext != ".png"):return false;endif;//如果图片格式不为.gif或者.jpg,直接退出
$filename = date("dMYHis") . $ext;//用天月面时分秒来命名新的文件名
}
ob_start();//打开输出
readfile($url);//输出图片文件
$img = ob_get_contents();//得到浏览器输出
ob_end_clean();//清除输出并关闭
$size = strlen($img);//得到图片大小
$fp2 = @fopen("__PUBLIC__/home/picture/".$filename, "a");
fwrite($fp2, $img);//向当前目录写入图片文件,并重新命名
fclose($fp2);
}

}
最佳答案
评论( 相关
后面还有条评论,点击查看>>