Subscribe & Follow

Create Stunning Avatars with mmnijas/avatar Laravel Package

Create Stunning Avatars with mmnijas/avatar Laravel Package

In the world of web development, user experience plays a pivotal role in attracting and retaining users. One effective way to enhance user interaction is by personalizing user profiles with custom avatars. The mmnijas/avatar package for Laravel makes it easy to generate user avatars based on initials, offering flexibility in size, colors, and styles. In this blog, we will explore how to install and use this package effectively in your Laravel application

Features

  • Initial-Based Avatars: Automatically generates avatars using the user's initials.
  • Customizable Sizes: Define the avatar size according to your requirements.
  • Random Background Colors: Select from a palette of dark shades to give your avatars a unique look.
  • Easy Integration: Simple to set up and use within any Laravel application.

Installation

To get started, you'll need to install the package via Composer. Run the following command in your terminal:


    composer require mmnijas/avatar
   


Setting Up Routes

After installing the package, you need to define a route in your web.php file. This route will handle requests for avatar generation.


    use App\Http\Controllers\AvatarController;

    Route::get('/avatar', [AvatarController::class, 'generateAvatar'])->name('avatar');


Creating the Controller

Next, create a controller to handle the avatar generation. You can name it AvatarController or any name you prefer. Inside this controller, define a method called generateAvatar.


    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use Mmnijas\Avatar\Generate;

    class AvatarController extends Controller
    {
        public function generateAvatar(Request $request)
        {
            return Generate::avatar($request->all());
        }
    }


Usage

Now that everything is set up, you can easily generate avatars in your Blade templates. Here's an example of how to display an avatar using the Generate::avatar method:


    <img src="{{ route('avatar', ['name' => 'John Doe', 'size' => 200]) }}" alt="Avatar">


In this example, you can customize the name and size parameters as needed. The package will automatically generate an avatar based on the provided initials.

Customization Options

Available Parameters

  1. name: (string) The full name of the user. This is required for generating initials.
  2. size: (integer) The size of the avatar in pixels. Defaults to 200.
  3. bg: (string) Background color in hexadecimal format. If not provided, a random dark shade will be selected.
  4. color: (string) The text color in hexadecimal format. Defaults to white (#ffffff).

Example with Custom Background Color and Text Color

You can further customize the avatar by specifying the background and text colors:


    <img src="{{ route('avatar', [
        'name' => 'Nijas M M',
        'size' => 200,
        'bg' => '#3498db',
        'color' => '#ffffff'
        ]) }}"
    alt="Avatar">
   


Conclusion

The mmnijas/avatar package is a powerful and flexible solution for generating user avatars in Laravel applications. By following the steps outlined in this blog, you can enhance your application's user interface and provide a personalized experience for your users.

Leave a Comment

Your email address will not be published. Required fields are marked *