abdullah-karam/laravel-svg

this package is about generating svg according to any string containing two words with some pretty options

v1.2.2 2024-03-14 19:04 UTC

README

Total Downloads Latest Version on Packagist

Laravel SVG

A Laravel package for generating SVG images from user full names or initials with some pretty cool customization options.

Requirements

  • This package requires PHP 8.0 or higher.

Installation

You can install the package via Composer by running the following command:

composer require abdullah-karam/laravel-svg

Usage

example (1)

To generate an SVG image from a user's full name, you can use the svgFor() method on the LaravelSvg facade. The svgFor() method accepts a single parameter, which is the user's full name. Here's an example of how you might use the svgFor() method:

<?php

namespace App\Http\Controllers;

use Abdullah\LaravelSvg\Facades\LaravelSvg;
use App\Models\User;

class UserController extends Controller
{
    public function generateSvg(User $user)
    {
        $svgDetails = LaravelSvg::svgFor(words: $user->fullname)->generate(); 
        dd($svgDetails);
    }
}

This will generate an SVG image with the user's initials in the center.

Abdullah Karam

array:6 [▼
  "name" => "64af002c8ba51.svg"
  "path" => "users/64af002c8ba51.svg"
  "full_path" => "http://localhost:8000/storage/users/64af002c8ba51.svg"
  "mime_type" => "image/svg+xml"
  "size" => 422
  "disk" => "public"
]

example (2)

You can also generate an SVG image from a user's initials with logo text by using the svgFor() method on the LaravelSvg facade. The svgFor() method accepts a single parameter, which is the user's initials and chain with method logoText(). Here's an example of how you might use

<?php

namespace App\Http\Controllers;

use Abdullah\LaravelSvg\Facades\LaravelSvg;
use App\Models\User;

class UserController extends Controller
{
    public function generateSvg(User $user)
    {
        $svgDetails = LaravelSvg::svgFor(words: $user->fullname)
                                  ->logoText()->generate(); 
        dd($svgDetails);
    }
}

This will generate an SVG image with the user's initials in the center and logo text. with default logo text. if you want to change logo text you can use logoText() method with parameter. like this logoText('logo') or define it once in config/laravel-svg.php file.

Abdullah Karam

array:6 [▼ 
  "name" => "64af03da5f35c.svg"
  "path" => "users/64af03da5f35c.svg"
  "full_path" => "http://localhost:8000/storage/users/64af03da5f35c.svg"
  "mime_type" => "image/svg+xml"
  "size" => 414
  "disk" => "public"
]

Configuration

You can publish the configuration file using this command:

php artisan vendor:publish --provider="Abdullah\LaravelSvg\LaravelSvgServiceProvider" --tag="config"

This is the contents of the published config file:

<?php

return [
    'folder' => 'storage', ## default folder name in public folder
    'default_logo_text' => 'logo', ## default logo text
    'logo_text_color' => '#000000',
    'avatar_text_color' => '#f1c40f',
    'avatar_background_color' => '#ffffff',
    'default_svg_path' => 'users', ## folder name will be created in public/{folder}/users
    'hash_svg_name' => true ## if you want to hash svg name by default true
];

you can change all configuration options as you want.

License

This package is open-source software licensed under the MIT license (MIT). Please see License File for more information.

Contact

If you have any questions or feedback, feel free to contact me via e-mail at abdallakaramdev@gmail.com