# How to generate OG images in Laravel

Source: https://snapsbrew.com/guides/laravel-og-images

In Laravel, install the Snapsbrew PHP SDK, set the instance URL and the signing key in .env, and call Snapsbrew::signedUrl() with the layer values of the page. Put the URL in the og:image meta tag of your Blade layout. The first crawler request renders the image, and later requests get the stored file.

By [Alejandro Medina](https://amedina.me), maintainer of Snapsbrew. Published 27 November 2026. Last updated 27 November 2026.

On this page

- [What you need](#what-you-need)
- [Step 1: Install the PHP SDK](#step-1-install-the-php-sdk)
- [Step 2: Configure .env](#step-2-configure-env)
- [Step 3: Sign the URL](#step-3-sign-the-url)
- [Step 4: Put it in the layout](#step-4-put-it-in-the-layout)
- [Step 5: Show the same image on the page](#step-5-show-the-same-image-on-the-page)
- [Step 6: Test it](#step-6-test-it)
- [Rails](#rails)

A Laravel site often has hundreds of pages of one kind: posts, products, events, job ads. Each one deserves a share image with its own title. You can draw these images with GD or Imagick in a job, but then a font change is a code change, and every layout fix needs a deploy.

This guide moves the image out of your Laravel app. A designer owns a template in the Snapsbrew editor. Your app signs a URL with the values of the page. The Snapsbrew instance renders the image the first time a crawler asks for it.

The guide ends with the same setup for Rails.

## What you need

- A Snapsbrew instance that the public internet can reach. Read [Install with Docker](https://snapsbrew.com/docs/getting-started/installation-docker.md).
- Laravel 11, 12 or 13 on PHP 8.2 or later. Laravel 13 needs PHP 8.3.
- A published template with the `open_graph` size preset (1200x630). This guide uses two text layers, `title` and `subtitle`, and one image layer, `cover`.
- A signed-URL key from **Account > Developers > Signed URL API keys**.

## Step 1: Install the PHP SDK

The package is not on Packagist yet. Add the GitHub repository to `composer.json`:

```json
{
    "repositories": [
        { "type": "vcs", "url": "https://github.com/amedina068/snapsbrew-php" }
    ]
}

```

Then install the `main` branch:

```bash
composer require snapsbrew/snapsbrew-php:dev-main

```

The repository has no release tag yet, so the `:dev-main` suffix is necessary. The SDK needs a PSR-18 HTTP client. If your project has none, run `composer require guzzlehttp/guzzle`.

The service provider and the facade load automatically.

## Step 2: Configure .env

```
SNAPSBREW_URL=https://snapsbrew.example
SNAPSBREW_SIGNING_KEY=<signed-url-key>
SNAPSBREW_OG_TEMPLATE=<template-uuid>

```

`SNAPSBREW_OG_TEMPLATE` is not a setting of the SDK. It is your own value. Read it through a config file, so that `php artisan config:cache` keeps it:

```php
// config/services.php
'snapsbrew' => [
    'og_template' => env('SNAPSBREW_OG_TEMPLATE'),
],

```

The signed-URL key can only sign image URLs. It cannot call the REST API. For API calls, the SDK reads `SNAPSBREW_TOKEN`. This guide does not need it.

## Step 3: Sign the URL

Make the URL where you have the model, for example in a controller:

```php
use Snapsbrew\Laravel\Facades\Snapsbrew;

public function show(Post $post)
{
    $ogImage = Snapsbrew::signedUrl(config('services.snapsbrew.og_template'))
        ->text('title', $post->title)
        ->text('subtitle', $post->author->name.' · '.$post->published_at->format('j M Y'))
        ->image('cover', $post->cover_url)
        ->format('png')
        ->url();

    return view('posts.show', compact('post', 'ogImage'));
}

```

The builder also has `background()` for a color, `qr()`, `barcode()` and `hide()`. Each method maps to one variant. The `image()` value must be a public `http` or `https` URL. The instance refuses private and loopback hosts.

If many controllers need it, move the call to a method on the model:

```php
// app/Models/Post.php
public function ogImageUrl(): string
{
    return Snapsbrew::signedUrl(config('services.snapsbrew.og_template'))
        ->text('title', $this->title)
        ->text('subtitle', $this->author->name)
        ->url();
}

```

## Step 4: Put it in the layout

In the Blade layout, print the tag when the view has an image:

```
{{-- resources/views/layouts/app.blade.php --}}
<head>
    <title>@yield('title', config('app.name'))</title>
    @isset($ogImage)
        <meta property="og:image" content="{{ $ogImage }}">
        <meta property="og:image:width" content="1200">
        <meta property="og:image:height" content="630">
        <meta name="twitter:card" content="summary_large_image">
        <meta name="twitter:image" content="{{ $ogImage }}">
    @endisset
</head>

```

Blade escapes the `&` of the query string as `&amp;`. That is correct HTML. Crawlers read it as `&`.

## Step 5: Show the same image on the page

The Blade component signs the URL on the server and writes the `<img>` tag. Other attributes go to the tag.

```
<x-snapsbrew::image
    :template="config('services.snapsbrew.og_template')"
    :variants="[['layer' => 'title', 'text' => $post->title]]"
    format="webp"
    alt="{{ $post->title }}"
    class="rounded-lg"
/>

```

Note that a different variant list or format makes a different URL, and so a second render.

## Step 6: Test it

```bash
php artisan tinker --execute="echo App\Models\Post::first()->ogImageUrl();"
curl -sI "<the URL>"

```

A `302` with a `Location` header is a good render. A `200` with a small gray PNG is the placeholder: the key, the signature or the template ID is wrong. A `400` means a variant broke a rule.

To render the image before the first share, request the URL one time after you publish the post. An event listener on `PostPublished` can do it with `Http::withoutRedirecting()->get($post->ogImageUrl())`.

## Rails

The Ruby gem has the same signature rules and a view helper for the meta tag. Add it to the Gemfile:

```ruby
gem "snapsbrew", github: "amedina068/snapsbrew-ruby"

```

Set `SNAPSBREW_URL` and `SNAPSBREW_SIGNING_KEY` in the environment. Then, in the view:

```erb
<% content_for :head do %>
  <%= snapsbrew_og_image_tag template: ENV["SNAPSBREW_OG_TEMPLATE"], title: @post.title %>
<% end %>

```

Each keyword other than `template`, `variants`, `format` and `transparent` is a layer key. `snapsbrew_image_tag` writes an `<img>` tag in the same way.

## Questions

Does the Laravel app need a queue worker for this?

No. The Laravel app only signs a URL, which is one HMAC calculation. The Snapsbrew instance renders the image when the URL is first requested.

Can I use the same template for the og:image and for an image on the page?

Yes. The `<x-snapsbrew::image>` Blade component signs the URL and writes an `<img>` tag. Use the same template ID and variants.

Is the SDK on Packagist?

Not yet. Install it from the public GitHub repository with a VCS repository entry in `composer.json` and the `dev-main` version.

Snapsbrew is also a Laravel app. Can I run it inside my app?

No. Snapsbrew is a separate app with its own database, queue and renderer. Run it next to your app, for example with Docker Compose on the same server, and call it over HTTP.

## Related pages

- [How to generate dynamic OG images in Next.js](https://snapsbrew.com/guides/nextjs-og-images.md)
- [How to generate dynamic OG images in Nuxt](https://snapsbrew.com/guides/nuxt-og-images.md)
- [Docs: SDKs for JavaScript, PHP, Python and Ruby](https://snapsbrew.com/docs/sdks/overview.md)
- [Docs: Signed URLs](https://snapsbrew.com/docs/using-snapsbrew/signed-urls.md)
- [Dynamic OG images compared: Satori, a headless browser or a template API](https://snapsbrew.com/blog/dynamic-og-images-compared.md)

## Run it on your own server

Docker Compose starts the app, the queue, MySQL, Redis and the renderer together.

[Self-host it](https://snapsbrew.com/docs/getting-started/installation-docker.md) [View on GitHub](https://github.com/amedina068/snapsbrew)

```bash
git clone https://github.com/amedina068/snapsbrew.git
cd snapsbrew
cp .env.docker.example .env   # then fill in .env
docker compose up -d --build
```
