Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Og Image

Laravel Og Image Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Generate OG images for your Laravel app

Frequently asked questions about Laravel Og Image
How do I generate Open Graph images for my Laravel blog posts using this package?
Define a Blade template for your OG image (e.g., `resources/views/og/post.blade.php`) with dynamic data like `{{ $post->title }}`. Use the `@ogImage` directive in your Blade view to reference the template. The package will auto-generate and cache the image when accessed via the dedicated route.
Does this package work with Laravel 10 and PHP 8.2?
Yes, `spatie/laravel-og-image` supports Laravel 8+ and PHP 8.0+. Check the [composer.json](https://github.com/spatie/laravel-og-image/blob/main/composer.json) for exact version requirements. For Laravel 10, ensure your `laravel-screenshot` dependency is also compatible.
Can I customize the OG image template beyond basic Blade variables?
Absolutely. Your OG templates inherit your app’s CSS, fonts, and Vite assets, so you can use any Blade logic, loops, or conditionals. For complex layouts, collaborate with your design team to create reusable Blade components for dynamic content like product cards or user profiles.
How does caching work, and can I clear stale OG images?
Generated images are cached on disk by default. To clear stale images, delete the cached files from `storage/app/public/og-images` or implement a cache invalidation strategy (e.g., delete files when related model data changes). The package doesn’t auto-purge cache, so manual handling is required.
Will this package slow down my Laravel app if I generate OG images on every request?
No, if configured properly. Use Laravel’s queue system to offload image generation to workers (e.g., Supervisor). Enable disk caching to serve pre-generated images on repeat requests. For high-traffic sites, monitor queue backlogs and consider async generation for non-critical routes.
Can I use this package with a CDN like Cloudflare or AWS CloudFront?
Yes, but you’ll need to configure your CDN to cache the OG image route (e.g., `/og-image/{id}`). Ensure the route’s cache headers (e.g., `Cache-Control`) are set to leverage CDN caching. For dynamic content, use cache busting (e.g., query strings) or purge the CDN when images change.
What happens if my OG image template fails to render (e.g., missing CSS or fonts)?
The package relies on your app’s existing assets (CSS, fonts, Vite) for rendering. If these are missing or misconfigured, the generated image may appear broken. Test templates locally before deployment and ensure all assets are accessible during screenshot generation (e.g., no blocked network requests).
Is there a way to generate OG images for API responses without Blade?
No, this package is Blade-centric. For API responses, consider generating OG images server-side using libraries like `spatie/pdf-to-image` or `imagick`, then serving them via a dedicated endpoint. Alternatively, use a headless Chrome service to render HTML dynamically.
How do I handle dynamic data in OG templates (e.g., user-generated content)?
Pass dynamic data to your OG template via the `@ogImage` directive’s parameters, e.g., `@ogImage('post', ['post' => $post])`. Ensure your template logic (e.g., loops, conditionals) handles edge cases like missing data. For user-generated content, validate inputs to avoid template errors.
What are the alternatives to this package, and when should I consider them?
Alternatives include external APIs like [Cloudinary](https://cloudinary.com/) or [Twilio’s ImageGen](https://www.twilio.com/docs/imagegen), which offer more features (e.g., real-time generation) but require external dependencies. Use this package if you want full control, no API costs, and Laravel-native integration. For simple static OG images, hardcoding a single template may suffice.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport