poster.go 5.0 KB

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