How to Load Gravatar Image in Laravel?

Laravel 6 , Laravel , Laravel 7

Nicesnippets

627

02-06-2020


Hi Dev,

Today, In this blog, I will learn you how to load gravatar image in laravel. There are several package for Gravatar image for laravel application, but We’re going to use creativeorange/gravatar package. that is i think better.

It is very easy to setup Gravatars on your site. Gvatar is a graphical representation of a user. We will use the e-mail address associated with your account and query Gravatar for your image.

Here I will give you full example for load gravatar image in laravel You can follow bellow step by step.

Step 1 : Install Laravel App

In this step, You can install laravel fresh app. So open terminal and put the bellow command.

composer create-project --prefer-dist laravel/laravel blog

Step 2 : Install Package

In this step, we are going to install creativeorange/gravatar package.So Let's open terminal and run bellow command:

composer require creativeorange/gravatar

After Run successfully above command thenafter we need to add this service provider and aliase in config/app.php file:

config/app.php

'providers' => [

//....

'Creativeorange\Gravatar\GravatarServiceProvider::class',

],

'aliases' => [

//....

'Gravatar' => 'Creativeorange\Gravatar\Facades\Gravatar::class',

],

Finally Add this service provider and aliase after publish the config by running the bellow command So Let's open terminal and run bellow command:

php artisan vendor:publish

Step 3 : Gravatar Configuration

In this step, we have to need define gravatar size and option in the package config file.

config/gravatar.php

return [

'default' => [

'size' => 80,

'fallback' => 'mm',

'secure' => false,

'maximumRating' => 'g',

'forceDefault' => false,

'forceExtension' => 'jpg',

],

'small-secure' => [

'size' => 30,

'secure' => true,

],

'medium' => [

'size' => 150,

]

];

Step 4 : Add Route

In this step, we have to add new route in routes file open web.php file add bellow route in routes file.

routes/web.php

Route::get('/gravatar', 'GravatarController@gravatar');

Step 5 : Create Controller

In this step, You can create new controller as GravatarController file.So, let's copy bellow code and put on GravatarController.php file.

app/http/controller/GravatarController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Creativeorange\Gravatar\Facades\Gravatar;

class GravatarController extends Controller

{

/**

* Display the specified resource.

*

* @return \Illuminate\Http\Response

*/

public function gravatar()

{

// get image

Gravatar::get('test@example.com');

// set fallback image

Gravatar::fallback('https://www.nicesnippets.com/.....image_url')->get('test@example.com');

return view('gravatar');

}

}

Step 6 : Create Blade File

In last step. In this step we have to create gravatar blade file. So mainly we have to create gravatar view file. So finally you have to create gravatar following file and put bellow code:

/resources/views/gravatar.blade.php

<!DOCTYPE html>

<html>

<head>

<title>How to Load Gravatar Image in Laravel? - Nicesnippets.com</title>

</head>

<body>

<h1>How to Load Gravatar Image in Laravel? - Nicesnippets.com</h1>

<img src="{{ Gravatar::get('test@example.com') }}" alt="No image found">

</body>

</html>

Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/gravatar

It will help you...


Recommended Posts