cropper.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package views
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/qor5/admin/presets"
  6. "time"
  7. "github.com/qor5/admin/media"
  8. "github.com/qor5/admin/media/media_library"
  9. "github.com/qor5/ui/cropper"
  10. . "github.com/qor5/ui/vuetify"
  11. "github.com/qor5/web"
  12. "github.com/qor5/x/i18n"
  13. h "github.com/theplant/htmlgo"
  14. "gorm.io/gorm"
  15. )
  16. func getParams(ctx *web.EventContext) (field string, id int, thumb string, cfg *media_library.MediaBoxConfig) {
  17. field = ctx.R.FormValue("field")
  18. id = ctx.QueryAsInt("id")
  19. thumb = ctx.R.FormValue("thumb")
  20. cfg = stringToCfg(ctx.R.FormValue("cfg"))
  21. return
  22. }
  23. func loadImageCropper(db *gorm.DB) web.EventFunc {
  24. return func(ctx *web.EventContext) (r web.EventResponse, err error) {
  25. msgr := i18n.MustGetModuleMessages(ctx.R, I18nMediaLibraryKey, Messages_en_US).(*Messages)
  26. field, id, thumb, cfg := getParams(ctx)
  27. var m media_library.MediaLibrary
  28. err = db.Find(&m, id).Error
  29. if err != nil {
  30. return
  31. }
  32. moption := m.GetMediaOption()
  33. size := moption.Sizes[thumb]
  34. if size == nil && thumb != media.DefaultSizeKey {
  35. return
  36. }
  37. c := cropper.Cropper().
  38. Src(m.File.URL("original")+"?"+fmt.Sprint(time.Now().Nanosecond())).
  39. ViewMode(cropper.VIEW_MODE_FILL_FIT_CONTAINER).
  40. AutoCropArea(1).
  41. Attr("@input", web.Plaid().
  42. FieldValue("CropOption", web.Var("JSON.stringify($event)")).
  43. String())
  44. if size != nil {
  45. c.AspectRatio(float64(size.Width), float64(size.Height))
  46. }
  47. // Attr("style", "max-width: 800px; max-height: 600px;")
  48. cropOption := moption.CropOptions[thumb]
  49. if cropOption != nil {
  50. c.Value(cropper.Value{
  51. X: float64(cropOption.X),
  52. Y: float64(cropOption.Y),
  53. Width: float64(cropOption.Width),
  54. Height: float64(cropOption.Height),
  55. })
  56. }
  57. r.UpdatePortals = append(r.UpdatePortals, &web.PortalUpdate{
  58. Name: cropperPortalName(field),
  59. Body: web.Scope(
  60. VDialog(
  61. VCard(
  62. VToolbar(
  63. VToolbarTitle(msgr.CropImage),
  64. VSpacer(),
  65. VBtn(msgr.Crop).Color("primary").
  66. Attr(":loading", "locals.cropping").
  67. Attr("@click", web.Plaid().
  68. BeforeScript("locals.cropping = true").
  69. EventFunc(cropImageEvent).
  70. Query("field", field).
  71. Query("id", fmt.Sprint(id)).
  72. Query("thumb", thumb).
  73. FieldValue("cfg", h.JSONString(cfg)).
  74. Go()),
  75. ).Class("pl-2 pr-2"),
  76. VCardText(
  77. c,
  78. ).Attr("style", "max-height: 500px"),
  79. ),
  80. ).Value(true).
  81. Scrollable(true).
  82. MaxWidth("800px"),
  83. ).Init(`{cropping: false}`).VSlot("{ locals }"),
  84. })
  85. return
  86. }
  87. }
  88. func cropImage(db *gorm.DB) web.EventFunc {
  89. return func(ctx *web.EventContext) (r web.EventResponse, err error) {
  90. cropOption := ctx.R.FormValue("CropOption")
  91. // log.Println(cropOption, ctx.Event.Params)
  92. field, id, thumb, cfg := getParams(ctx)
  93. mb := &media_library.MediaBox{}
  94. err = mb.Scan(ctx.R.FormValue(fmt.Sprintf("%s.Values", field)))
  95. if err != nil {
  96. panic(err)
  97. }
  98. if len(cropOption) > 0 {
  99. cropValue := cropper.Value{}
  100. err = json.Unmarshal([]byte(cropOption), &cropValue)
  101. if err != nil {
  102. panic(err)
  103. }
  104. var m media_library.MediaLibrary
  105. err = db.Find(&m, id).Error
  106. if err != nil {
  107. return
  108. }
  109. moption := m.GetMediaOption()
  110. if moption.CropOptions == nil {
  111. moption.CropOptions = make(map[string]*media.CropOption)
  112. }
  113. moption.CropOptions[thumb] = &media.CropOption{
  114. X: int(cropValue.X),
  115. Y: int(cropValue.Y),
  116. Width: int(cropValue.Width),
  117. Height: int(cropValue.Height),
  118. }
  119. moption.Crop = true
  120. err = m.ScanMediaOptions(moption)
  121. if err != nil {
  122. return
  123. }
  124. err = media.SaveUploadAndCropImage(db, &m)
  125. if err != nil {
  126. presets.ShowMessage(&r, err.Error(), "error")
  127. return r, nil
  128. }
  129. mb.FileSizes = m.File.FileSizes
  130. if thumb == media.DefaultSizeKey {
  131. mb.Width = int(cropValue.Width)
  132. mb.Height = int(cropValue.Height)
  133. }
  134. }
  135. r.UpdatePortals = append(r.UpdatePortals, &web.PortalUpdate{
  136. Name: mediaBoxThumbnailsPortalName(field),
  137. Body: mediaBoxThumbnails(ctx, mb, field, cfg, false),
  138. })
  139. return
  140. }
  141. }