php 上传base63图片 到七牛云
前提是安装一下 七牛云的官方demo 官方文档 https://developer.qiniu.com/kodo/sdk/1241/php、
composer 安装命令如下:
composer require qiniu/php-sdk
<?php
// 七牛云 图片上传
use Qiniu\Auth;
class QiniuService
{
protected $AppKey;
protected $MasterSecret;
protected $qny_host;
protected $bucket;
function __construct()
{
$config = config_set();
$this->AppKey = '';//key
$this->MasterSecret = ''//mastersecret
$this->qny_host = '';//绑定的域名
$this->bucket = '';//空间名称
}
/**
* 上传base64位图片到七牛云
* $image base64位图片流
*/
public function upload_picture_base64($base64)
{
$base64 = trim($base64);
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $result)) {
$img = str_replace($result[1],'',$base64);
}else{
$img = $img = base64_decode($base64);
}
$auth = new Auth($this->AppKey, $this->MasterSecret);
// 生成上传Token
$token = $auth->uploadToken($this->bucket);
if($img)
{
$qiniu = self::curl_picture("http://up-z0.qiniup.com/putb64/-1/",$img,$token);
//upload.qiniup.com 上传域名适用于华东空间。
//华北空间使用 upload-z1.qiniu.com,
//华南空间使用 upload-z2.qiniu.com,
//北美空间使用 upload-na0.qiniu.com。
$qiniuArr = json_decode($qiniu,true);
if(!isset($qiniuArr['key'])){
//失败
$data = [
'data'=>$qiniuArr['error'],
'code'=>0,
];
}