Inserting, drawing, and text
Inserting images
Insert draws the first frame of another image over every frame of the image at a position. This is how you add a watermark:
logo, err := image.From(r).Open("resources/images/logo.png")
if err != nil {
return web.Fail(err)
}
img.Insert(logo, image.BottomRight.Offset(20, 20))InsertWithOpacity inserts the image at an opacity from 0, which leaves the image unchanged, to 100:
img.InsertWithOpacity(logo, image.BottomRight.Offset(20, 20), 40)Drawing shapes
Draw draws one or more shapes on every frame, in order. Shapes are plain structs, and their colors are strings that image.ParseColor reads. Translucent colors blend over the image:
img.Draw(
image.Rectangle{X: 40, Y: 40, Width: 400, Height: 120, Background: "#0b1118cc"},
image.Circle{X: 600, Y: 100, Diameter: 80, Background: "#00add8", Border: image.Border{Color: "fff", Width: 4}},
)An empty background or border color draws nothing, so a shape with only a Border is an outline. A Border with a color but a Width of 0 is 1 pixel wide.
Rectangles
A Rectangle has its top-left corner at X and Y. Its border is drawn inside it, so the rectangle covers exactly Width by Height pixels:
image.Rectangle{X: 10, Y: 10, Width: 200, Height: 50, Background: "#f005", Border: image.Border{Color: "f00", Width: 2}}Ellipses and circles
An Ellipse and a Circle are centered on the pixel at X and Y. Their borders are centered on their outlines:
image.Ellipse{X: 320, Y: 240, Width: 200, Height: 120, Background: "steelblue"}
image.Circle{X: 320, Y: 240, Diameter: 60, Border: image.Border{Color: "white", Width: 3}}Lines
A Line runs through the centers of two pixels. Width defaults to 1 and Color to black:
image.Line{From: image.Pt(0, 0), To: image.Pt(640, 480), Color: "#ff2d20", Width: 4}Polygons and curves
A Polygon is a closed shape through three or more points. A polygon that crosses itself fills by the nonzero rule, so a pentagram's center is filled:
image.Polygon{
Points: []image.Point{image.Pt(320, 40), image.Pt(560, 420), image.Pt(80, 420)},
Background: "#22c55e",
}A Bezier is a curve from its first point to its last, pulled toward the points between them. Three points draw a quadratic curve and four draw a cubic one. Its border draws the curve, and its background fills the area between the curve and the line that joins its ends:
image.Bezier{
Points: []image.Point{image.Pt(40, 400), image.Pt(320, 40), image.Pt(600, 400)},
Border: image.Border{Color: "#00add8", Width: 3},
}Drawing pixels
DrawPixel blends a color over a single pixel on every frame. A pixel outside the image is skipped:
img.DrawPixel(10, 10, "#ff2d20")Writing text
Text writes text on every frame. The image.Font struct describes how it looks, and its zero value writes 12 pixel black text in Go Regular, centered on the point:
img.Text("Laravel Go Image", img.Width()/2, img.Height()/2, image.Font{
Size: 64,
Color: "fff",
})Fonts
Font.File is the path to a TrueType or OpenType font. When it's empty, the package uses Go Regular, which it embeds:
font := image.Font{
File: "resources/fonts/Inter-Bold.ttf",
Size: 48,
Color: "#0b1118",
LineHeight: 1.4,
}Size is in pixels, up to 4096. LineHeight is the distance between baselines as a multiple of the size, and defaults to 1.25.
Alignment and wrapping
Font.Align is the point of the text block that lines up with the position you pass to Text. It also aligns the lines within the block. To place text in a corner, align it to that corner:
img.Text("© Laravel", img.Width()-10, img.Height()-10, image.Font{
Size: 14,
Color: "fff",
Align: image.BottomRight,
})Newlines, including \r\n, start new lines. Set Wrap to a width in pixels to also break long lines between words:
img.Text(caption, 40, 40, image.Font{Size: 24, Align: image.TopLeft, Wrap: 600})