media.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package util
  2. import (
  3. "bytes"
  4. "fmt"
  5. "image"
  6. "image/gif"
  7. "image/jpeg"
  8. "image/png"
  9. "io"
  10. imaging "github.com/disintegration/imaging"
  11. "github.com/nfnt/resize"
  12. exif "github.com/rwcarlsen/goexif/exif"
  13. "golang.org/x/image/bmp"
  14. )
  15. type ContentsCenter struct {
  16. TargetWidth int
  17. }
  18. type rectangle struct {
  19. Width int
  20. Hegith int
  21. }
  22. // Support image format list -> "png", "jpeg", "gif", "jpg",
  23. func (mc *ContentsCenter) setting() {
  24. image.RegisterFormat("jpeg", "jpeg", jpeg.Decode, jpeg.DecodeConfig)
  25. image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
  26. image.RegisterFormat("gif", "gif", gif.Decode, gif.DecodeConfig)
  27. image.RegisterFormat("bmp", "bmp", bmp.Decode, bmp.DecodeConfig)
  28. mc.TargetWidth = 1440
  29. }
  30. func (mc *ContentsCenter) analyzeImage(reader io.Reader) (bool, string, image.Config) { // protected function
  31. config, format, err := image.DecodeConfig(reader)
  32. if err == nil {
  33. return true, format, config
  34. }
  35. return false, "", image.Config{}
  36. }
  37. func (mc *ContentsCenter) analyzeVideo() bool { // protected function
  38. return false
  39. }
  40. func (mc *ContentsCenter) Resize(fileData []byte) ([]byte, error) {
  41. mc.setting()
  42. var c image.Config
  43. res := false
  44. format := ""
  45. axisSwap := false
  46. r := bytes.NewReader(fileData)
  47. if res, format, c = mc.analyzeImage(io.Reader(r)); !res && !mc.analyzeVideo() {
  48. return nil, fmt.Errorf("Unsupported file format")
  49. }
  50. r = bytes.NewReader(fileData)
  51. img, _, err := image.Decode(r)
  52. if err != nil {
  53. }
  54. // rotate if exif's orient value.
  55. r = bytes.NewReader(fileData)
  56. if _exif, err := exif.Decode(r); err == nil {
  57. if exifInfo, err := _exif.Get(exif.Orientation); err != nil {
  58. fmt.Errorf(err.Error())
  59. } else {
  60. _v := exifInfo.Val[0]
  61. if _v == 0 {
  62. _v = exifInfo.Val[1]
  63. }
  64. // fmt.Printf("exifInfo: %d, %d", _v, exifInfo.Val[1])
  65. if _v == 6 {
  66. img = imaging.Rotate270(img)
  67. axisSwap = true
  68. } else if _v == 1 {
  69. } else if _v == 8 {
  70. img = imaging.Rotate90(img)
  71. axisSwap = true
  72. } else if _v == 3 {
  73. img = imaging.Rotate180(img)
  74. }
  75. }
  76. } else {
  77. fmt.Print("Occurred error when decoding!")
  78. fmt.Print(err.Error())
  79. }
  80. //calculate image ratio
  81. var modImg image.Image
  82. rY := float32(c.Height) / float32(c.Width)
  83. if axisSwap {
  84. modImg = resize.Resize(
  85. (uint)((float32)(mc.TargetWidth)*rY), (uint)(float32(mc.TargetWidth)),
  86. img, resize.Lanczos3)
  87. } else {
  88. modImg = resize.Resize(
  89. (uint)(mc.TargetWidth), (uint)(float32(mc.TargetWidth)*rY),
  90. img, resize.Lanczos3)
  91. }
  92. var buff bytes.Buffer
  93. if format == "png" {
  94. png.Encode(&buff, modImg)
  95. } else if format == "jpeg" || format == "jpg" {
  96. jpeg.Encode(&buff, modImg, &jpeg.Options{70})
  97. } else if format == "gif" {
  98. gif.Encode(&buff, modImg, &gif.Options{NumColors: 256})
  99. } else if format == "bmp" {
  100. png.Encode(&buff, modImg)
  101. }
  102. return buff.Bytes(), nil
  103. }