How to write text on image in Laravel and save

Generic placeholder image
Brijpal Sharma
laravel html  

Date:-November 04th, 2018 , Posted On:- Web Development


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','ImageController@textOnImage')->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.

 

 

 


About the author

user profile
Brijpal Sharma

Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...

0 comments

You must be logged in to post a comment.