Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram
Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram Pinterest LinkedIn Reddit
Readytocode
You are at:Home How to Generate image from text with Laravel 10

Laravel

How to Generate image from text with Laravel 10

JustinBy JustinMay 16, 2023No Comments2 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email




Numerous articles on generating images from text using the intervention image package can be found on the internet; however, they tend to be flawed. To create an image from a string, please adhere to the straightforward steps provided below.

Step : 1 – Installation of Laravel Application

I hope you already installed laravel.

 

Step : 2 – Add package on your Laravel project

Now you need below package for image create and modify.

composer require intervention/image

 

Step : 3 – Add provider path and alias path

Once your installation done add this to config/app.php

'providers' => ServiceProvider::defaultProviders()->merge([
        ....
        ....
        Intervention\Image\ImageServiceProvider::class,
])->toArray(),

'aliases' => Facade::defaultAliases()->merge([
        'Image' => Intervention\Image\Facades\Image::class,  
])->toArray(),

 

Step 4 : Create route

route/web.php

Route::get('/generate-image-horizental',[HomePageController::class,"createImage"]);

 

Step 5 : Create Function for Image Generate

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Intervention\Image\Facades\Image;

class HomePageController extends Controller
{
    public function index(){
        $locale = App::currentLocale();
        return view('home_page');
    }

    public function createImage(){
        $path = public_path('assets/images/blank.jpeg');

        $img = Image::make($path);

        $file_name = time().'_'.'readytocode.net';

        $img->text('readytocode.net', 300, 200,function ($font) {
            $font->file(public_path('/Fonts/sagitty-list.ttf'));
            $font->size(54);
            $font->color('#000000');
            $font->align('center');
            $font->valign('middle');
            $font->angle(0);
        });

        $img->save(public_path('pictures/'.$file_name.'.png'));
    }
}

 

Step 6 – Run your project

 

php artisan serv

http://127.0.0.1:8000/generate-image-horizental

Output:

Output

You can use your desire font and color.

Explanation (Must Read)

$path = public_path('assets/images/blank.jpeg');

Initially, it’s necessary to generate a standard underlying image possessing a backdrop shade utilizing paint or any alternative picture editing software. Subsequently, the script is adjusted to output the text on this picture. In this case, I formed an image having a black backdrop entitled blank.jpeg utilizing paint, having a picture resolution of 600 x 400.

$font->file(public_path('/Fonts/sagitty-list.ttf'));

If you don’t specify the font file path, an error will be displayed. To prevent this, I searched for “sagitty-list.ttf” on Google and downloaded it to my project’s Fonts directory. Following that, I assigned the font path in the function.

 

$img->save(public_path('pictures/'.$file_name.'.png'));

Specify the directory where you wish to store the newly created image and name the file as you see fit.

Thanks for Reading….

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Share 0

Like this:

Like Loading...

Related

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Justin
  • Website

Related Posts

Laravel 10 Restrict User Access

May 3, 2023

Laravel 10 Pagination Example

May 3, 2023

What’s New in Laravel 10

February 17, 2023

Comments are closed.

Featured Posts
  • Why meta tag is important for seo 1
    How to change react Page title, Page Description and some other meta Tag
    • August 4, 2023
  • 2
    How to Generate image from text with Laravel 10
    • May 16, 2023
  • Laravel 10 Restrict User Access 3
    Laravel 10 Restrict User Access
    • May 3, 2023
  • Laravel 10 Pagination Example 4
    Laravel 10 Pagination Example
    • May 3, 2023
  • install Steam on Ubuntu 5
    How to install Steam on Ubuntu 22.04 step by step
    • April 20, 2023



Readytocode
Facebook X (Twitter) Instagram Pinterest
  • About Us
  • Privacy Policy
© 2025 ReadyToCode.

Type above and press Enter to search. Press Esc to cancel.

%d