123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package util
- import (
- "bytes"
- "fmt"
- "image"
- "image/color"
- "image/draw"
- "image/jpeg"
- "image/png"
- "io/ioutil"
- "ordonnance/server/model"
- "os"
- "time"
- "github.com/golang/freetype"
- "github.com/golang/freetype/truetype"
- qrcode "github.com/skip2/go-qrcode"
- )
- func MakeImg(ordonnance model.Ordonnance) string {
- m := image.NewRGBA(image.Rect(0, 0, 200, 200))
- white := color.RGBA{255, 255, 255, 0}
- draw.Draw(m, m.Bounds(), &image.Uniform{white}, image.ZP, draw.Src)
- var font *truetype.Font
- fontBytes, _ := ioutil.ReadFile("./line.ttf")
- font, _ = freetype.ParseFont(fontBytes)
- f := freetype.NewContext()
- f.SetFont(font)
- f.SetClip(m.Bounds())
- f.SetDst(m)
- f.SetSrc(image.NewUniform(color.RGBA{0, 0, 0, 255}))
- f.SetFontSize(18)
- url := fmt.Sprintf("https://sm.hqedust.com/ordonnance/#/ordonnance/%d", ordonnance.OrdonnanceId)
- qrcodeData, _ := qrcode.Encode(url, qrcode.Medium, 100)
- qrImg, _ := png.Decode(bytes.NewReader(qrcodeData))
- offset := image.Pt(10, 10)
- draw.Draw(m, qrImg.Bounds().Add(offset), qrImg, image.ZP, draw.Over)
- f.SetFontSize(12)
- w, h := 120, 30
- f.DrawString("同春煎药", freetype.Pt(w, h))
- w, h = 120, 50
- f.DrawString("冷藏保管", freetype.Pt(w, h))
- w, h = 120, 80
- f.DrawString("时间:", freetype.Pt(w, h))
- w, h = 120, 100
- f.DrawString(ordonnance.PrintDate, freetype.Pt(w, h))
- w, h = 20, 130
- f.DrawString("To:", freetype.Pt(w, h))
- w, h = 20, 150
- f.DrawString("姓名: "+ordonnance.Nickname, freetype.Pt(w, h))
- w, h = 120, 150
- f.DrawString(fmt.Sprintf("数量: %d 贴", ordonnance.Counter), freetype.Pt(w, h))
- w, h = 20, 170
- f.DrawString("地址: "+ordonnance.Organ, freetype.Pt(w, h))
- filename := time.Now().Format("tempdir/20060102150405.jpg")
- myfile, _ := os.Create(filename)
- jpeg.Encode(myfile, m, &jpeg.Options{Quality: 100})
- return filename
- }
|