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.
By default, OG images are generated lazily, on the first request from a crawler. This means the first crawler to request the image triggers a Chrome screenshot, which takes a few seconds. If many new pages go live at the same time, this can lead to multiple concurrent screenshot processes and put load on your server.
To avoid this, you can pre-generate images ahead of time.
php artisan og-image:generate https://yourapp.com/page1 https://yourapp.com/page2
generateForUrl()You can generate an OG image programmatically by passing a page URL:
use Spatie\OgImage\Facades\OgImage;
$imageUrl = OgImage::generateForUrl('https://yourapp.com/blog/my-post');
This fetches the page, extracts the <x-og-image> template, takes a screenshot, and saves it to disk.
A common pattern is to dispatch a queued job after saving content, so the OG image is ready before any crawler requests it:
use Spatie\OgImage\Facades\OgImage;
class PublishPostAction
{
public function execute(Post $post): void
{
// ... publish logic ...
dispatch(function () use ($post) {
OgImage::generateForUrl($post->url);
});
}
}
How can I help you explore Laravel packages today?