我正在尝试在邮递员中上传图片,我的json有两个对象,其外观如下:
{
"user_profile": {
"email": "shahzad176@ovadamd.com",
"password": "admin123",
"password_confirmation": "admin123",
"status": 0,
"first_name": "Shahzad",
"middle_name": "Hussain",
"last_name": "Shah",
"date_of_birth": "2015-01-01",
"gender": "M",
"area_id": 1,
"address": "Minhatten NY",
"city": "New York",
"state": "Washington",
"zip": "12312",
"fax": "111-111-1111",
"phone_extension": "2471",
"work_phone": "111-111-1111",
"phone_no": "111-111-1111",
"emergency_contact": "111-111-1111",
"social_security": "111-11-1111",
"module_id": 6
},
"privileges": {
"is_super_admin": 0,
"facilities": [
{
"facility_id": 1,
"is_facility_supervisor": 0,
"speciality_id": 1,
"priv_key": "ced",
"role_id": 2
}
]
}
}
我的控制器:
$body = $request->all();
$userProfile = $body['user_profile'];
$userPrev = $body['privileges'];
$userProfile['is_super_admin'] = $userPrev['is_super_admin'];
$facilities = $userPrev['facilities'];
$bodyObj = array_merge($userProfile, $userPrev);
$validator = UserValidations::validateUser($bodyObj);
if ($validator->fails()) {
return response([
'status' => false,
'message' => __('messages.validation_errors'),
'errors' => $validator->errors()->all()
], 200);
}
DB::beginTransaction();
try{
$photoName = time().'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('profile_images'), $photoName);
$path = asset('public/profile_images/'.$photoName);
$this->user->where('id', Auth::user()->id)->update(['profile_pic' => $path]);
我在json中有两个不同的对象,我想上传用户个人资料图片,并且我正在使用表单数据并上传image [],但它表示未定义索引user_profile,我在哪里做错了?
我们将非常感谢您的帮助!
答案 0 :(得分:0)
也许您需要将图像转换为base64数据,并且您可能希望使用一个package来简化它。
也许您的代码看起来像
public function saveImage() {
$data = Input::all();
$image_url = "image-".time().".png";
$image_path = public_path().'img/' . $image_url;
Image::make(file_get_contents($data->base64_image))->save($image_path);
$response = array(
'status' => 'success',
);
return Response::json( $response );
}
我的一个项目的实际代码
$base64Img = $request->input('image');
$name = time() . '.' . explode('/', explode(':', substr($base64Img, 0, strpos($base64Img, ';')))[1])[1];
$path = public_path() . '/img/notices/' . $name;
Image::make(file_get_contents($base64Img))->save($path);
您的代码
try{
$base64Img = $request->input('image');
$photoName= time() . '.' . explode('/', explode(':', substr($base64Img, 0, strpos($base64Img, ';')))[1])[1];
$path = asset('public/profile_images/'.$photoName);
Image::make(file_get_contents($base64Img))->save($path);
$this->user->where('id', Auth::user()->id)->update(['profile_pic' => $path]);
您需要使用$request->image->move()
来代替file_get_contents()
。
并添加
use Intervention\Image\ImageManagerStatic as Image;
位于控制器顶部。
在请求中发送正确的base64格式的图像非常重要
示例格式为
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAFAAlgDASIAAhEBAxEB/8QAHQABAAICAwEBAAAAAAAAAAAAAAEIBgcEBQkCA//EAEIQAQABAwIDBAUIBwYHAAAAAAABAgMEBREGBxIIITGBE0FhcZEUFhgiQlFUlBUyUlWCkrEjU6Gio9IkM0NicrLR/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAH/xAAVEQEBAAAAAAAAAAAAAAAAAAAAEf/aAAwDAQACEQMRAD8AtCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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