Reading images
- Introduction
- Reading files
- Reading uploads
- Reading from storage disks
- Reading bytes, streams, and encoded strings
- Creating images in code
- Supported formats
- Size limits
Introduction
The *image.Manager reads images from every source your application deals with. In a handler, reach it with image.From(r). Each reading method returns an *image.Image and an error.
The manager detects the format from the data itself, not from a file name or a content type. A PNG saved as photo.jpg still decodes as a PNG.
Reading files
Open reads the image file at a path:
img, err := image.From(r).Open("resources/images/logo.png")
if err != nil {
return web.Fail(err)
}If the file doesn't exist, the error matches fs.ErrNotExist. The image remembers its path, so img.Save("") writes back to the same file.
Reading uploads
Upload reads a *web.Upload from a validated form. Make the field required, because Upload returns an error for a missing file:
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 Update(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)
}
// ...
}When the upload isn't an image, the error matches image.ErrDecoder, and web.Fail answers with 422 Unprocessable Entity.
Reading from storage disks
Load reads an image from a storage disk. Pass a context, the disk, and the path on the disk:
img, err := image.From(r).Load(r, storage.From(r), "avatars/42.png")Reading bytes, streams, and encoded strings
The manager also reads images from memory:
img, err := manager.Decode(data) // the bytes of an image file
img, err := manager.Read(response.Body) // any io.Reader
img, err := manager.Base64(encoded) // a Base64 string
img, err := manager.DataURI(uri) // data:image/png;base64,...If you already hold a standard library image.Image, FromImage copies it:
img := manager.FromImage(thumbnail)Creating images in code
Canvas creates a transparent image of a given size. You may fill it, draw on it, and insert other images into it:
banner := image.From(r).Canvas(1200, 630).Fill("#0b1118")Supported formats
| Format | Decodes | Encodes |
|---|---|---|
| JPEG | Yes | Yes |
| PNG | Yes | Yes |
| GIF | Yes, with animations | Yes, with animations |
| WebP | Yes, still images | Yes, lossy or lossless |
| BMP | Yes | Yes |
| TIFF | Yes | Yes |
| AVIF | No | No |
| HEIC | No | No |
The manager recognizes AVIF and HEIC data and returns an error that matches image.ErrNotSupported. web.Fail answers that error with 415 Unsupported Media Type.
Size limits
An image may hold up to 2^26 pixels (67,108,864) across all its frames, such as an 8192x8192 photo or a 100-frame 800x800 animation. The manager checks the size in the file's header before it decodes the pixels, so a small file that claims a huge size can't exhaust memory. A larger image returns an error that matches image.ErrDecoder.
Modifiers check the same limit. A modifier that would make the image larger, such as Resize or ResizeCanvas, sets the image's error instead.