ordonnance.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package util
  2. import (
  3. "bytes"
  4. "fmt"
  5. "image"
  6. "image/color"
  7. "image/draw"
  8. "image/jpeg"
  9. "image/png"
  10. "io/ioutil"
  11. "ordonnance/server/model"
  12. "os"
  13. "time"
  14. "github.com/golang/freetype"
  15. "github.com/golang/freetype/truetype"
  16. qrcode "github.com/skip2/go-qrcode"
  17. )
  18. func MakeImg(ordonnance model.Ordonnance) string {
  19. m := image.NewRGBA(image.Rect(0, 0, 200, 200))
  20. white := color.RGBA{255, 255, 255, 0}
  21. draw.Draw(m, m.Bounds(), &image.Uniform{white}, image.ZP, draw.Src)
  22. var font *truetype.Font
  23. fontBytes, _ := ioutil.ReadFile("./line.ttf")
  24. font, _ = freetype.ParseFont(fontBytes)
  25. f := freetype.NewContext()
  26. f.SetFont(font)
  27. f.SetClip(m.Bounds())
  28. f.SetDst(m)
  29. f.SetSrc(image.NewUniform(color.RGBA{0, 0, 0, 255}))
  30. f.SetFontSize(18)
  31. url := fmt.Sprintf("https://sm.hqedust.com/ordonnance/#/ordonnance/%d", ordonnance.OrdonnanceId)
  32. qrcodeData, _ := qrcode.Encode(url, qrcode.Medium, 100)
  33. qrImg, _ := png.Decode(bytes.NewReader(qrcodeData))
  34. offset := image.Pt(10, 10)
  35. draw.Draw(m, qrImg.Bounds().Add(offset), qrImg, image.ZP, draw.Over)
  36. f.SetFontSize(12)
  37. w, h := 120, 30
  38. f.DrawString("同春煎药", freetype.Pt(w, h))
  39. w, h = 120, 50
  40. f.DrawString("冷藏保管", freetype.Pt(w, h))
  41. w, h = 120, 80
  42. f.DrawString("时间:", freetype.Pt(w, h))
  43. w, h = 120, 100
  44. f.DrawString(ordonnance.PrintDate, freetype.Pt(w, h))
  45. w, h = 20, 130
  46. f.DrawString("To:", freetype.Pt(w, h))
  47. w, h = 20, 150
  48. f.DrawString("姓名: "+ordonnance.Nickname, freetype.Pt(w, h))
  49. w, h = 120, 150
  50. f.DrawString(fmt.Sprintf("数量: %d 贴", ordonnance.Counter), freetype.Pt(w, h))
  51. w, h = 20, 170
  52. f.DrawString("地址: "+ordonnance.Organ, freetype.Pt(w, h))
  53. filename := time.Now().Format("tempdir/20060102150405.jpg")
  54. myfile, _ := os.Create(filename)
  55. jpeg.Encode(myfile, m, &jpeg.Options{Quality: 100})
  56. return filename
  57. }