Skip to content

Installation ​

Requirements ​

Laravel Go Image requires Go 1.27.4 or higher and Laravel for Go 0.1.1 or higher. It needs no C compiler. The system's libwebp is optional and makes WebP encoding faster.

Installing the package ​

You may add the package to your application's module with go get:

shell
go get github.com/WendellAdriel/laravel-go-image

Then list it in the Packages of your application options, usually in bootstrap/app.go:

go
package bootstrap

import (
	laravel "go.laravel.com"

	image "github.com/WendellAdriel/laravel-go-image"
)

func Options() laravel.Options {
	return laravel.Options{
		Packages: []laravel.Package{image.New()},
		// ...
	}
}

At startup, the package reads its configuration and installs a *image.Manager. Handlers reach it with image.From:

go
img, err := image.From(r).Open("storage/app/photos/cat.jpg")

Using the package outside an application ​

image.New() also works on its own, such as in a command-line tool or a test. Outside an application, the manager uses the default settings and any configure functions you pass:

go
manager := image.New()

img, err := manager.Open("cat.jpg")
if err != nil {
	return err
}

return img.Scale(800, 0).Save("cat-small.webp")

WebP encoding ​

The package encodes WebP with gen2brain/webp, which runs libwebp without cgo. At startup, it looks for the system's libwebp and libwebpdemux libraries. When it finds them, it encoded a 1920x1080 test image in about 50 ms on the benchmark machine. Otherwise, it runs libwebp compiled to WebAssembly, which took about 230 to 265 ms for the same image.

You may install the libraries with your system's package manager:

shell
# Debian and Ubuntu
apt install libwebp-dev

# macOS
brew install webp

The library that loads libwebp links the binary dynamically, even with CGO_ENABLED=0. To build a static binary, such as for a FROM scratch container, turn off cgo and add the nodynamic tag. That binary always uses the WebAssembly build:

shell
CGO_ENABLED=0 go build -tags nodynamic .

Next steps ​

Released under the MIT License.