6

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?

basagabi
  • 4,900
  • 6
  • 38
  • 84

2 Answers2

10

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");
Nadeem0035
  • 3,757
  • 1
  • 24
  • 36
  • also, It works like a charm on Laravel 5.8. In my case, I was getting a trouble to name correctly the uploaded file. So, I saved the new user first, before, get the Id to form the name (.jpg). To show, I used: $userId = Auth::user()->id; $picProfile = '../uploads/profile/' . $userId . ".jpg"; – LuizEduardoMPF Apr 23 '19 at 13:32
1

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')]);
}
Paolo Loconsole
  • 135
  • 2
  • 8