I'm facing an issue, I'm login with social networks and everything works perfect but my problem is with Avatars. When login, I get all the data including the Avatar but that avatar is saved in my database with Social Network original Url, I want to save it with my url instead.
In other words, I save the profile pictures using timestamps and original file extension in Laravel
This my provider callback method in controller
public function handleProviderCallback(Request $request, $provider)
{
try
{
$socialUser = Socialite::driver($provider)->user();
}
catch(\Exception $e)
{
return redirect('/');
}
//Check if we have logged on Social Provider
$socialProvider = SocialProvider::where('provider_id', $socialUser->getId())->first();
if (!$socialProvider)
{
//Create a new user and provider
$user = new User;
$user->name = $socialUser->getName();
$user->profilepic = $socialUser->getAvatar();
$user->email = $socialUser->getEmail();
if($request->hasFile('avatar')){
$profilepic = $request->file('avatar');
$filename = time() . '.' . $profilepic->getClientOriginalExtension();
Image::make($profilepic)->resize(300, 300)->save( public_path('/uploads/avatars/' . $filename ) );
$user->profilepic = $filename;
}
$user->save();
// $user = User::firstOrCreate([
// 'name' => $socialUser->getName(),
// 'profilepic' => $socialUser->getAvatar(),
// 'email' => $socialUser->getEmail()
// ]);
$user->socialProviders()->create(
['provider_id' => $socialUser->getId(), 'provider' => $provider]
);
Session::flash('success', 'Tu cuenta ha sido enlazada correctamente, sin embargo, ahora debes crear una contraseña. Crea tu contraseña ahora.');
$user->save();
auth()->login($user);
return redirect('account-complete');
}
else
$user = $socialProvider->user;
auth()->login($user);
return back();
}
Im getting this URL with facebook:
https://graph.facebook.com/v2.10/10155364486426042/picture?type=normal
This is how i want it to be
http://localhost:8000/uploads/avatars/1519702861.jpg
Thanks in advance
You can do it like this:
if ($socialUser->getAvatar()) {
$filename = "uploads/avatar/".time().".jpg";
// The filename to save in the database.
file_put_contents(
$filename,
file_get_contents($socialUsr->getAvatar())
);
}
Above method will work given that the destination directory is writable by php.
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.