Skip to content

Instantly share code, notes, and snippets.

@tommy351
Created November 7, 2016 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommy351/ba925d01c41afa676dbd8f5be112ff98 to your computer and use it in GitHub Desktop.
Save tommy351/ba925d01c41afa676dbd8f5be112ff98 to your computer and use it in GitHub Desktop.
src, _ := loadBaseImageForPost(post)
img := image.NewRGBA(src.Bounds())
gc := draw2dimg.NewGraphicContext(img)
gc.DrawImage(src)
// Draw title
gc.SetFillColor(color.RGBA{0xff, 0xff, 0xff, 0xff})
gc.SetFontSize(26)
gc.FillStringAt(post.Title, 140, 81.5)
// Draw content
gc.SetFillColor(color.RGBA{0x32, 0x32, 0x32, 0xff})
gc.SetFontSize(26)
gc.FillStringAt(post.Content, 60, 190)
// Draw forum
forum := post.ForumName
_, _, right, _ := gc.GetStringBounds(forum)
gc.SetFillColor(color.RGBA{0xff, 0xff, 0xff, 0xff})
gc.SetFontSize(26)
gc.FillStringAt(forum, 1140-right, 81.5)
// Draw time
timeLocation, _ := time.LoadLocation("Asia/Taipei")
time := post.CreatedAt.In(timeLocation).Format("2006/1/2 15:04")
_, _, right, bottom := gc.GetStringBounds(time)
gc.SetFillColor(color.RGBA{0x79, 0x79, 0x79, 0xff})
gc.SetFontSize(26)
gc.FillStringAt(time, 1140-right, 510-bottom)
ctx.Response.Header.Set("Content-Type", "image/png")
png.Encode(ctx, img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment