poster.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package poster
  2. import (
  3. "image"
  4. "image/color"
  5. "image/draw"
  6. "image/jpeg"
  7. "io/ioutil"
  8. "os"
  9. "github.com/golang/freetype"
  10. "github.com/golang/freetype/truetype"
  11. "github.com/skip2/go-qrcode"
  12. )
  13. //新PNG载体
  14. type Rect struct {
  15. X0 int
  16. X1 int
  17. Y0 int
  18. Y1 int
  19. }
  20. // Pt 坐标
  21. type Pt struct {
  22. X int
  23. Y int
  24. }
  25. // DImage 图片切片
  26. type DImage struct {
  27. PNG draw.Image //合并到的PNG切片,可用image.NewrRGBA设置
  28. X int //横坐标
  29. Y int //纵坐标
  30. }
  31. // DText 文字切片
  32. type DText struct {
  33. PNG draw.Image //合并到的PNG切片,可用image.NewrRGBA设置
  34. Title string //文字
  35. X int //横坐标
  36. Y int //纵坐标
  37. Size float64
  38. R uint8
  39. G uint8
  40. B uint8
  41. A uint8
  42. }
  43. // NewMerged 新建文件载体
  44. func NewMerged(path string) (*os.File, error) {
  45. f, err := os.Create(path)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return f, nil
  50. }
  51. // NewPNG 新建图片载体
  52. func NewPNG(X0 int, Y0 int, X1 int, Y1 int) *image.RGBA {
  53. return image.NewRGBA(image.Rect(X0, Y0, X1, Y1))
  54. }
  55. // MergeImage 合并图片到载体
  56. func MergeImage(PNG draw.Image, image image.Image, imageBound image.Point) {
  57. draw.Draw(PNG, PNG.Bounds(), image, imageBound, draw.Over)
  58. }
  59. // LoadTextType 读取字体类型
  60. func LoadTextType(path string) (*truetype.Font, error) {
  61. fbyte, err := ioutil.ReadFile(path)
  62. if err != nil {
  63. return nil, err
  64. }
  65. trueTypeFont, err := freetype.ParseFont(fbyte)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return trueTypeFont, nil
  70. }
  71. // NewDrawText 创建新字体切片
  72. func NewDrawText(png draw.Image) *DText {
  73. return &DText{
  74. PNG: png,
  75. Size: 18,
  76. X: 0,
  77. Y: 0,
  78. R: 0,
  79. G: 0,
  80. B: 0,
  81. A: 255,
  82. }
  83. }
  84. // SetColor 设置字体颜色
  85. func (dtext *DText) SetColor(R uint8, G uint8, B uint8) {
  86. dtext.R = R
  87. dtext.G = G
  88. dtext.B = B
  89. }
  90. // MergeText 合并字体到载体
  91. func (dtext *DText) MergeText(title string, tf *truetype.Font, x int, y int, rect image.Rectangle) error {
  92. fc := freetype.NewContext()
  93. //设置屏幕每英寸的分辨率
  94. fc.SetDPI(72)
  95. //设置用于绘制文本的字体
  96. fc.SetFont(tf)
  97. //以磅为单位设置字体大小
  98. fc.SetFontSize(dtext.Size)
  99. //设置剪裁矩形以进行绘制
  100. fc.SetClip(rect)
  101. //设置目标图像
  102. fc.SetDst(dtext.PNG)
  103. //设置绘制操作的源图像,通常为 image.Uniform
  104. fc.SetSrc(image.NewUniform(color.RGBA{dtext.R, dtext.G, dtext.B, dtext.A}))
  105. pt := freetype.Pt(x, y)
  106. _, err := fc.DrawString(title, pt)
  107. if err != nil {
  108. return err
  109. }
  110. return nil
  111. }
  112. // Merge 合并到图片
  113. func Merge(png draw.Image, merged *os.File) error {
  114. err := jpeg.Encode(merged, png, nil)
  115. if err != nil {
  116. return err
  117. }
  118. return nil
  119. }
  120. // GetQRImage 获取二维码图像
  121. func GetQRImage(url string, level qrcode.RecoveryLevel, size int) (image.Image, error) {
  122. newQr, err := qrcode.New(url, level)
  123. if err != nil {
  124. return nil, err
  125. }
  126. qrImage := newQr.Image(size)
  127. return qrImage, nil
  128. }