Home Laravel How to Take Website Screenshot From URL in Laravel?

How to Take Website Screenshot From URL in Laravel?

by Shahriar Sagor

Hello Dev’s today now in this example, I will teach you how to take website screenshot from url in laravel. if you want to know about a sample example of how to take screenshot in landscape url laravel then i hope you are in the right place. So let’s discuss laravel and then take a screenshot of the website url. It’s a very simple example of how to take screenshots in laravel with browsershot. Just Follow the below step to know how to take a screenshot in laravel application.

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

Here i will use spatie/browsershot composer package to take a screenshot of the website in laravel. Here i will use url(), setOption(), windowSize(), waitUntilNetworkIdle() and save() method for capture browser screenshot in laravel application.

so let’s follow the below steps:

Step 1 : Install Laravel

At first of all i have to get a fresh Laravel version application by using the bellow command, So need to open our terminal OR command prompt and then run the bellow command:

composer create-project laravel/laravel example-app

Step 2: Install spatie/browsershot Package

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

composer require spatie/browsershot

Next, i have to install the puppeteer npm package, which is used to capture the screenshot. let’s installed by using the below command:

npm install puppeteer --global

Step 3: Create Route

Now in this step i have to create one route for capturing browser screenshot. let’s just 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

Now in this step, i have to create DemoController also with index()method.

Just 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.codingspoint.com/')
            ->setOption('landscape', true)
            ->windowSize(3840, 2160)
            ->waitUntilNetworkIdle()
            ->save('codingspoint.jpg');
  
        dd("Done");
    }
}

Read Also: How To Use JQuery Ajax Loading Spinner In Laravel ?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like