【问题标题】:Laravel socialite facebook avatarLaravel 社交名媛脸书头像
【发布时间】:2017-03-04 07:18:11
【问题描述】:

我在社交名流中使用 Laravel 5.2。我能够提取用户的详细信息,但问题是如果我将头像注入到其src 上,则无法正确显示头像。

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

如果我在图像src 中回显这个值,它会是这样的。

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

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

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

【问题讨论】:

    标签: php facebook laravel facebook-graph-api laravel-socialite


    【解决方案1】:

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

    在控制器中

    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");
    

    【讨论】:

    • 另外,它在 Laravel 5.8 上就像一个魅力。就我而言,我很难正确命名上传的文件。因此,我先保存了新用户,然后再获取 Id 以形成名称 (.jpg)。为了展示,我使用了: $userId = Auth::user()->id; $picProfile = '../uploads/profile/' 。 $userId 。 ".jpg";
    【解决方案2】:

    我正在使用以下代码:

    /**
     * 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')]);
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 2018-12-20
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-23
      相关资源
      最近更新 更多