ordonnance.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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(90, 10)
  35. draw.Draw(m, qrImg.Bounds().Add(offset), qrImg, image.ZP, draw.Over)
  36. f.SetFontSize(12)
  37. w, h := 20, 40
  38. f.DrawString("同春煎药", freetype.Pt(w, h))
  39. w, h = 20, 70
  40. f.DrawString("冷藏保管", freetype.Pt(w, h))
  41. w, h = 20, 120
  42. f.DrawString("To:", freetype.Pt(w, h))
  43. f.SetFontSize(10)
  44. w, h = 110, 120
  45. f.DrawString("打包人: "+ordonnance.Packer, freetype.Pt(w, h))
  46. f.SetFontSize(12)
  47. w, h = 20, 140
  48. f.DrawString("姓  名: "+ordonnance.Nickname, freetype.Pt(w, h))
  49. w, h = 120, 140
  50. f.DrawString(fmt.Sprintf("数量: %d 贴", ordonnance.Counter), freetype.Pt(w, h))
  51. w, h = 20, 160
  52. f.DrawString("地  址: "+ordonnance.Organ, freetype.Pt(w, h))
  53. w, h = 20, 180
  54. f.DrawString("煎药时间: "+ordonnance.PrintDate[0:16], freetype.Pt(w, h))
  55. filename := time.Now().Format("tempdir/20060102150405.jpg")
  56. myfile, _ := os.Create(filename)
  57. jpeg.Encode(myfile, m, &jpeg.Options{Quality: 100})
  58. return filename
  59. }