微信头像处理

/*处理微信头像
*
 * $headimgurl 微信头像地址
 * 
 * $openid 设置生产图片的名称(此处用 openid 当作名称)
 * */
protected function wxPortrait($headimgurl,$openid){
    ob_start();
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $headimgurl);
    curl_setopt($curl, CURLOPT_REFERER, '');
    curl_setopt($curl, CURLOPT_USERAGENT, 'Baiduspider');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    echo $result;
    $data = ob_get_clean();
    $qr = "data:image/png;base64,".base64_encode($data);
    ob_end_clean();
    //图片内容 //echo $img_content;exit;
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $qr, $result)) {
        $type = $result[2];//得到图片类型png?jpg?gif?
        $new_file = "public/wx_img/{$openid}.{$type}";
        $src = "public/wx_img/{$openid}.{$type}";
        file_put_contents($new_file, base64_decode(str_replace($result[1], '', $qr)));
    }
    return $src;
}

猜你喜欢

转载自blog.csdn.net/qq_30202073/article/details/83537236