spatie/laravel-og-image
Generate Open Graph images in Laravel from Blade-defined HTML. Automatically renders screenshots, serves them from a route, and caches files. Templates reuse your app’s CSS, fonts, and Vite assets—no external API required.
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.
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,
],
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.
Handles injecting fallback OG image meta tags and template content into pages that don't have an <x-og-image> component.
Overridable methods: renderFallback, injectBeforeClosingTag.
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.
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,
],
How can I help you explore Laravel packages today?