basagabi
basagabi

Reputation: 5064

Laravel socialite facebook avatar

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.

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

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

<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.

How could I display the image on the img tag to display the actual image?

Upvotes: 6

Views: 8787

Answers (2)

Paolo Loconsole
Paolo Loconsole

Reputation: 135

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

Upvotes: 1

Nadeem0035
Nadeem0035

Reputation: 3957

Simply fetch the data using file_get_contents function and process the retrieved data.

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

Upvotes: 10

Related Questions