Extending with macros | laravel-screenshot | Spatie

 SPATIE

  Laravel Screenshot
=====================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-screenshot](https://spatie.be/docs/laravel-screenshot/v1)  Advanced-usage  Extending with macros

 Version   v1

 Other versions for crawler [v1](https://spatie.be/docs/laravel-screenshot/v1)

- [ Introduction ](https://spatie.be/docs/laravel-screenshot/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-screenshot/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-screenshot/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-screenshot/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-screenshot/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-screenshot/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-screenshot/v1/about-us)

Basic usage
-----------

- [ Taking screenshots ](https://spatie.be/docs/laravel-screenshot/v1/basic-usage/taking-screenshots)
- [ Customizing screenshots ](https://spatie.be/docs/laravel-screenshot/v1/basic-usage/customizing-screenshots)
- [ Saving screenshots to disks ](https://spatie.be/docs/laravel-screenshot/v1/basic-usage/saving-screenshots-to-disks)
- [ Queued screenshot generation ](https://spatie.be/docs/laravel-screenshot/v1/basic-usage/queued-screenshot-generation)
- [ Testing screenshots ](https://spatie.be/docs/laravel-screenshot/v1/basic-usage/testing-screenshots)

Drivers
-------

- [ Configuration ](https://spatie.be/docs/laravel-screenshot/v1/drivers/configuration)
- [ Customizing Browsershot ](https://spatie.be/docs/laravel-screenshot/v1/drivers/customizing-browsershot)
- [ Using the Cloudflare driver ](https://spatie.be/docs/laravel-screenshot/v1/drivers/using-the-cloudflare-driver)
- [ Custom drivers ](https://spatie.be/docs/laravel-screenshot/v1/drivers/custom-drivers)

Advanced usage
--------------

- [ Extending with macros ](https://spatie.be/docs/laravel-screenshot/v1/advanced-usage/extending-with-macros)

 Extending with macros
=====================

###  On this page

1. [ Registering macros ](#content-registering-macros)
2. [ Using macros ](#content-using-macros)

The `ScreenshotBuilder` class uses Laravel's `Macroable` trait, which means you can add custom methods to it.

Registering macros
--------------------------------------------------------------------------------------------------------------

You can register macros in the `boot` method of a service provider:

```
use Spatie\LaravelScreenshot\ScreenshotBuilder;

// in a service provider

public function boot(): void
{
    ScreenshotBuilder::macro('mobile', function () {
        return $this
            ->size(375, 812)
            ->deviceScaleFactor(3);
    });

    ScreenshotBuilder::macro('desktop', function () {
        return $this
            ->size(1920, 1080)
            ->deviceScaleFactor(2);
    });
}
```

Using macros
--------------------------------------------------------------------------------------------

Once registered, you can use your macros like any other builder method:

```
use Spatie\LaravelScreenshot\Facades\Screenshot;

Screenshot::url('https://example.com')
    ->mobile()
    ->save('mobile.png');

Screenshot::url('https://example.com')
    ->desktop()
    ->save('desktop.png');
```

This is useful for defining common screenshot configurations that you use across your application.
