Skip to content

Commit

Permalink
add article poster text feature
Browse files Browse the repository at this point in the history
  • Loading branch information
eddycjy committed Jul 5, 2018
1 parent 45174b7 commit ee7073d
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.DS_Store

runtime/*
!runtime/qrcode/bg.jpg
!runtime/qrcode/bg.jpg
!runtime/fonts
1 change: 1 addition & 0 deletions conf/app.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ImageAllowExts = .jpg,.jpeg,.png

ExportSavePath = export/
QrCodeSavePath = qrcode/
FontSavePath = fonts/

LogSavePath = logs/
LogSaveName = log
Expand Down
1 change: 1 addition & 0 deletions pkg/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type App struct {

ExportSavePath string
QrCodeSavePath string
FontSavePath string

LogSavePath string
LogSaveName string
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,4 @@ func GenerateArticlePoster(c *gin.Context) {
"poster_url": qrcode.GetQrCodeFullUrl(posterName),
"poster_save_url": filePath + posterName,
})
}
}
Binary file added runtime/fonts/msyhbd.ttc
Binary file not shown.
77 changes: 76 additions & 1 deletion service/article_service/article_poster.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (

"github.com/EDDYCJY/go-gin-example/pkg/file"
"github.com/EDDYCJY/go-gin-example/pkg/qrcode"
"github.com/EDDYCJY/go-gin-example/pkg/setting"
"github.com/golang/freetype"
"io/ioutil"
)

type ArticlePoster struct {
Expand Down Expand Up @@ -74,6 +77,61 @@ func NewArticlePosterBg(name string, ap *ArticlePoster, rect *Rect, pt *Pt) *Art
}
}

type DrawText struct {
JPG draw.Image
Merged *os.File

Title string
X0 int
Y0 int
Size0 float64

SubTitle string
X1 int
Y1 int
Size1 float64
}

func (a *ArticlePosterBg) DrawPoster(d *DrawText, fontName string) error {
fontSource := setting.AppSetting.RuntimeRootPath + setting.AppSetting.FontSavePath + fontName
fontSourceBytes, err := ioutil.ReadFile(fontSource)
if err != nil {
return err
}

trueTypeFont, err := freetype.ParseFont(fontSourceBytes)
if err != nil {
return err
}

fc := freetype.NewContext()
fc.SetDPI(72)
fc.SetFont(trueTypeFont)
fc.SetFontSize(d.Size0)
fc.SetClip(d.JPG.Bounds())
fc.SetDst(d.JPG)
fc.SetSrc(image.Black)

pt := freetype.Pt(d.X0, d.Y0)
_, err = fc.DrawString(d.Title, pt)
if err != nil {
return err
}

fc.SetFontSize(d.Size1)
_, err = fc.DrawString(d.SubTitle, freetype.Pt(d.X1, d.Y1))
if err != nil {
return err
}

err = jpeg.Encode(d.Merged, d.JPG, nil)
if err != nil {
return err
}

return nil
}

func (a *ArticlePosterBg) Generate() (string, string, error) {
fullPath := qrcode.GetQrCodeFullPath()
fileName, path, err := a.Qr.Encode(fullPath)
Expand Down Expand Up @@ -114,7 +172,24 @@ func (a *ArticlePosterBg) Generate() (string, string, error) {
draw.Draw(jpg, jpg.Bounds(), bgImage, bgImage.Bounds().Min, draw.Over)
draw.Draw(jpg, jpg.Bounds(), qrImage, qrImage.Bounds().Min.Sub(image.Pt(a.Pt.X, a.Pt.Y)), draw.Over)

jpeg.Encode(mergedF, jpg, nil)
err = a.DrawPoster(&DrawText{
JPG: jpg,
Merged: mergedF,

Title: "Golang Gin 系列文章",
X0: 80,
Y0: 160,
Size0: 42,

SubTitle: "---煎鱼",
X1: 320,
Y1: 220,
Size1: 36,
}, "msyhbd.ttc")

if err != nil {
return "", "", err
}
}

return fileName, path, nil
Expand Down

0 comments on commit ee7073d

Please sign in to comment.