php图片下载方法

8-19 1,672 views

使用php的curl方法下载图片,只需要提供图片地址就可以了。
$url = "http://www.bac.com/1.gif";
getImage($url,'./');

function getImage($url,$path='',$filename='',$type=0){
    if($url==''){return false;}
    if($filename==''){
        $ext=pathinfo($url, PATHINFO_EXTENSION);
        if(!empty($ext)){
            $ext = '.'.$ext;
        }
        if ($path!=''){
            $path = $path . date('Y/').date('m/');
            if (!is_dir($path)){
                @mkdir($path,777,true);
            }
        }
        $filename=$path . date('YmdHis').'_curl_'.time().rand(100,999).$ext;
    }

    //文件保存路径 
    if($type){
        $ch=curl_init();
        $timeout=5;
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
        $img=curl_exec($ch);
        curl_close($ch);
    }else{
        ob_start(); 
        readfile($url);
        $img=ob_get_contents(); 
        ob_end_clean(); 
    }
    $size=strlen($img);
    //文件大小 
    $fp2=@fopen($filename,'a');
    fwrite($fp2,$img);
    fclose($fp2);
    return $filename;
}

nginx 解析php conf配置文件

本博客的nginx 配置文件 server { listen 80;#80和443端口同时监听 listen 443 ssl; server_name www.koukousky.com koukousky.com...

阅读全文

php composer 安装阿里云镜像

#doc: https://developer.aliyun.com/composer 全局配置(推荐) composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ 取消配...

阅读全文

intervention/image处理IOS上传图片翻转问题

#安装 $ composer require intervention/image <?php use Intervention\Image\ImageManagerStatic as Image; //test.png要处理的图片路径 Image::make(&...

阅读全文

欢迎留言