Laravel 社交名媛脸书头像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40185781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:38:54  来源:igfitidea点击:

Laravel socialite facebook avatar

phpfacebooklaravelfacebook-graph-apilaravel-socialite

提问by basagabi

Im using Laravel 5.2 with Socialite. I am able to pull out the details of the user but the problem is that the avatar is not being displayed properly if I inject it on its src.

我将 Laravel 5.2 与 Socialite 一起使用。我能够提取用户的详细信息,但问题是如果我将其注入到src.

Socialite returns an object wherein I could use it as $facebookDetails->getAvatar()in which returns a value of like this https://graph.facebook.com/v2.6/123456789/picture?type=normal

Socialite 返回一个对象,我可以$facebookDetails->getAvatar()在其中使用它作为返回这样的值https://graph.facebook.com/v2.6/123456789/picture?type=normal

If I echo this value in the image src, it would look like this.

如果我在图像中回显这个值src,它看起来像这样。

<img src="https://graph.facebook.com/v2.6/123456789/picture?type=normal" />

<img src="https://graph.facebook.com/v2.6/123456789/picture?type=normal" />

It seems that this is not the actual URL of the image since when I enter this on the browser, it redirects me to the "actual" image and displays the image.

这似乎不是图像的实际 URL,因为当我在浏览器上输入它时,它会将我重定向到“实际”图像并显示图像。

How could I display the image on the imgtag to display the actual image?

如何在img标签上显示图像以显示实际图像?

回答by Nadeem0035

Simply fetch the data using file_get_contentsfunction and process the retrieved data.

只需使用file_get_contents函数获取数据并处理检索到的数据。

In Controller

在控制器中

use File; 

$fileContents = file_get_contents($user->getAvatar());
File::put(public_path() . '/uploads/profile/' . $user->getId() . ".jpg", $fileContents);

//To show picture 
$picture = public_path('uploads/profile/' . $user->getId() . ".jpg");

回答by Paolo Loconsole

i'm using following code:

我正在使用以下代码:

/**
 * In order to save the user's avatar
 * REMEMBER TO ADD "use File; use Illuminate\Support\Carbon; use DB;" TO TOP! 
 * @param  $avatar Socialite user's avatar ($user->getAvatar())
 * @param $userId User's id database
 */
public function saveImageAvatar($avatar, $userId)
{
    $fileContents = file_get_contents($avatar);
    $path = public_path() . '/users/images/' . $userId . "_avatar.jpg";
    File::put($path, $fileContents);
    DB::table('Images')->insert(
        ['path' => $path,
        'nome' => 'avatar',
        'users_id' =>  $userId,
        'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
        'updated_at' => Carbon::now()->format('Y-m-d H:i:s')]);
}