Skip to content

Resize with fit, cover, and crop position

Copy page

A resize starts by choosing the canvas, then the crop. fit inside places the whole frame inside 400 × 400 without enlargement. cover fills 600 × 300. This landscape frame then loses a strip from the top or bottom. The kept region can be the centre, the top edge, or a high-entropy area. A square cover would only crop the sides, so north would not move the frame. The wider target makes the top crop visible.

The fixed-coordinate crop and arbitrary rotation in the geometry bundle are a different page: resize, crop, rotate, and flip.

Night photograph used for fit and cover
Input: JPEG1938 × 1292

The reference images come from govips v2.19.0 thumbnail loading on libvips 8.17.3, WebP quality 82, with enlargement disabled. north is libvips crop=low. On this wider target the crop is vertical, so the low coordinate keeps the top. sharp expresses the same product intent with position: "north".

Night scene scaled inside a 400-pixel box
fit inside400 × 267, 14,990 bytes
Night scene covered to 600 by 300 from the centre
cover centre600 × 300, 30,842 bytes
Night scene covered to 600 by 300 from the top
cover north600 × 300, 26,506 bytes
Night scene covered to 600 by 300 by entropy
cover entropy600 × 300, 30,600 bytes
if err := vips.Startup(nil); err != nil { return err }
defer vips.Shutdown()
write := func(name string, width, height int, crop vips.Interesting) error {
image, err := vips.NewThumbnailWithSizeFromFile("input.jpg", width, height, crop, vips.SizeDown)
if err != nil { return err }
defer image.Close()
params := vips.NewWebpExportParams()
params.Quality = 82
data, _, err := image.ExportWebp(params)
if err != nil { return err }
return os.WriteFile(name, data, 0o644)
}
if err := write("fit-inside.webp", 400, 400, vips.InterestingNone); err != nil { return err }
if err := write("cover-centre.webp", 600, 300, vips.InterestingCentre); err != nil { return err }
if err := write("cover-north.webp", 600, 300, vips.InterestingLow); err != nil { return err }
return write("cover-entropy.webp", 600, 300, vips.InterestingEntropy)

Fit is 400 × 267. Each cover is 600 × 300. All four are 3-band images without alpha. sharp uses its bundled libvips 8.18.7; the other three use 8.17.3. An entropy crop depends on image content, so one run does not define a fixed window.

Whether a large thumbnail can stay on a sequential path depends on the access mode. See sequential access, random access, and materialisation.