imageutil.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package image
  2. import (
  3. "image"
  4. "image/jpeg"
  5. "image/png"
  6. "log"
  7. "os"
  8. "runtime"
  9. "strings"
  10. )
  11. func LoadImg(filepath string) (img image.Image, err error) {
  12. file, err := os.Open(filepath)
  13. if err != nil {
  14. return
  15. }
  16. defer file.Close()
  17. img, _, err = image.Decode(file)
  18. return
  19. }
  20. func MakeResize(path string, newX, newY, quality int, t int) (newName string, err error) {
  21. defer func() {
  22. if r := recover(); r != nil {
  23. log.Println("[E]", r)
  24. for skip := 1; ; skip++ {
  25. _, file, line, ok := runtime.Caller(skip)
  26. if !ok {
  27. break
  28. }
  29. go log.Printf("%v,%v\n", file, line)
  30. }
  31. }
  32. }()
  33. img, err := LoadImg(path)
  34. if nil == err {
  35. path := strings.Replace(strings.Replace(path, "\\\\", "\\", -1), "\\", "/", -1)
  36. strs := strings.Split(path, "/")
  37. oname := strs[len(strs)-1]
  38. onames := strings.Split(oname, ".")
  39. // 产生缩略图,等比例缩放
  40. b := img.Bounds()
  41. //判断大小
  42. var newBounds image.Rectangle
  43. if newX < 0 && newY < 0 {
  44. newBounds = b
  45. } else if newX > 0 && newY < 0 {
  46. newYY := (float32(newX) / float32(b.Dx())) * float32(b.Dy())
  47. newBounds = image.Rect(0, 0, newX, int(newYY))
  48. } else if newY > 0 && newX < 0 {
  49. newXX := (float32(newY) / float32(b.Dy())) * float32(b.Dx())
  50. newBounds = image.Rect(0, 0, int(newXX), newY)
  51. } else {
  52. newBounds = image.Rect(0, 0, newX, newY)
  53. }
  54. var max int
  55. if newBounds.Dx() >= newBounds.Dy() {
  56. max = newBounds.Dx()
  57. } else {
  58. max = newBounds.Dy()
  59. }
  60. // If it's gigantic, it's more efficient to downsample first
  61. // and then resize; resizing will smooth out the roughness.
  62. var i1 *image.RGBA
  63. if b.Dx() > 4*max || b.Dy() > 4*max {
  64. w, h := 2*max, 2*max
  65. if b.Dx() > b.Dy() {
  66. h = b.Dy() * h / b.Dx()
  67. } else {
  68. w = b.Dx() * w / b.Dy()
  69. }
  70. i1 = Resample(img, b, w, h)
  71. } else {
  72. // "Resample" to same size, just to convert to RGBA.
  73. i1 = Resample(img, b, b.Dx(), b.Dy())
  74. }
  75. b = i1.Bounds()
  76. // Encode to PNG.
  77. dx, dy := newBounds.Dx(), newBounds.Dy()
  78. log.Println(b.Dx(), b.Dy(), dx, dy, max)
  79. if b.Dx() > b.Dy() {
  80. dy = b.Dy() * dx / b.Dx()
  81. } else {
  82. dx = b.Dx() * dy / b.Dy()
  83. }
  84. i128 := ResizeRGBA(i1, i1.Bounds(), dx, dy)
  85. var s = "/s_"
  86. if t == 1 {
  87. s = "/"
  88. }
  89. newF, err1 := os.Create(strings.Join(strs[:(len(strs)-1)], "/") + s + oname)
  90. defer newF.Close()
  91. var err error
  92. switch strings.ToLower(onames[1]) {
  93. case "jpg":
  94. err = jpeg.Encode(newF, i128, &jpeg.Options{quality})
  95. case "jpeg":
  96. err = jpeg.Encode(newF, i128, &jpeg.Options{quality})
  97. case "png":
  98. err = png.Encode(newF, i128)
  99. }
  100. if err != nil {
  101. log.Println("生成缩略图出错", err)
  102. }
  103. return strings.Replace(s, "/", "", -1) + oname, err1
  104. }
  105. return "", err
  106. }
  107. //是否达到压缩条件
  108. func IsCompress(path string) string {
  109. img, err := LoadImg(path)
  110. log.Println(err)
  111. if nil == err {
  112. x := img.Bounds().Dx()
  113. y := img.Bounds().Dy()
  114. if x > 1920 {
  115. if x > y {
  116. return "x"
  117. } else {
  118. return "y"
  119. }
  120. } else {
  121. if y > 1080 {
  122. return "y"
  123. }
  124. }
  125. }
  126. return ""
  127. }
  128. func MakeShareImage(path string) string {
  129. path = "./web/staticres" + path
  130. image, err := LoadImg(path)
  131. if err != nil {
  132. return "f"
  133. }
  134. x := image.Bounds().Dx()
  135. y := image.Bounds().Dy()
  136. if x < 300 && y < 300 {
  137. if x > y {
  138. x = -1
  139. y = 350
  140. } else {
  141. y = -1
  142. x = 350
  143. }
  144. } else if x < 300 && y > 300 {
  145. y = -1
  146. x = 350
  147. } else if x > 300 && y < 300 {
  148. x = -1
  149. y = 350
  150. }
  151. log.Println("x:", x, "y:", y)
  152. rs, _ := MakeResize(path, x, y, 100, 0)
  153. log.Println("rs:", rs)
  154. return rs
  155. }