Laravel Take a Screenshot of the Website URL

Dec 02, 2022 . Admin



Hello Friends,

Here, I will show you the laravel take a screenshot of the website url. step by step explains laravel and how to take screenshots in laravel with browsershot. if you want to see an example of how to take a screenshot in laravel then you are in the right place. I explained simply how to take screenshot in landscape url laravel. Here, Creating a basic example of how to take website screenshot from url in laravel.

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

We will use the 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 the 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 capture 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.mywebtuts.com/')
            ->setOption('landscape', true)
            ->windowSize(3840, 2160)
            ->waitUntilNetworkIdle()
            ->save('mywebtuts.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