This tutorial will show you how to take a screenshot in Laravel. This tutorial will teach you how to use Laravel to take a screenshot of a website’s URL. You can grasp the concept of taking a website screenshot from a URL in Laravel. We will assist you in demonstrating how to take a screenshot in Laravel. Okay, let’s get into the specifics.
This example works with Laravel 6, Laravel 7, Laravel 8, and Laravel 9.
To take a screenshot of the website in Laravel, we will use the spatie/browsershot composer package. To capture a browser screenshot in Laravel, we will use the url(), setOption(), windowSize(), waitUntilNetworkIdle(), and save() methods. So let’s take the following steps:
Step 1 : Install Laravel
First of all we need to get fresh Laravel version application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project laravel/laravel example-app
Step 2: Install spatie/browsershot Package
here, we will install spatie/browsershot package for take a screenshot of url in laravel. so, let’s run the bellow commands:
composer require spatie/browsershot
Next, we need to install puppeteer npm package, that used to capture screenshot. let’s installed using the below command:
npm install puppeteer --global
Step 3: Create Route
In this is step we need to create one route for capture browser screenshot. let’s add the below route on web.php file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DemoController;
/*
|--------------------------------------------------------------------------
| 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('demo', [DemoController::class,'index']);
Step 4: Create Controller
in this step, we need to create DemoController with index()method.
Add the below code on controller file.
app/Http/Controllers/DemoController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Spatie\Browsershot\Browsershot;
class DemoController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
Browsershot::url('https://www.itsolutionstuff.com')
->setOption('landscape', true)
->windowSize(3840, 2160)
->waitUntilNetworkIdle()
->save('itsolutionstuff.jpg');
dd("Done");
}
}
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output: