上传图片到阿里云服务器base64 上传

//上传图片到阿里云服务器
function upload_Ali($remoteImage){

    $imageData = $this->n_img_base_64($remoteImage);

    if ($imageData !== false) {
        // 初始化 cURL 句柄
        $ch = curl_init();

        // 设置请求 URL 和一些 cURL 选项
        curl_setopt($ch, CURLOPT_URL, 'http://dev.com/index/ajax/upload');
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array(
            'file' => $imageData
        ));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // 执行请求并处理响应
        $response = curl_exec($ch);
        $ret = json_decode($response,true);
        if ($ret['code'] == 1){
            return $ret['data']['fix_url'];
        }else{
            echo '上传图片到阿里云失败';
        }
        // 关闭 cURL 句柄
        curl_close($ch);
    } else {
        echo 'Error: failed to read remote image data';
    }
}
function n_img_base_64($img){
    $imageInfo = getimagesize($img);
    return 'data:' . $imageInfo['mime'] . ';base64,' . chunk_split(base64_encode(file_get_contents($img)));
}

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>