Skip to content

Resizing and cropping ​

Introduction ​

Resizing methods change the image in place and return it, so you may chain them with other modifiers. Methods that place the image take a position, such as image.Center or image.BottomRight.Offset(10, 10).

Resize, Scale, Cover, and Contain have Down variants that never make the image larger than it is. Use the Down variants for user uploads, where upscaling only adds blur and bytes.

The manager decodes a JPEG's pixels only when a method needs them. When the first method to run on a freshly read JPEG is one of these four, or a Down variant, it decodes the photo at 1/2, 1/4, or 1/8 of its size whenever the result is that much smaller, and Cover with an alignment decodes only the area it keeps from a photo stored upright. A thumbnail of a large photo then costs a fraction of a full decode. Its colors may differ slightly from those of a resized full-size decode: usually by 10 or fewer of 255 levels, and up to about 20 near the edges of very small results.

Resizing ​

Resize stretches the image to an exact width and height. A width or height of 0 keeps that side as it is:

go
img.Resize(300, 200)
img.Resize(300, 0) // keeps the height

ResizeDown resizes the same way but never makes a side larger.

Scaling ​

Scale keeps the aspect ratio. Given one side and 0 for the other, it scales to that side. Given both, it scales to fit within them:

go
img.Scale(800, 0)   // 800 pixels wide
img.Scale(800, 800) // fits in an 800x800 box

ScaleDown scales the same way but never makes the image larger.

Cover and contain ​

Cover fills an exact size. It crops the largest area with the target's aspect ratio at the position, then scales that area. This is the usual choice for thumbnails and avatars:

go
img.Cover(300, 300, image.Center)

Contain fits the whole image within a size, then places it on a canvas of exactly that size, filled with the image's background color:

go
img.SetBackground("transparent").Contain(800, 800, image.Center)

CoverDown and ContainDown never make the image larger.

Cropping ​

Crop cuts out an area of a width and height at a position:

go
img.Crop(800, 600, image.Center)
img.Crop(200, 100, image.TopLeft.Offset(40, 20))

An offset moves away from the edges its alignment names, so image.BottomRight.Offset(10, 10) ends 10 pixels left of and above the bottom-right corner. Parts of the area outside the image get the image's background color.

Resizing the canvas ​

ResizeCanvas changes the image's size without scaling it. New areas get the background color, and a side of 0 stays as it is:

go
img.ResizeCanvas(1200, 0, image.Center)

ResizeCanvasRelative grows the canvas by a number of pixels, or shrinks it for negative values:

go
img.SetBackground("#000").ResizeCanvasRelative(40, 40, image.Center) // a 20 pixel frame

Trimming ​

Trim removes borders that match the average color of the image's corners. Its tolerance, from 0 to 100, is how far a color may differ from the corners' color and still be trimmed:

go
img.Trim(10)

Animations can't be trimmed. Trimming one sets the image's error.

Rotating, flipping, and orienting ​

Rotate turns the image clockwise by degrees, or counterclockwise for a negative angle. Quarter turns move pixels exactly. Other angles grow the image to hold the turned image and fill the corners with the background color:

go
img.Rotate(90)
img.SetBackground("transparent").Rotate(-15)

Flip mirrors the image. image.Horizontal swaps left and right, and image.Vertical swaps top and bottom:

go
img.Flip(image.Horizontal)

Orient turns the image upright by its Exif orientation. The manager already does this while decoding unless you turn off IMAGE_AUTO_ORIENTATION.

Released under the MIT License.