back.txt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. func (p *Poster) Create2(json PosterJson) (string, error) {
  2. cachePath := fmt.Sprintf("%s/cache", util.Getwd())
  3. image := NewImage(util.Getwd())
  4. image.SetCache(cachePath)
  5. if json.Use == "bgUrl" {
  6. json.Background = ""
  7. }
  8. if json.Use == "bgc" {
  9. json.BackgroundUrl = ""
  10. }
  11. poster, err := image.Create(json.BackgroundUrl, json.Background, *NewSize(json.Width, json.Height))
  12. if err != nil {
  13. return "", err
  14. }
  15. defer poster.Destroy()
  16. itemLen := len(json.Items)
  17. destChan := make(chan *PosterMW, itemLen)
  18. defer close(destChan)
  19. for index, item := range json.Items {
  20. go func(item Items, index int) {
  21. var drawErr error
  22. var drawMW *gmagick.MagickWand
  23. var updated Items
  24. if item.Type == "text" {
  25. drawMW, updated, drawErr = p.Text(item)
  26. }
  27. if item.Type == "image" {
  28. drawMW, drawErr = image.Resize(item.Value, item.Width, item.Height, item.Angle)
  29. }
  30. if item.Type == "qrcode" {
  31. drawMW, updated, drawErr = p.QRCode(item)
  32. }
  33. destChan <- &PosterMW{Index: index, Image: drawMW, Item: updated, Err: drawErr}
  34. }(item, index)
  35. }
  36. var response = make([]*PosterMW, itemLen)
  37. for i := 0; i < len(json.Items); i++ {
  38. res := <-destChan
  39. response[res.Index] = res
  40. }
  41. for i := 0; i < len(response); i++ {
  42. draw := response[i].Image
  43. errDraw := response[i].Err
  44. if errDraw != nil {
  45. continue
  46. }
  47. if errComposite := poster.CompositeImage(draw, gmagick.COMPOSITE_OP_OVER, response[i].Item.X, response[i].Item.Y); errComposite != nil {
  48. fmt.Println("poster.CompositeImage text err : ", errComposite.Error())
  49. continue
  50. }
  51. }
  52. destFile := image.CacheFile("preview-poster", poster.GetImageFormat())
  53. err = poster.WriteImage(destFile)
  54. if err != nil {
  55. return "", err
  56. }
  57. return destFile, nil
  58. }
  59. func (p *Poster) Create1(json PosterJson) (string, error) {
  60. cachePath := fmt.Sprintf("%s/cache", util.Getwd())
  61. image := NewImage(util.Getwd())
  62. image.SetCache(cachePath)
  63. if json.Use == "bgUrl" {
  64. json.Background = ""
  65. }
  66. if json.Use == "bgc" {
  67. json.BackgroundUrl = ""
  68. }
  69. poster, err := image.Create(json.BackgroundUrl, json.Background, *NewSize(json.Width, json.Height))
  70. if err != nil {
  71. return "", err
  72. }
  73. defer poster.Destroy()
  74. for _, item := range json.Items {
  75. if item.Type == "text" {
  76. drawText, _, errTxt := p.Text(item)
  77. if errTxt != nil {
  78. fmt.Println("drawText err: ", errTxt.Error())
  79. continue
  80. }
  81. if errComposite := poster.CompositeImage(drawText, gmagick.COMPOSITE_OP_OVER, item.X, item.Y); errComposite != nil {
  82. fmt.Println("poster.CompositeImage text err : ", errComposite.Error())
  83. continue
  84. }
  85. }
  86. if item.Type == "image" {
  87. imageMW, errImage := image.Resize(item.Value, item.Width, item.Height, item.Angle)
  88. if errImage != nil {
  89. continue
  90. }
  91. if errComposite := poster.CompositeImage(imageMW, gmagick.COMPOSITE_OP_OVER, item.X, item.Y); errComposite != nil {
  92. fmt.Println("poster.CompositeImage image err : ", errComposite.Error())
  93. continue
  94. }
  95. }
  96. if item.Type == "qrcode" {
  97. qrWM, _, errQr := p.QRCode(item)
  98. if errQr != nil {
  99. fmt.Println("Qr err", errQr.Error())
  100. continue
  101. }
  102. if errComposite := poster.CompositeImage(qrWM, gmagick.COMPOSITE_OP_OVER, item.X, item.Y); errComposite != nil {
  103. fmt.Println("poster.CompositeImage qr err : ", errComposite.Error())
  104. continue
  105. }
  106. }
  107. }
  108. //util.RandomStr(10);
  109. destFile := image.CacheFile("preview-poster", poster.GetImageFormat())
  110. err = poster.WriteImage(destFile)
  111. if err != nil {
  112. return "", err
  113. }
  114. return destFile, nil
  115. }