1. 程式人生 > >微信頭像處理

微信頭像處理

/*處理微信頭像
*
 * $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;
}