image.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package image
  2. import (
  3. "fmt"
  4. "github.com/gographics/gmagick"
  5. "icloudapp.cn/tools/util"
  6. fileObj "icloudapp.cn/tools/util/file"
  7. )
  8. type Image struct {
  9. Cache
  10. Path string
  11. }
  12. func NewImage(path string) *Image {
  13. return &Image{
  14. Path: path,
  15. }
  16. }
  17. func (i *Image) Create(file, background string, size Size) (*gmagick.MagickWand, error) {
  18. if file != "" {
  19. file = fmt.Sprintf("%s/%s", i.Path, file)
  20. if !fileObj.IsFile(file) {
  21. return nil, util.NewError(fmt.Sprintf("文件 %s 不存在", file))
  22. }
  23. }
  24. imageMW := gmagick.NewMagickWand()
  25. if file != "" {
  26. if err := imageMW.ReadImage(file); err != nil {
  27. return nil, util.NewError(fmt.Sprintf("imageMW.ReadImageBlob:%s", err.Error()))
  28. }
  29. } else {
  30. if err := imageMW.ReadImageBlob(util.TransparentPNG()); err != nil {
  31. return nil, util.NewError(fmt.Sprintf("imageMW.ReadImageBlob:%s", err.Error()))
  32. }
  33. }
  34. fmt.Println("size", size)
  35. if err := imageMW.SetImageFormat("PNG"); err != nil {
  36. return nil, util.NewError("imageMW.SetImageFormat", err.Error())
  37. }
  38. if err := imageMW.ResizeImage(size.Width, size.Height, gmagick.FILTER_LANCZOS, 1); err != nil {
  39. return nil, util.NewError("imageMW.SetSize", err.Error())
  40. }
  41. if background != "" {
  42. if err := imageMW.SetImageBackgroundColor(Pixel(background)); err != nil {
  43. return nil, util.NewError(fmt.Sprintf("imageMW.SetImageBackgroundColor:%s", err.Error()))
  44. }
  45. }
  46. return imageMW, nil
  47. }
  48. func (i *Image) Thumb(file string, size *Size) (string, error) {
  49. if !fileObj.IsFile(file) {
  50. return "", util.NewError(fmt.Sprintf("文件 %s 不存在", file))
  51. }
  52. mw := gmagick.NewMagickWand()
  53. defer mw.Destroy()
  54. var err error
  55. if err = mw.ReadImage(file); err != nil {
  56. return "", err
  57. }
  58. width := mw.GetImageWidth()
  59. height := mw.GetImageHeight()
  60. imageScale := NewScale(width, height, 1.0)
  61. scale := imageScale.MinRatio(size.Width, size.Height)
  62. if err = mw.ResizeImage(scale.Width, scale.Height, gmagick.FILTER_LANCZOS, 1); err != nil {
  63. return "", err
  64. }
  65. //fmt.Println("DisplayImage:", os.Getenv("DISPLAY"))
  66. //err = mw.DisplayImage(os.Getenv("DISPLAY"))
  67. destFile := i.CacheFile(file, mw.GetImageFormat())
  68. if err = mw.WriteImage(destFile); err != nil {
  69. return "", err
  70. }
  71. return destFile, nil
  72. }
  73. func (i *Image) Cut(file string, size *Size, position string) (string, error) {
  74. if position == "" {
  75. position = "left"
  76. }
  77. mw := gmagick.NewMagickWand()
  78. defer mw.Destroy()
  79. var err error
  80. err = mw.ReadImage(file)
  81. if err != nil {
  82. return "", err
  83. }
  84. sourceWidth := mw.GetImageWidth()
  85. sourceHeight := mw.GetImageHeight()
  86. imageScale := NewScale(sourceWidth, sourceHeight, 1.0)
  87. scale := imageScale.MaxRatio(size.Width, size.Height)
  88. if err = mw.ResizeImage(scale.Width, scale.Height, gmagick.FILTER_LANCZOS, 1); err != nil {
  89. return "", err
  90. }
  91. x, y, width, height := i.Position(position, scale.Width, scale.Height, size)
  92. if err = mw.CropImage(width, height, x, y); err != nil {
  93. return "", err
  94. }
  95. destFile := i.CacheFile(file, mw.GetImageFormat())
  96. if err = mw.WriteImage(destFile); err != nil {
  97. return "", err
  98. }
  99. return destFile, nil
  100. }
  101. func (i *Image) Resize(image string, width, height uint, angle int) (*gmagick.MagickWand, error) {
  102. imageMV := gmagick.NewMagickWand()
  103. if err := imageMV.ReadImage(image); err != nil {
  104. return nil, util.NewError("Resize", "ReadImage", err.Error())
  105. }
  106. if err := imageMV.ResizeImage(width, height, gmagick.FILTER_LANCZOS, 1); err != nil {
  107. return nil, util.NewError("Resize", "ResizeImage", err.Error())
  108. }
  109. if err := imageMV.SetSize(width, height); err != nil {
  110. return nil, util.NewError("Resize", "SetSize", err.Error())
  111. }
  112. return imageMV, nil
  113. }
  114. // 计算 裁切的x、y及宽高
  115. func (i *Image) Position(position string, width, height uint, size *Size) (dx, dy int, dWidth, dHeight uint) {
  116. x := 0
  117. y := 0
  118. scale := 1.0
  119. if width < size.Width {
  120. scale = CustomScale(width, size.Width)
  121. width = size.Width
  122. }
  123. if height < size.Height {
  124. scale = CustomScale(height, size.Height)
  125. height = size.Height
  126. }
  127. paddingLeft := width - size.Width
  128. paddingTop := height - size.Height
  129. if position == "right" {
  130. x = int(paddingLeft)
  131. }
  132. if position == "center" {
  133. x = int(paddingLeft / 2)
  134. y = int(paddingTop / 2)
  135. }
  136. if position == "right|center" {
  137. x = int(paddingLeft)
  138. y = int(paddingTop / 2)
  139. }
  140. if position == "left|center" {
  141. y = int(paddingTop / 2)
  142. }
  143. return x, y, uint(float64(size.Width) * scale), uint(float64(size.Height) * scale)
  144. }
  145. // Cache 获取缓存文件名
  146. /*func (i *Image) CacheFile(file string, format string) string {
  147. if !fileObj.IsDir(i.Cache) {
  148. if err := os.Mkdir(i.Cache, 0755); err != nil {
  149. panic(err.Error())
  150. }
  151. }
  152. if file == "" {
  153. file = util.RandomStr(10)
  154. }
  155. destName := fmt.Sprintf("%x", md5.Sum([]byte(file)))
  156. destFile := fmt.Sprintf("%s/%s.%s", i.Cache, destName, strings.ToLower(format))
  157. return destFile
  158. }*/