Taking screenshots of browsers is very easy in Laravel applications with the help of spatie/browsershot package. Just install spatie/browsershot in laravel project and use its object browsershot, you can take screenshots of browser.
How to Take Browser Screenshot in Laravel using spatie/browsershot Package
Steps to make or take browser screenshots in Laravel using spatie/browsershot Package:
Step 1 – Setup New Laravel Project
First of all, execute the following command on terminal or cmd to install new laravel new with latest version:
composer create-project laravel/laravel blog-example
Step 2 – Install spatie/browsershot Package
Then execute the following command on the terminal to install spatie/browsershot package in laravel apps:
composer require spatie/browsershot npm install puppeteer --global
Step 3 – Create Route
Navigate to routes directory and open web.php file, then create one route for capturing browser screenshot:
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\TestController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('test', [TestController::class,'index']);
Step 4 – Create Controller using Artisan
To create a controller class, simply run php artisan make:controller TestController
command to create it:
php artisan make:controller TestController
Navigate to controller folder/directory and open testcontroller.php and update the following function into it:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Spatie\Browsershot\Browsershot; class TestController extends Controller { /** * Write code on Method * * @return response() */ public function index(Request $request) { Browsershot::url('https://www.tutsmake.com') ->setOption('landscape', true) ->windowSize(3840, 2160) ->waitUntilNetworkIdle() ->save('tutsmake.jpg'); dd("Done"); } }
Step 5 – Run the Laravel App
Execute the following command on temrinal to start laravel apps:
php artisan serve
Now, Go to your web browser, hit the following URL into it:
http://localhost:8000/demo
Conclusion
That’s all; Through this tutorial, you have learned how to take/make browser screenshots in laravel using spatie/browsershot package.