How To Capture Website Screenshot with Laravel Tutorial

Reading Time: 4 minutes
909 Views

Inside this article we will see the concept i.e How To Capture Website Screenshot with Laravel. Article contains the classified information about Capture Screenshot of Website from URL using Laravel.

Here, 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.

Tutorial is super easy to understand and implement it in your code as well.

Read More: Convert HTML to PDF Using Javascript Example Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Install spatie/browsershot Package

Here, we will install spatie/browsershot package which will be responsible to provide functions and methods for taking screenshots from URL in laravel.

Open project terminal and run this command.

$ composer require spatie/browsershot

Next,

Install Node Package “puppeteer”

This is also a working partner for last installed composer package when we work of taking screenshots from URL. This is a node package, we will install it via NPM.

Run this command into your project terminal.

$ npm install puppeteer --global

Now, everything is ready.

Read More: Laravel Move Data From One Table To Another Table Example

Just we need to create a application controller, route and a method to see the execution.

Create a Controller

Open project into terminal and run this command.

$ php artisan make:controller SiteController

It will create a SiteController.php file inside /app/Http/Controllers folder.

Open file and write this code into it.

<?php
 
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Spatie\Browsershot\Browsershot;
  
class SiteController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        Browsershot::url('https://skillshike.com/')
            ->setOption('landscape', true)
            ->windowSize(3840, 2160)
            ->waitUntilNetworkIdle()
            ->save('skillshike.jpg');
  
        dd("Done");
    }
}

Add Route

Open web.php file from /routes folder. Add this route into it.

//...
use App\Http\Controllers\SiteController;

Route::get('capture', [SiteController::class,'index']);

//...

Application Testing

Run this command into project terminal to start development server,

php artisan serve

URL: http://127.0.0.1:8000/capture

Read More: How To Check a Key Exists in JavaScript Object?

When you open this URL, it will capture a screenshot of entire website i.e of skillshike.com

We hope this article helped you to learn How To Capture Website Screenshot with Laravel Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more