七牛上传之二进制流文件上传
public function images(){
$images = trim($_POST['images']);
if(!$images){
error("没有上传文件",APP_CODE_LACK_PARAM);
return $this->getResult(false);
}
preg_match('/^(data:\s*image\/(\w+);base64,)/', $images, $result);
$type = $result[2]; //图片类型
$types = empty($types)? array('jpg', 'gif', 'png', 'jpeg'):$types;
if(!in_array($type, $types)){
error("类型错误,不支持此类型图片");
return $this->getResult(false);
}
$image = ImageService::Uploads($type,$images);
if(!$image){
return $this->getResult(false);
}
$data = ['image' => $image];
return $this->getResult($data);
}
/**
* 单个图片上传储存处理
* @param string $uploadKey
*/
public static function Uploads($type,$images){
if(!$images){
error("没有图片");
return false;
}
$md5 = md5_file($images);
$sha1 = sha1_file($images);
$condition = [
'md5' => $md5,
'sha1' => $sha1,
];
$image = db('image')->where($condition)->find();
if($image && !self::$update){
return self::RenderURL($image);
}
$imageID = 0;
if($image){
$imageID = $image['id'];
}
$bucket = 'image';
$emote_server = 'http://upload.qiniu.com/putb64/-1';
$result = self::request_by_curl($emote_server,$images,$type);
$result = json_decode($result, true);
if(!$result){
error("文件上传失败~");
return false;
}
$uploadKey = $result['key'];
$path = "{$bucket}@{$uploadKey}";
$image = [
'storage_type' => ImageConfig::STORAGE_TYPE_QINIU,
'path' => $path,
'create_time' => time(),
'type' => $type,
'md5'=>$md5,
'sha1'=>$sha1,
'title'=>"base_64上传",
'mime_type'=>"image/".$type
];
$loginInfo = MerchantUserService::GetLoginMerchant();
if($loginInfo){
$image['create_merchant_id'] = $loginInfo['merchant']['id'];
}
if($imageID){
db('image')->where('id',$imageID)->update($image);
} else {
$imageID = db('image')->insertGetId($image);
}
if(!$imageID){
error("数据库错误",APP_CODE_DB);
return false;
}
$image['id'] = $imageID;
return self::RenderURL($image);
}
public static function request_by_curl($remote_server,$post_string,$type) {
$base64 = trim($post_string);
switch ($type){
case "jpeg":
$base64 = str_replace('data:image/jpeg;base64,', '', $base64); //只要逗号后面的
break;
case "png":
$base64 = str_replace('data:image/png;base64,', '', $base64); //只要逗号后面的
break;
case "jpg":
$base64 = str_replace('data:image/jpg;base64,', '', $base64); //只要逗号后面的
break;
case "gif":
$base64 = str_replace('data:image/gif;base64,', '', $base64); //只要逗号后面的
break;
}
$accessKey = config('qiniu.access_key');
$secretKey = config('qiniu.secret_key');
$auth = new Auth($accessKey, $secretKey);
$token = $auth->uploadToken("image");
$headers = array();
$headers[] = 'Authorization:UpToken '.$token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$remote_server);
curl_setopt($ch, CURLOPT_HTTPHEADER ,$headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $base64);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
public static function RenderURL($images){
if(!$images){
return $images;
}
$one = false;
if(array_one($images)){
$one = true;
$images = [$images];
}
foreach ($images as $key => $image){
$image['url'] = QiniuStorage::GetURL($image['path']);
$images[$key] = $image;
}
if($one){
return current($images);
}
return $images;
}
七牛上传之普通上传
public function image(){
$image = ImageService::Upload();
if(!$image){
return $this->getResult(false);
}
$data = ['image' => $image];
return $this->getResult($data);
}
/**
* 单个图片上传储存处理
* @param string $uploadKey
*/
public static function Upload($uploadKey = 'image'){
$imageInfo = isset($_FILES[$uploadKey]) ? $_FILES[$uploadKey] : null;
if($imageInfo == null){
error("没有上传文件",APP_CODE_LACK_PARAM);
return false;
}
$request = Request::instance();
$file = $request->file($uploadKey);
if(!$file->checkImg()){
error("类型错误,不支持此类型图片");
return false;
}
$fileName = $imageInfo['name'];
$path = $imageInfo['tmp_name'];
$mimeType = $imageInfo['type'];
$extension = pathinfo($fileName,PATHINFO_EXTENSION);
$md5 = md5_file($path);
$sha1 = sha1_file($path);
$condition = [
'md5' => $md5,
'sha1' => $sha1,
];
$image = db('image')->where($condition)->find();
if($image && !self::$update){
return self::RenderURL($image);
}
$imageID = 0;
if($image){
$imageID = $image['id'];
}
$uploadKey = date('Y') . '/'. date('m') . '/' . date('d') . '/';
$uploadKey .= $md5 . '.' . $extension;
$bucket = 'image';
$result = QiniuStorage::UploadFile($path, $uploadKey, $bucket);
if(!$result){
error("文件上传失败~");
return false;
}
$path = "{$bucket}@{$uploadKey}";
$image = [
'storage_type' => ImageConfig::STORAGE_TYPE_QINIU,
'path' => $path,
'md5' => $md5,
'sha1' => $sha1,
'create_time' => time(),
'mime_type' => $mimeType,
'type' => $extension,
'title' => $fileName,
];
$loginInfo = MerchantUserService::GetLoginMerchant();
if($loginInfo){
$image['create_merchant_id'] = $loginInfo['merchant']['id'];
}
if($imageID){
db('image')->where('id',$imageID)->update($image);
} else {
$imageID = db('image')->insertGetId($image);
}
if(!$imageID){
error("数据库错误",APP_CODE_DB);
return false;
}
$image['id'] = $imageID;
return self::RenderURL($image);
}
// $picturedatas = base64_encode($picturedata);
// return ('data:' . 'image/'.$type . ';base64,' . $picturedatas);