Skip to content

Overview ​

Introduction ​

Laravel Go Image brings Intervention Image and its Laravel integration, intervention/image-laravel, to Laravel for Go. It reads images from files, uploads, storage disks, and raw bytes, changes them with chainable modifiers, and encodes them into files or HTTP responses.

The package is written in Go and needs no cgo. It follows Go and Laravel for Go conventions instead of copying the PHP API word for word. Methods take explicit arguments, errors are values, and an *image.Image works anywhere the standard library expects an image.Image.

How it works ​

You install the package once in your application options. Handlers reach the configured Manager with image.From(r), read an image, modify it, and return it:

go
package avatars

import (
	"go.laravel.com/validation"
	"go.laravel.com/validation/rule"
	"go.laravel.com/web"

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

type UpdateAvatar struct {
	Avatar *web.Upload `json:"avatar"`
}

func (f *UpdateAvatar) Rules() validation.Rules {
	return validation.For(f, rule.Field(&f.Avatar, rule.Required, rule.Image))
}

func Show(r *web.Request) *web.Response {
	form, err := r.Validate[UpdateAvatar]()
	if err != nil {
		return web.Fail(err)
	}

	img, err := image.From(r).Upload(form.Avatar)
	if err != nil {
		return web.Fail(err)
	}

	return image.Response(img.Cover(256, 256, image.Center).Grayscale(), image.WEBP, image.Quality(80))
}

Every modifier changes the image in place and returns it, so calls chain. When a modifier fails, the image keeps the first error and skips the remaining modifiers. image.Response, Encode, Save, and Store return that error, so you check for errors once at the end.

What it supports ​

The package decodes JPEG, PNG, GIF, WebP, BMP, and TIFF, and encodes the same formats. Images may hold up to 2^26 pixels (about 67 megapixels) across all their frames. It recognizes AVIF and HEIC but returns image.ErrNotSupported for them, because no Go implementation exists without cgo.

Images can be resized, cropped, rotated, flipped, trimmed, filtered, and combined. You may also write text, draw shapes, and keep or slice GIF animations. Its settings mirror the autoOrientation, decodeAnimation, and backgroundColor options of intervention/image-laravel's config file.

Documentation ​

If you are adding Laravel Go Image to an application for the first time, read the docs in this order:

If you already know Intervention Image, start with Coming from Intervention Image.

Released under the MIT License.