How to write text on an image in Laravel and save it?

In this post, we learn how to write text on image in Laravel using intervention. we also learn how to save the image.

Step:1 Install Package

First, we need to add a Laravel package by the composer using the following line.

composer require intervention/image

After that, we need to add the provider path and alias in the config/app.php

'Intervention\Image\ImageServiceProvider'

And alias

'Image' => 'Intervention\Image\Facades\Image'

After That add a route in web.php.

Route::get('text-on-image','[email protected]')->name('textOnImage');

After that create a New Route using this flowing command.

php artisan make:controller ImageController 

Then, go the app/http/controllers find ImageController and add a new function called textOnImage.

public function textOnImage()  
    {  
       $img = Image::make(public_path('images/codermen.jpg'));  
       $img->text('Hello coderMen', 120, 100, function($font) {  
          $font->file(public_path('path/font.ttf'));  
          $font->size(28);  
          $font->color('#4285F4');  
          $font->align('center');  
          $font->valign('bottom');  
          $font->angle(0);  
      });  
       $img->save(public_path('images/text_with_image.jpg'));  
    }

Possible Error

Maybe it will give an error 

NotSupportedException in Font.php line 30: Internal GD font () not available. Use only 1-5.

Solution:

Remove public_path() from Image: make, file and save the class. and use this finale code.

public function textOnImage()  
    {  
       $img = Image::make('images/codermen.jpg');  
       $img->text('Hello coderMen', 120, 100, function($font) {  
          $font->file('path/font.ttf');  
          $font->size(28);  
          $font->color('#4285F4');  
          $font->align('center');  
          $font->valign('bottom');  
          $font->angle(0);  
      });  
       $img->save('images/text_with_image.jpg');  
    }

So, finally, we completed a tutorial on “how to write text on image in Laravel and save”.

if you have any dought feel free to ask in comment box.

Leave a Comment