/*
* base64图片上传
* @param string $base64_img 图片数据流
* @return string|bool
*/
public function base64img($base64_img)
{
$up_dir = dirname(__DIR__).’/web/uplodes/image/’;
if(preg_match(‘/^(data:\s*image\/(\w+);base64,)/’, $base64_img, $result)){
$type = $result[2];
//判断图片类型
if(in_array($type,array(‘pjpeg’,’jpeg’,’jpg’,’gif’,’ bmp ‘,’ png ‘)))
{
$new_file = $up_dir.date(‘YmdHis’).’.’.$type;
$returnurl = Yii::$app->params[‘IMGURL’].’/uplodes/image/’.date(‘YmdHis’).’.’.$type;
if(file_put_contents($new_file, base64_decode(str_replace($result[1], ”, $base64_img))))
{
return [‘message’=>’上传成功’,’data’=>$returnurl,’code’=>200];
}else{
return [‘message’=>’上传失败’,’data’=>[],’code’=>400];
}
}else{
//文件类型错误
return [‘message’=>’类型错误’,’data’=>[],’code’=>400];
}
}else{
//文件错误
return [‘message’=>’文件错误’,’data’=>[],’code’=>400];
}
}
/**
* @ api {post} uplode/uploadimg 图片路径
* @apiDescription 图片路径
* @apiParam {String} image 图片文件
* @apiGroup Uplodes
* @apiPermission ???
* @apiVersion 1.1.0
* @apiSuccessExample { json } Success-Response:
{
“code”: “200”,
“message”: “上传成功”,
“data”:’图片地址’
}
{
“code”: “400”,
“message”: “上传失败”
}
*/
// 文件上传
public function actionUploadimg()
{
$file = $_FILES[‘image’];
//接收参数
$request = Yii::$app->request->post();
//定义路径
$imgurl = dirname(__DIR__).’/web/uplodes/image/’.date(“Ym”);
if(!is_dir($imgurl)){
mkdir($imgurl);
}
//图片名称
$filename = ‘/’.md5(time().mt_rand(10, 99)).”.png”;
$newFilePath = $imgurl.$filename;
$returnurl = Yii::$app->params[‘IMGURL’].’/uplodes/image/’.date(“Ym”).$filename;
//移动图片
$isup = move_uploaded_file($_FILES[‘image’][‘tmp_name’], $newFilePath);
if($isup == true){
$info[‘imgurl’] = $imgurl;
return $this->response([‘message’=>’上传成功’,’data’=>$returnurl,’code’=>200]);
}else{
return $this->response([‘message’=>’上传失败’,’data’=>[],’code’=>400]);
}
}
相关文章
本站已关闭游客评论,请登录或者注册后再评论吧~