This Laravel package enables you to dynamically create Open Graph images for your website based on a single Blade template with HTML and CSS. In our example we use the Tailwind CDN. So designing a dynamic Open Graph Image as a developer just got very easy using this package!
Just add the meta tag with our url to the head of your page. The package will then generate the image and add it to the page. You can edit the view template which you can find in the resources folder.
- PHP 8.2+
You can install the package via composer:
composer require backstage/laravel-og-image
Run the command to install the package:
php artisan og-image:install
You should also publish the views, to change the default layout of your Open Graph images:
php artisan vendor:publish --tag="og-image-views"
This is the content of the published config file (published at config/og-image.php
):
return [
'extension' => 'jpg',
'quality' => 100,
'width' => 1200,
'height' => 630,
// The cache location to use.
'storage' => [
'disk' => 'public',
'path' => 'open-graph',
],
// Whether to use the browse URL instead of the HTML input.
// This is slower, but makes fonts available.
// Alternative: http
'method' => 'html',
'metatags' => [
'og:title' => 'title',
'og:description' => 'description',
'og:type' => 'type',
'og:url' => 'url',
],
];
Add the blade component into the head of your page. Providing the attributes you need in your view file:
<x-og-image title="Backstage" subtitle="" />
If you want to use a different view than the default, add a view
attribute with the path using dot or slash notation:
<x-og-image title="Backstage" subtitle="" view="path.to.view.file" />
If you do not want to use a view but HTML directly in your view file, than you can use the slot to add the HTML to:
Note
If you're using this option, make sure to clear caches before adding or changing the HTML using php artisan og-image:clear
to see the result in your browser.
<x-og-image title="Backstage" subtitle="" view="path.to.view.file">
<h1>Use this HTML and inline CSS to style the open graph image...</h1>
</x-og-image>
If you don't want to use the blade component you can also use the facade or helper method to generate the url to the image.
// Facade
use Backstage\OgImage\Laravel\Facades\OgImage;
$url = OgImage::url(['title' => 'Backstage', 'subtitle' => '...']);
// or using the `og()` helper
$url = og(['title' => 'Backstage', 'subtitle' => '...']);
And add it like this to your Blade file:
<meta property="og:image" content="{!! $url !!}">
<meta property="og:image:type" content="image/{{ config('og-image.extension') }}">
<meta property="og:image:width" content="{{ config('og-image.width') }}">
<meta property="og:image:height" content="{{ config('og-image.height') }}">
When you share the page on any platform, the image will automatically be generated, cached and then shown in your post. The image from the default template will look like this:
This component uses the 'template' blade view by default. You can change this template to your needs. It is even possible to pass more attributes than the default ones. You can find the default template in the resources folder.
Want to add more custom attributes to modify the button text for example? Simply pass them down to the blade component, facade or helper method:
<x-og-image
title="Backstage"
subtitle=""
button="Read more"
/>
// Facade
use Backstage\OgImage\Laravel\Facades\OgImage;
OgImage::url(['title' => 'Slimme websites', 'subtitle' => '...', 'button' => 'Read more']);
// Helper
og(['title' => 'Backstage', 'subtitle' => '...', 'button' => 'Read more']);
You can now access the variable in your view by using the {{ $button }}
variable.
When you need to generate the image without using the blade component, you can use the following method:
OgImage::createImageFromParams(['title' => 'Backstage', 'subtitle' => '...']);
This will return the actual image from your configured storage. You can use this method to generate the image in your own controller for example.
All generated open graph images are cached by default. If you want to remove the cache, you can use the following command:
php artisan og-image:clear-cache
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.