翻了不少文章才找到的一段代码,修改了一下使它可以下载到图片API的图片。


<?php
function download($url){
    $state = @file_get_contents($url,0,null,0,1);//获取网络资源的字符内容
    if($state){
        $ext = pathinfo($url,PATHINFO_EXTENSION);
        $dir = './';//图片储存地址
        $allow_type = array('jpg','jpeg','gif','png'); //图片类型
        if(in_array($ext, $allow_type)){//判断不是图片接口
            $filename = $dir.time().'.'.$ext;//一般图片文件名称生成
        }else{
              $filename = $dir.time().'.png';//API图片文件名称生成
        }
        if(!file_exists($dir)){
            return false;
        }else{
               ob_start();//打开输出
            readfile($url);//输出图片文件
            $img = ob_get_contents();//得到浏览器输出
            ob_end_clean();//清除输出并关闭
            $size = strlen($img);//得到图片大小
            $fp2 = @fopen($filename, "a");
            fwrite($fp2, $img.'png');//向当前目录写入图片文件,并重新命名
            fclose($fp2);
            return $filename;//输出图片储存地址
        }
    }else{
        return false;
    }
}
echo download('http://qr.topscan.com/api.php?text='.time());
Last modification:September 19th, 2019 at 04:04 pm
如果觉得我的文章对你有用,请随意赞赏