poster.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package image
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gographics/gmagick"
  6. "icloudapp.cn/tools/util"
  7. "strings"
  8. )
  9. type Poster struct {
  10. ctx context.Context
  11. }
  12. func NewPoster(ctx context.Context) *Poster {
  13. return &Poster{ctx: ctx}
  14. }
  15. func (p *Poster) Text(item Items) (*gmagick.MagickWand, Items, error) {
  16. var updated Items
  17. item.Value = strings.Replace(item.Value, " ", " ", -1)
  18. txt := NewText(item.Font, float64(item.Size), "")
  19. txt.SetText(item.Value)
  20. txt.SetColor(item.Color)
  21. txt.SetBackground(item.Background)
  22. txt.SetSize(item.Width, item.Height)
  23. txt.SetPoint(float64(item.X), float64(item.Y))
  24. txt.SetAlign(*item.Align)
  25. txt.SetBius(*item.Bius)
  26. txt.SetAngle(int64(item.Angle))
  27. if *item.Wrap {
  28. txt.SetWrap(*item.Wrap)
  29. }
  30. mw, err := txt.Create()
  31. updated = item
  32. updated.X = int(txt.Coordinate.X)
  33. updated.Y = int(txt.Coordinate.Y)
  34. updated.Width = txt.ViewSize.Width
  35. updated.Height = txt.ViewSize.Height
  36. return mw, updated, err
  37. }
  38. func (p *Poster) QRCode(item Items) (*gmagick.MagickWand, Items, error) {
  39. var updated Items
  40. cachePath := fmt.Sprintf("%s/cache", util.Getwd())
  41. qr := NewQR()
  42. qr.SetValue(item.Value)
  43. qr.SetSize(item.Width, item.Height)
  44. qr.SetBackgroundColor(item.Background)
  45. qr.SetFrontColor(item.Color)
  46. qr.SetCache(cachePath)
  47. mw, err := qr.Create()
  48. updated = item
  49. return mw, updated, err
  50. }
  51. type PosterMW struct {
  52. Index int
  53. Image *gmagick.MagickWand
  54. Item Items
  55. Err error
  56. }
  57. func (p *Poster) Create(json PosterJson) (*gmagick.MagickWand, error) {
  58. cachePath := fmt.Sprintf("%s/cache", util.Getwd())
  59. image := NewImage(util.Getwd())
  60. image.SetCache(cachePath)
  61. if json.Use == "bgUrl" {
  62. json.Background = ""
  63. }
  64. if json.Use == "bgc" {
  65. json.BackgroundUrl = ""
  66. }
  67. poster, err := image.Create(json.BackgroundUrl, json.Background, *NewSize(json.Width, json.Height))
  68. if err != nil {
  69. return nil, err
  70. }
  71. itemLen := len(json.Items)
  72. destChan := make(chan *PosterMW, itemLen)
  73. defer close(destChan)
  74. for index, item := range json.Items {
  75. go func(item Items, index int) {
  76. var drawErr error
  77. var drawMW *gmagick.MagickWand
  78. if item.Type == "text" {
  79. drawMW, item, drawErr = p.Text(item)
  80. }
  81. if item.Type == "image" {
  82. drawMW, drawErr = image.Resize(item.Value, item.Width, item.Height, item.Angle)
  83. }
  84. if item.Type == "qrcode" {
  85. drawMW, item, drawErr = p.QRCode(item)
  86. }
  87. destChan <- &PosterMW{Index: index, Image: drawMW, Item: item, Err: drawErr}
  88. }(item, index)
  89. }
  90. var response = make([]*PosterMW, itemLen)
  91. for i := 0; i < len(json.Items); i++ {
  92. res := <-destChan
  93. response[res.Index] = res
  94. }
  95. for i := 0; i < len(response); i++ {
  96. draw := response[i].Image
  97. errDraw := response[i].Err
  98. if errDraw != nil {
  99. continue
  100. }
  101. if response[i].Item.Type == "text" {
  102. fmt.Println(fmt.Sprintf("response[i].Item : %+v", response[i].Item))
  103. }
  104. if errComposite := poster.CompositeImage(draw, gmagick.COMPOSITE_OP_OVER, response[i].Item.X, response[i].Item.Y); errComposite != nil {
  105. fmt.Println("poster.CompositeImage text err : ", errComposite.Error())
  106. continue
  107. }
  108. }
  109. return poster, nil
  110. }