-
Notifications
You must be signed in to change notification settings - Fork 49
/
imagemeta.go
56 lines (49 loc) · 1.42 KB
/
imagemeta.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package imagemeta
import (
"bytes"
"fmt"
"image/color"
"image/png"
"io"
"path/filepath"
"github.com/golang-cafe/job-board/internal/job"
"github.com/fogleman/gg"
"github.com/pkg/errors"
)
const (
backgroundImageFilename = "static/assets/img/meta-bg.jpg"
outputFilename = "output.jpg"
)
func GenerateImageForJob(jobPost job.JobPost) (io.ReadWriter, error) {
dc := gg.NewContext(1200, 628)
backgroundImage, err := gg.LoadImage(backgroundImageFilename)
w := bytes.NewBuffer([]byte{})
if err != nil {
return w, errors.Wrap(err, "load background image")
}
// draw background image
dc.DrawImage(backgroundImage, 0, 0)
// draw job post title and description
title := fmt.Sprintf("%s with %s\n\n %s\n\n %s", jobPost.JobTitle, jobPost.Company, jobPost.Location, jobPost.SalaryRange)
mainTextColor := color.RGBA{
R: uint8(0),
G: uint8(0),
B: uint8(144),
A: uint8(255),
}
fontPath := filepath.Join("static", "assets", "fonts", "verdana", "verdana.ttf")
if err := dc.LoadFontFace(fontPath, 60); err != nil {
return w, errors.Wrap(err, "load Courier_Prime for job link")
}
textRightMargin := 80.0
textTopMargin := 90.0
x := textRightMargin
y := textTopMargin
maxWidth := float64(dc.Width()) - textRightMargin - textRightMargin
dc.SetColor(mainTextColor)
dc.DrawStringWrapped(title, x, y, 0, 0, maxWidth, 1.5, gg.AlignLeft)
if err := png.Encode(w, dc.Image()); err != nil {
return w, err
}
return w, nil
}