How to Take Website Screenshot From URL in Laravel?

Dec 02, 2022

.

Admin

How to Take Website Screenshot From URL in Laravel?

Hello Friends,

In this example, I will show you how to take website screenshot from url in laravel. if you want to see a sample of how to take screenshot in landscape url laravel then you are in the right place. let’s discuss laravel and take a screenshot of the website url. it's a simple example of how to take screenshots in laravel with browsershot. Follow the below step to know how to take a screenshot in laravel.

You can use this example with laravel 6, laravel 7, laravel 8, and laravel 9 versions.

We will use spatie/browsershot composer package to take a screenshot of the website in laravel. we will use url(), setOption(), windowSize(), waitUntilNetworkIdle() and save() method to capture browser screenshot in laravel. so let's follow the below steps:

Step 1 : Install Laravel


first of all we need to get a fresh Laravel version application using the bellow command, So open your terminal OR command prompt and run the bellow command:

composer create-project laravel/laravel example-app

Step 2: Install spatie/browsershot Package

here, we will install spatie/browsershot package to take a screenshot of url in laravel. so, let's run the bellow commands:

composer require spatie/browsershot

Next, we need to install the puppeteer npm package, which is used to capture screenshot. let's installed using the below command:

npm install puppeteer --global

Step 3: Create Route

In this step we need to create one route for capturing browser screenshot. let's add the below route on the 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 the 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.nicesnippets.com/')

->setOption('landscape', true)

->windowSize(3840, 2160)

->waitUntilNetworkIdle()

->save('nicesnippets.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:

http://localhost:8000/demo

Output:

#Laravel