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(90, 10) draw.Draw(m, qrImg.Bounds().Add(offset), qrImg, image.ZP, draw.Over) f.SetFontSize(12) w, h := 20, 40 f.DrawString("同春煎药", freetype.Pt(w, h)) w, h = 20, 70 f.DrawString("冷藏保管", freetype.Pt(w, h)) w, h = 20, 120 f.DrawString("To:", freetype.Pt(w, h)) f.SetFontSize(10) w, h = 110, 120 f.DrawString("打包人: "+ordonnance.Packer, freetype.Pt(w, h)) f.SetFontSize(12) w, h = 20, 140 f.DrawString("姓  名: "+ordonnance.Nickname, freetype.Pt(w, h)) w, h = 120, 140 f.DrawString(fmt.Sprintf("数量: %d 贴", ordonnance.Counter), freetype.Pt(w, h)) w, h = 20, 160 f.DrawString("地  址: "+ordonnance.Organ, freetype.Pt(w, h)) w, h = 20, 180 f.DrawString("煎药时间: "+ordonnance.PrintDate[0:16], 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 }