Images (PNG, JPEG, BMP, TIFF, WEBP, VP8, GIF):
*Resizing images
There are several libraries for resizing images in Go. Package golang.org/x/image/draw is one of the options:
func resize(src image.Image, dstSize image.Point) *image.RGBA {
srcRect := src.Bounds()
dstRect := image.Rectangle{
Min: image.Point{0, 0},
Max: dstSize,
}
dst := image.NewRGBA(dstRect)
draw.CatmullRom.Scale(dst, dstRect, src, srcRect, draw.Over, nil)
return dst
}
Notable points:
image.RGBA, not image.Image because Go's best practice is to accept interface as arguments to functions, but return concrete types
draw is, at its core, a compositing engine which allows compositing (drawing) bitmap images with a specific operation. draw.Over is an operation that draws one image over another, optionally using a mask.
Scaler interface which takes destination image, a rectangle within destination image, source image, a rectangle within source images and paints source rectangle into destination rectangle. Resizing happens when destination rectangle has different dimension that source rectangle
draw.CatmullRom is a global variable for an instance implementing catmull-rom algorithm. Other algorithms are available as draw.NearestNeighbor, draw.ApproxBiLinear and draw.BiLinear package global variables
Scale method of one of the global scalers to paint source image into a destination, with resizing being a side effect of using a scaler.