poster.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "github.com/gographics/gmagick"
  7. setting "icloudapp.cn/tools/config"
  8. "icloudapp.cn/tools/entity"
  9. "icloudapp.cn/tools/model"
  10. "icloudapp.cn/tools/util/image"
  11. "os"
  12. )
  13. func Preview(ctx *gin.Context) {
  14. file := ctx.Query("file")
  15. if file == "" {
  16. ResponseError(ctx, entity.CodePageNotFound)
  17. return
  18. }
  19. path, _ := os.Getwd()
  20. gmagick.Initialize()
  21. defer gmagick.Terminate()
  22. thumb := image.NewImage(setting.Conf.CacheDir)
  23. file, err := thumb.Thumb(fmt.Sprintf("%s/%s", path, file), image.NewSize(300, 300))
  24. if err != nil {
  25. ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
  26. return
  27. }
  28. stream, err := os.ReadFile(file)
  29. if err != nil {
  30. ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
  31. return
  32. }
  33. ctx.Writer.WriteString(string(stream))
  34. }
  35. func Image(ctx *gin.Context) {
  36. jsonByte, err := os.ReadFile("data.json")
  37. if err != nil {
  38. ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
  39. return
  40. }
  41. var posterJson image.PosterJson
  42. err = json.Unmarshal(jsonByte, &posterJson)
  43. if err != nil {
  44. ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
  45. return
  46. }
  47. poster := image.NewPoster(ctx)
  48. file, err := poster.Create(posterJson)
  49. if err != nil {
  50. ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("poster.Create err : %s", err.Error()), []interface{}{})
  51. return
  52. }
  53. if err = file.SetImageFormat("PNG"); err != nil {
  54. ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetImageFormat err : %s", err.Error()), []interface{}{})
  55. return
  56. }
  57. defer file.Destroy()
  58. if posterJson.Quality == 0 {
  59. posterJson.Quality = 50
  60. }
  61. if err = file.SetCompressionQuality(uint(posterJson.Quality)); err != nil {
  62. ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetCompressionQuality err: %s", err.Error()), []interface{}{})
  63. return
  64. }
  65. //不写入文件,直接返回给浏览器
  66. _, err = ctx.Writer.Write(file.WriteImageBlob())
  67. if err != nil {
  68. ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("ctx.Writer.Write err: %s", err.Error()), []interface{}{})
  69. return
  70. }
  71. /*
  72. stream, err := os.ReadFile(file)
  73. if err != nil {
  74. fmt.Println("read error")
  75. ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
  76. return
  77. }
  78. ctx.Writer.WriteString(string(stream))
  79. */
  80. }
  81. func Cut(ctx *gin.Context) {
  82. file := ctx.Query("file")
  83. if file == "" {
  84. ResponseError(ctx, entity.CodePageNotFound)
  85. return
  86. }
  87. path, _ := os.Getwd()
  88. gmagick.Initialize()
  89. defer gmagick.Terminate()
  90. thumb := image.NewImage(setting.Conf.CacheDir)
  91. file, err := thumb.Cut(fmt.Sprintf("%s/%s", path, file), image.NewSize(400, 400), "center")
  92. if err != nil {
  93. ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
  94. return
  95. }
  96. stream, err := os.ReadFile(file)
  97. if err != nil {
  98. ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
  99. return
  100. }
  101. ctx.Writer.WriteString(string(stream))
  102. }
  103. func SavePoster(ctx *gin.Context) {
  104. }
  105. func PosterInfo(ctx *gin.Context) {
  106. var request entity.PosterInfoRequest
  107. if err := ctx.ShouldBind(&request); err != nil {
  108. ResponseError(ctx, entity.CodeInvalidParam)
  109. return
  110. }
  111. sUserPoster := model.NewUserPoster(ctx)
  112. info, err := sUserPoster.Info(request.PosterID)
  113. if err != nil {
  114. ResponseNormal(ctx, entity.CodeServerBusy, info.Msg, nil)
  115. return
  116. }
  117. var api entity.UserPosterInfoForApi
  118. api.Code = info.Code
  119. api.Msg = info.Msg
  120. api.Body.Poster = info.Body
  121. ResponseNormal(ctx, api.Code, api.Msg, api.Body)
  122. }
  123. func DrawImage(ctx *gin.Context) {
  124. var request entity.PosterInfoRequest
  125. if err := ctx.ShouldBind(&request); err != nil {
  126. ResponseError(ctx, entity.CodeInvalidParam)
  127. return
  128. }
  129. sUserPoster := model.NewUserPoster(ctx)
  130. info, err := sUserPoster.Info(request.PosterID)
  131. if err != nil {
  132. ResponseNormal(ctx, entity.CodeServerBusy, info.Msg, nil)
  133. return
  134. }
  135. var posterJson image.PosterJson
  136. err = json.Unmarshal([]byte(info.Body.PosterJson), &posterJson)
  137. if err != nil {
  138. ResponseNormal(ctx, entity.CodeSystemError, err.Error(), []interface{}{})
  139. return
  140. }
  141. poster := image.NewPoster(ctx)
  142. file, err := poster.Create(posterJson)
  143. if err != nil {
  144. ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("poster.Create err : %s", err.Error()), []interface{}{})
  145. return
  146. }
  147. if err = file.SetImageFormat("PNG"); err != nil {
  148. ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetImageFormat err : %s", err.Error()), []interface{}{})
  149. return
  150. }
  151. defer file.Destroy()
  152. if posterJson.Quality == 0 {
  153. posterJson.Quality = 50
  154. }
  155. if err = file.SetCompressionQuality(uint(posterJson.Quality)); err != nil {
  156. ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("file.SetCompressionQuality err: %s", err.Error()), []interface{}{})
  157. return
  158. }
  159. //不写入文件,直接返回给浏览器
  160. _, err = ctx.Writer.Write(file.WriteImageBlob())
  161. if err != nil {
  162. ResponseNormal(ctx, entity.CodeSystemError, fmt.Sprintf("ctx.Writer.Write err: %s", err.Error()), []interface{}{})
  163. return
  164. }
  165. }