Getting started | laravel-og-image-go | Spatie       

 SPATIE  

laravel-og-image-go
===================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-og-image-go](https://spatie.be/docs/laravel-og-image-go/v1)  Basic-usage  Getting started

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-og-image-go/v1) 

  Getting started    
- [ Introduction ](https://spatie.be/docs/laravel-og-image-go/v1/introduction)
- [ Requirements ](https://spatie.be/docs/laravel-og-image-go/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-og-image-go/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-og-image-go/v1/questions-issues)
- [ Testing ](https://spatie.be/docs/laravel-og-image-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-og-image-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-og-image-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-og-image-go/v1/support-us)
- [ About us ](https://spatie.be/docs/laravel-og-image-go/v1/about-us)

Basic usage
-----------

- [ How it works ](https://spatie.be/docs/laravel-og-image-go/v1/basic-usage/how-it-works)
- [ Getting started ](https://spatie.be/docs/laravel-og-image-go/v1/basic-usage/getting-started)
- [ Customizing screenshots ](https://spatie.be/docs/laravel-og-image-go/v1/basic-usage/customizing-screenshots)
- [ Defining fallback images ](https://spatie.be/docs/laravel-og-image-go/v1/basic-usage/defining-fallback-images)
- [ Caching and storage ](https://spatie.be/docs/laravel-og-image-go/v1/basic-usage/managing-caching-and-storage)
- [ Pre-generating images ](https://spatie.be/docs/laravel-og-image-go/v1/basic-usage/pre-generating-images)
- [ Clearing generated images ](https://spatie.be/docs/laravel-og-image-go/v1/basic-usage/clearing-generated-images)

Advanced usage
--------------

- [ Customizing the page URL ](https://spatie.be/docs/laravel-og-image-go/v1/advanced-usage/customizing-the-page-url)
- [ Using a custom screenshot driver ](https://spatie.be/docs/laravel-og-image-go/v1/advanced-usage/using-a-custom-screenshot-driver)
- [ Customizing the screenshot layout ](https://spatie.be/docs/laravel-og-image-go/v1/advanced-usage/customizing-the-screenshot-layout)
- [ Customizing actions ](https://spatie.be/docs/laravel-og-image-go/v1/advanced-usage/customizing-actions)
- [ Troubleshooting ](https://spatie.be/docs/laravel-og-image-go/v1/advanced-usage/troubleshooting)
- [ Using a hosted solution ](https://spatie.be/docs/laravel-og-image-go/v1/advanced-usage/using-a-hosted-solution)
- [ Using Laravel Boost ](https://spatie.be/docs/laravel-og-image-go/v1/advanced-usage/using-laravel-boost)

 Getting started
===============

###  On this page 

1. [ The template ](#content-the-template)
2. [ Migrating from manual meta tags ](#content-migrating-from-manual-meta-tags)
3. [ Using a view ](#content-using-a-view)
4. [ Using an existing image URL ](#content-using-an-existing-image-url)
5. [ Specifying the image format ](#content-specifying-the-image-format)
6. [ Previewing your OG image ](#content-previewing-your-og-image)
7. [ Design tips ](#content-design-tips)

The template
--------------------------------------------------------------------------------------------

Place an inert OG image template anywhere in your page view:

```
<template data-og-image>
    <div class="w-full h-full bg-blue-900 text-white flex items-center justify-center">
        <h1 class="text-6xl font-bold">{{.Title}}</h1>
    </div>
</template>
```

Use Laravel Go's regular `web.View` to render the page. The middleware hashes the rendered template content, remembers the current page URL and injects these tags into `<head>`:

```
<meta property="og:image" content="https://yourapp.com/og-image/5d41402abc4b2a76b9719d911017c592.jpeg">
<meta name="twitter:image" content="https://yourapp.com/og-image/5d41402abc4b2a76b9719d911017c592.jpeg">
<meta name="twitter:card" content="summary_large_image">
```

The image URL contains a hash of your HTML content. When that content changes, the hash changes. The first image request generates the screenshot. Later requests serve the stored image.

Migrating from manual meta tags
-----------------------------------------------------------------------------------------------------------------------------------------------------

Remove existing `og:image`, `twitter:image`, and `twitter:card` tags. The package handles these automatically. Keep your other Open Graph tags, including `og:title`, `og:description`, and `og:type`.

Using a view
--------------------------------------------------------------------------------------------

Use a Go template partial inside the inert template:

```
<template data-og-image>{{template "og-card" .}}</template>
```

You can also render a namespaced or application view with `view.Render(r, "og-image.post", data)`, wrap the result with `ogimage.HTML(string(rendered))`, and pass the returned `template.HTML` to your page data. Rendered content is not compiled a second time, so literal `{{ }}` in user content stays literal.

Using an existing image URL
-----------------------------------------------------------------------------------------------------------------------------------------

If you already have an OG image, reference it directly:

```
<template data-og-image data-og-url="https://yourapp.com/images/custom.png"></template>
```

The package skips screenshot generation and metadata caching. `ogimage.ImageURL(url)` produces the same markup from Go and validates the URL. Direct URLs must use HTTP or HTTPS without embedded credentials.

Specifying the image format
-----------------------------------------------------------------------------------------------------------------------------------------

The default format is JPEG. Override it per template:

```
<template data-og-image data-og-format="webp">
    <h1>{{.Title}}</h1>
</template>
```

Use `ogimage.HTML(content, ogimage.Options{Format: "webp"})` when building markup in Go. Pass only trusted, already-escaped HTML to `HTML`.

Previewing your OG image
--------------------------------------------------------------------------------------------------------------------------------

Append `?ogimage` to a page URL. This renders just the template content at the configured dimensions, using the page's full `<head>` with its CSS and fonts.

Design tips
-----------------------------------------------------------------------------------------

- Design for 1200×630 CSS pixels by default
- Make the root element fill the viewport
- Use flex or grid for layout
- Keep text large enough to read as a thumbnail
- Preview with `?ogimage` before sharing a page

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
