[英]Replace avatar Url on Social login with Laravel Socialite
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
换句话说,我在
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:我通过 facebook 获取这个 URL:
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.鉴于目标目录可由 php 写入,上述方法将起作用。
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.