Customizing actions | laravel-og-image | Spatie

 SPATIE

  Laravel Open Graph Image
===========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-og-image](https://spatie.be/docs/laravel-og-image/v1)  Advanced-usage  Customizing actions

 Version   v1

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

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

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

- [ How it works ](https://spatie.be/docs/laravel-og-image/v1/basic-usage/how-it-works)
- [ Getting started ](https://spatie.be/docs/laravel-og-image/v1/basic-usage/getting-started)
- [ Customizing screenshots ](https://spatie.be/docs/laravel-og-image/v1/basic-usage/customizing-screenshots)
- [ Defining fallback images ](https://spatie.be/docs/laravel-og-image/v1/basic-usage/defining-fallback-images)
- [ Caching and storage ](https://spatie.be/docs/laravel-og-image/v1/basic-usage/managing-caching-and-storage)
- [ Pre-generating images ](https://spatie.be/docs/laravel-og-image/v1/basic-usage/pre-generating-images)
- [ Clearing generated images ](https://spatie.be/docs/laravel-og-image/v1/basic-usage/clearing-generated-images)

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

- [ Customizing the page URL ](https://spatie.be/docs/laravel-og-image/v1/advanced-usage/customizing-the-page-url)
- [ Using a custom screenshot driver ](https://spatie.be/docs/laravel-og-image/v1/advanced-usage/using-a-custom-screenshot-driver)
- [ Customizing the screenshot layout ](https://spatie.be/docs/laravel-og-image/v1/advanced-usage/customizing-the-screenshot-layout)
- [ Customizing actions ](https://spatie.be/docs/laravel-og-image/v1/advanced-usage/customizing-actions)
- [ Troubleshooting ](https://spatie.be/docs/laravel-og-image/v1/advanced-usage/troubleshooting)
- [ Using a hosted solution ](https://spatie.be/docs/laravel-og-image/v1/advanced-usage/using-a-hosted-solution)
- [ Using Laravel Boost ](https://spatie.be/docs/laravel-og-image/v1/advanced-usage/using-laravel-boost)

 Customizing actions
===================

###  On this page

1. [ Available actions ](#content-available-actions)
2. [ Overriding an action ](#content-overriding-an-action)

The package uses action classes for its core operations. You can replace any of them with your own implementation to customize the behavior. Each action has small, focused protected methods that you can override individually.

Available actions
-----------------------------------------------------------------------------------------------------------

The following actions are registered in the `og-image` config file:

```
'actions' => [
    'generate_og_image' => \Spatie\OgImage\Actions\GenerateOgImageAction::class,
    'inject_og_image_fallback' => \Spatie\OgImage\Actions\InjectOgImageFallbackAction::class,
    'render_og_image_screenshot' => \Spatie\OgImage\Actions\RenderOgImageScreenshotAction::class,
],
```

### GenerateOgImageAction

Handles the full flow when a social platform requests an OG image URL (`/og-image/{hash}.jpeg`): checking if the image exists on disk, looking up the page URL, taking a screenshot with locking, and serving the image directly.

Overridable methods: `serveImage`, `generateImage`, `respondWithImage`, `redirectToImage`.

### InjectOgImageFallbackAction

Handles injecting fallback OG image meta tags and template content into pages that don't have an `` component.

Overridable methods: `renderFallback`, `injectBeforeClosingTag`.

### RenderOgImageScreenshotAction

Handles rendering the screenshot page when `?ogimage` is appended to a URL. Extracts the template content and head from the page, and renders the screenshot view.

Overridable methods: `extractHead`.

Overriding an action
--------------------------------------------------------------------------------------------------------------------

Create a class that extends the default action and override the methods you want to customize:

```
namespace App\Actions;

use Spatie\OgImage\Actions\GenerateOgImageAction;

class CustomGenerateOgImageAction extends GenerateOgImageAction
{
    protected function generateImage(array $cached, string $path, $disk): void
    {
        // Custom generation logic...
    }
}
```

Then register it in `config/og-image.php`:

```
'actions' => [
    'generate_og_image' => \App\Actions\CustomGenerateOgImageAction::class,
],
```
