Creating images | browsershot-go | Spatie       

 SPATIE  

browsershot-go
==============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Browsershot-go](https://spatie.be/docs/browsershot-go/v1)  Usage  Creating images

 Version   v1      

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

  Creating images    
- [ Introduction ](https://spatie.be/docs/browsershot-go/v1/introduction)
- [ Support us ](https://spatie.be/docs/browsershot-go/v1/support-us)
- [ Requirements ](https://spatie.be/docs/browsershot-go/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/browsershot-go/v1/installation-setup)
- [ Upgrading ](https://spatie.be/docs/browsershot-go/v1/upgrading)
- [ Questions and issues ](https://spatie.be/docs/browsershot-go/v1/questions-issues)
- [ Testing ](https://spatie.be/docs/browsershot-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/browsershot-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/browsershot-go/v1/changelog)
- [ About us ](https://spatie.be/docs/browsershot-go/v1/about-us)

Usage
-----

- [ Introduction ](https://spatie.be/docs/browsershot-go/v1/usage/introduction)
- [ Creating images ](https://spatie.be/docs/browsershot-go/v1/usage/creating-images)
- [ Creating PDFs ](https://spatie.be/docs/browsershot-go/v1/usage/creating-pdfs)
- [ Creating HTML ](https://spatie.be/docs/browsershot-go/v1/usage/creating-html)

Miscellaneous options
---------------------

- [ Adding extra headers to every request ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/adding-extra-headers-to-every-request)
- [ Adding extra headers to the navigational request ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/adding-extra-headers-to-the-navigational-request)
- [ Changing the language of the browser ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/changing-the-language-of-the-browser)
- [ Changing the value of a dropdown ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/changing-the-value-of-a-dropdown)
- [ Clicking on the page ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/clicking-on-the-page)
- [ Connection to a remote chromium/chrome instance ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/connection-to-a-remote-chrome-instance)
- [ Disable sandboxing ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/disable-sandboxing)
- [ Fixing CORS issues ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/fixing-cors-options)
- [ Getting console output ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/getting-console-ouput)
- [ Getting failed requests ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/getting-failed-requests)
- [ Ignore HTTPS errors ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/ignore-https-errors)
- [ Passing environment variables to the browser ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/passing-environment-variables-to-the-browser)
- [ Performance ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/performance)
- [ Prevent unsuccessful responses ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/prevent-unsuccessful-responses)
- [ Sending POST requests ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/sending-post-requests)
- [ Setting an arbitrary option ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/setting-an-arbirary-option)
- [ Setting the CSS media type of the page ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/setting-the-css-media-type-of-the-page)
- [ Setting the timeout ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/setting-the-timeout)
- [ Setting the user agent ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/setting-the-user-agent)
- [ Specifying a proxy server ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/specifying-a-proxy-server)
- [ Typing on the page ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/typing-on-the-page)
- [ Using a pipe instead of a WebSocket ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/using-a-pipe-instead-of-a-websocket)
- [ Using cookies ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/using-cookies)
- [ Using HTTP authentication ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/using-http-authentication)
- [ Using URL for HTML content ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/using-url-for-html-content)
- [ Writing options to a file ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/writing-options-to-a-file)
- [ Disabling redirects ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/disabling-redirects)
- [ Read redirect history ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/read-redirect-history)
- [ Evaluating a script before page load ](https://spatie.be/docs/browsershot-go/v1/miscellaneous-options/evaluating-a-script-before-page-load)

 Creating images
===============

###  On this page 

1. [ Formatting the image ](#content-formatting-the-image)
2. [ Sizing the image ](#content-sizing-the-image)
3. [ Manipulating the image ](#content-manipulating-the-image)
4. [ Taking a full page screenshot ](#content-taking-a-full-page-screenshot)
5. [ Setting the device scale ](#content-setting-the-device-scale)
6. [ Mobile emulation ](#content-mobile-emulation)
7. [ Device emulation ](#content-device-emulation)
8. [ Backgrounds ](#content-backgrounds)
9. [ Dismiss dialogs ](#content-dismiss-dialogs)
10. [ Disable JavaScript ](#content-disable-javascript)
11. [ Disable images ](#content-disable-images)
12. [ Block URLs ](#content-block-urls)
13. [ Block domains ](#content-block-domains)
14. [ Waiting for lazy-loaded resources ](#content-waiting-for-lazy-loaded-resources)
15. [ Delayed screenshots ](#content-delayed-screenshots)
16. [ Waiting for a JavaScript function ](#content-waiting-for-a-javascript-function)
17. [ Waiting for a selector ](#content-waiting-for-a-selector)
18. [ Adding JS ](#content-adding-js)
19. [ Adding CSS ](#content-adding-css)
20. [ Output directly to the browser ](#content-output-directly-to-the-browser)
21. [ Setting the user data directory ](#content-setting-the-user-data-directory)

Here's the easiest way to create an image of a webpage:

```
err := browsershot.URL("https://example.com").Save(ctx, "example.png")
```

Formatting the image
--------------------------------------------------------------------------------------------------------------------

By default screenshots are PNG. Use JPEG with an optional quality, or WebP. The filename does not select the image format.

```
err := browsershot.URL("https://example.com").SetScreenshotType("jpeg", 90).Save(ctx, "example.jpg")
```

Sizing the image
--------------------------------------------------------------------------------------------------------

The default viewport is 800 × 600.

```
page := browsershot.URL("https://example.com").WindowSize(640, 480)
```

Capture a portion with `Clip(x, y, width, height)`, or an element with `Select(".some-selector", 3)`. Index 3 selects the fourth match. The default index is zero.

### Getting a screenshot as base64

```
data, err := browsershot.URL("https://example.com").Base64Screenshot(ctx)
```

Manipulating the image
--------------------------------------------------------------------------------------------------------------------------

Use `TransformImage` for application-defined processing. It receives encoded bytes and must return encoded bytes. It applies to `Screenshot`, `Base64Screenshot` and saved screenshots, not PDFs. Multiple transforms run in registration order and stop on the first error.

```
page := browsershot.URL("https://example.com").TransformImage(
    func(ctx context.Context, data []byte) ([]byte, error) {
        return myImageProcessor(ctx, data)
    },
)
```

No Go image-port dependency is required, and no internals are copied. Plug in your chosen released image library. PHP's dynamic image-method forwarding is replaced by this hook.

Taking a full page screenshot
-----------------------------------------------------------------------------------------------------------------------------------------------

```
page := browsershot.URL("https://example.com").FullPage()
```

Setting the device scale
--------------------------------------------------------------------------------------------------------------------------------

`DeviceScaleFactor(2)` or `DeviceScaleFactor(3)` captures at higher pixel density. Values are clamped to 1 through 3.

Mobile emulation
--------------------------------------------------------------------------------------------------------

Use `Mobile()` and `Touch()` with a mobile user agent. Pass `false` to disable either option.

```
page := browsershot.URL("https://example.com").UserAgent("My Mobile Browser 1.0").Mobile().Touch()
```

Device emulation
--------------------------------------------------------------------------------------------------------

`Device("iPhone X")` uses [Puppeteer's known devices](https://pptr.dev/api/puppeteer.knowndevices). It replaces the default viewport. Call `WindowSize` afterward if you want to override the device dimensions.

Backgrounds
-----------------------------------------------------------------------------------------

`HideBackground()` omits the page's default background. `ShowBackground()` restores it. CSS backgrounds remain subject to Chrome's screenshot behavior.

Dismiss dialogs
-----------------------------------------------------------------------------------------------------

`DismissDialogs()` closes alerts, prompts and confirmation dialogs that would otherwise block rendering.

Disable JavaScript
--------------------------------------------------------------------------------------------------------------

`DisableJavascript()` prevents scripts from executing. Some sites will not render correctly without JavaScript.

Disable images
--------------------------------------------------------------------------------------------------

`DisableImages()` blocks image requests and removes `<img>` elements from the page.

Block URLs
--------------------------------------------------------------------------------------

`BlockURLs([]string{"tracker.example/pixel"})` blocks URLs containing any listed substring.

Block domains
-----------------------------------------------------------------------------------------------

`BlockDomains([]string{"doubleclick.net", "google-analytics.com"})` blocks exact hostnames.

Waiting for lazy-loaded resources
-----------------------------------------------------------------------------------------------------------------------------------------------------------

`WaitUntilNetworkIdle()` waits for 500 ms without network activity. `WaitUntilNetworkIdle(false)` permits two active connections during that interval.

Delayed screenshots
-----------------------------------------------------------------------------------------------------------------

`Delay(500*time.Millisecond)` delays capture after the page interactions.

Waiting for a JavaScript function
-----------------------------------------------------------------------------------------------------------------------------------------------------------

`WaitForFunction("window.ready === true")` waits until an expression becomes truthy. Use `SetOption("functionPolling", "mutation")` or a millisecond interval to change polling. `SetOption("functionTimeout", 5000)` sets a function-specific timeout in milliseconds.

Waiting for a selector
--------------------------------------------------------------------------------------------------------------------------

```
page := browsershot.URL("https://example.com").WaitForSelector("#ready", map[string]any{"visible": true})
```

Adding JS
-----------------------------------------------------------------------------------

`AddScriptTag(map[string]string{"content": "window.ready = true"})` adds JavaScript before capture. Puppeteer's `url` and `path` options also work.

Adding CSS
--------------------------------------------------------------------------------------

`AddStyleTag(map[string]string{"content": "body { font-size: 14px }"})` adds CSS before capture.

Output directly to the browser
--------------------------------------------------------------------------------------------------------------------------------------------------

`Screenshot(ctx)` returns bytes. Write them to your HTTP response with the corresponding image content type.

Setting the user data directory
-----------------------------------------------------------------------------------------------------------------------------------------------------

`UserDataDir("/absolute/path/to/session")` stores browser session data. Reusing it can improve performance but may introduce cache problems. Do not open the same directory from simultaneous Chrome processes.

 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)
