nikhilsharma1's avatar

base64 to upload image

cannot upload image in laravel 5.5 and base64 to convert image and save to database in image name anyone tell me how i am convert and change image in laravel 5.5

0 likes
3 replies
RamjithAp's avatar

Try this

$imagedata = base64_encode(file_get_contents($request->file('image')->pat‌​h())); 
nikhilsharma1's avatar

can you tell me how to decode image and save image to public directory its not working and tell me the solution my data is base64_encoded and i am converted data to image

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'username' => 'required|alpha|min:6|unique:userinfo',
        'password' => 'required|string|min:6|confirmed',
        'userType' => 'required|string',
        'phone' => 'required|numeric|min:10|unique:userinfo',
        'email' => 'required|string|email|max:255|unique:userinfo',
        'region' => 'required|string',
        'country' => 'required|string',
        'profileImg' => 'required',
        'status' => 'required',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\Model\userinfo
 */
protected function create(array $data)
{
    return userinfo::create([
        'username' => $data['username'],
        'password' => bcrypt($data['password']),
        'userType' => $data['userType'],
        'phone' => $data['phone'],
        'email' => $data['email'],
        'region' => $data['region'],
        'country' => $data['country'],
        'profileImg' => $data['profileImg'],
        'status' => $data['status'],
    ]);
}
simioluwatomi's avatar

I'm guessing the base64 image is profileImg. Use this

$profileImg= Image::make(base64_decode(preg_replace('#^data:image/\w+;base64,#i', '',$data['profileImg'])))->stream();
Storage::put(path/to/folder, $profileImg, 'public');

Please or to participate in this conversation.