spatie/laravel-screenshot
Driver-based Laravel package for taking web page screenshots with great defaults. Use Browsershot (Chromium) or Cloudflare Browser Rendering, customize viewport/format/quality, save to files, and easily fake/assert screenshots in tests.
The ScreenshotBuilder class uses Laravel's Macroable trait, which means you can add custom methods to it.
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);
});
}
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.
How can I help you explore Laravel packages today?