首页 > 代码库 > php远程抓取图片

php远程抓取图片


    public  function GrabImage($url,$filename="") {
        if($url=="") return false;

        if($filename=="") {
            $ext=strrchr($url,".");
            if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") return false;
            $filename=date("YmdHis").$ext;
        }

        $data = http://www.mamicode.com/file_get_contents($url);
        file_put_contents(‘./temp/cardpicture/‘.$filename,$data);
        die;
        ob_start();
        readfile($url);
        $img = ob_get_contents($url);
        ob_end_clean();
        $fp2=@fopen(‘./temp/cardpicture/‘.$filename, "a");//指定图片路径
        fwrite($fp2,$img);
        fclose($fp2);

        return $filename;
    }
    public function getpic(){
        $img=$this->GrabImage("http://www.baidu.com/img/baidu_jgylogo3.gif","logo33.jpg");
        if($img){
            echo ‘<img src="http://www.mamicode.com/‘.$img.‘">‘;
        }else{
            echo "false";
        }
    }

php远程抓取图片