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:
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….