本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.6KB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package avatar
  5. import (
  6. "bytes"
  7. "fmt"
  8. "image"
  9. "image/color/palette"
  10. // Enable PNG support:
  11. _ "image/png"
  12. "math/rand"
  13. "time"
  14. "code.gitea.io/gitea/modules/setting"
  15. "github.com/issue9/identicon"
  16. "github.com/nfnt/resize"
  17. "github.com/oliamb/cutter"
  18. )
  19. // AvatarSize returns avatar's size
  20. const AvatarSize = 290
  21. // RandomImageSize generates and returns a random avatar image unique to input data
  22. // in custom size (height and width).
  23. func RandomImageSize(size int, data []byte) (image.Image, error) {
  24. randExtent := len(palette.WebSafe) - 32
  25. rand.Seed(time.Now().UnixNano())
  26. colorIndex := rand.Intn(randExtent)
  27. backColorIndex := colorIndex - 1
  28. if backColorIndex < 0 {
  29. backColorIndex = randExtent - 1
  30. }
  31. // Define size, background, and forecolor
  32. imgMaker, err := identicon.New(size,
  33. palette.WebSafe[backColorIndex], palette.WebSafe[colorIndex:colorIndex+32]...)
  34. if err != nil {
  35. return nil, fmt.Errorf("identicon.New: %v", err)
  36. }
  37. return imgMaker.Make(data), nil
  38. }
  39. // RandomImage generates and returns a random avatar image unique to input data
  40. // in default size (height and width).
  41. func RandomImage(data []byte) (image.Image, error) {
  42. return RandomImageSize(AvatarSize, data)
  43. }
  44. // Prepare accepts a byte slice as input, validates it contains an image of an
  45. // acceptable format, and crops and resizes it appropriately.
  46. func Prepare(data []byte) (*image.Image, error) {
  47. imgCfg, _, err := image.DecodeConfig(bytes.NewReader(data))
  48. if err != nil {
  49. return nil, fmt.Errorf("DecodeConfig: %v", err)
  50. }
  51. if imgCfg.Width > setting.AvatarMaxWidth {
  52. return nil, fmt.Errorf("Image width is too large: %d > %d", imgCfg.Width, setting.AvatarMaxWidth)
  53. }
  54. if imgCfg.Height > setting.AvatarMaxHeight {
  55. return nil, fmt.Errorf("Image height is too large: %d > %d", imgCfg.Height, setting.AvatarMaxHeight)
  56. }
  57. img, _, err := image.Decode(bytes.NewReader(data))
  58. if err != nil {
  59. return nil, fmt.Errorf("Decode: %v", err)
  60. }
  61. if imgCfg.Width != imgCfg.Height {
  62. var newSize, ax, ay int
  63. if imgCfg.Width > imgCfg.Height {
  64. newSize = imgCfg.Height
  65. ax = (imgCfg.Width - imgCfg.Height) / 2
  66. } else {
  67. newSize = imgCfg.Width
  68. ay = (imgCfg.Height - imgCfg.Width) / 2
  69. }
  70. img, err = cutter.Crop(img, cutter.Config{
  71. Width: newSize,
  72. Height: newSize,
  73. Anchor: image.Point{ax, ay},
  74. })
  75. if err != nil {
  76. return nil, err
  77. }
  78. }
  79. img = resize.Resize(AvatarSize, AvatarSize, img, resize.NearestNeighbor)
  80. return &img, nil
  81. }
上海开阖软件有限公司 沪ICP备12045867号-1