Browse Source

压缩头像

Minho 8 years ago
parent
commit
d02768aad0
3 changed files with 22 additions and 20 deletions
  1. 2 2
      controllers/setting.go
  2. 12 12
      graphics/copy.go
  3. 8 6
      graphics/file.go

+ 2 - 2
controllers/setting.go

@@ -132,8 +132,8 @@ func (c *SettingController) Upload() {
 		logs.Error("ImageCopyFromFile => ",err)
 		c.JsonResult(6001,"头像剪切失败")
 	}
-
-	err = graphics.SaveImage(filePath,subImg)
+	err = graphics.ImageResizeSaveFile(subImg,120,120,filePath)
+	//err = graphics.SaveImage(filePath,subImg)
 
 	if err != nil {
 		logs.Error("保存文件失败 => ",err.Error())

+ 12 - 12
graphics/copy.go

@@ -1,32 +1,32 @@
 package graphics
 
 import (
+	"errors"
 	"image"
 	"os"
-	"errors"
 
 	"github.com/nfnt/resize"
 )
 
-func ImageCopy(src image.Image,x, y ,w, h int) (image.Image,error)  {
+func ImageCopy(src image.Image, x, y, w, h int) (image.Image, error) {
 
 	var subImg image.Image
 
-	if rgbImg,ok := src.(*image.YCbCr); ok {
+	if rgbImg, ok := src.(*image.YCbCr); ok {
 		subImg = rgbImg.SubImage(image.Rect(x, y, x+w, y+h)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1
-	}else if rgbImg,ok := src.(*image.RGBA); ok {
+	} else if rgbImg, ok := src.(*image.RGBA); ok {
 		subImg = rgbImg.SubImage(image.Rect(x, y, x+w, y+h)).(*image.RGBA) //图片裁剪x0 y0 x1 y1
-	}else if rgbImg,ok := src.(*image.NRGBA); ok {
+	} else if rgbImg, ok := src.(*image.NRGBA); ok {
 		subImg = rgbImg.SubImage(image.Rect(x, y, x+w, y+h)).(*image.NRGBA) //图片裁剪x0 y0 x1 y1
 	} else {
 
-		return subImg,errors.New("图片解码失败")
+		return subImg, errors.New("图片解码失败")
 	}
 
-	return subImg,nil
+	return subImg, nil
 }
 
-func ImageCopyFromFile(p string,x, y ,w, h int) (image.Image,error) {
+func ImageCopyFromFile(p string, x, y, w, h int) (image.Image, error) {
 	var src image.Image
 
 	file, err := os.Open(p)
@@ -39,11 +39,11 @@ func ImageCopyFromFile(p string,x, y ,w, h int) (image.Image,error) {
 	return ImageCopy(src, x, y, w, h)
 }
 
-func ImageResize(src image.Image,w,h int) (image.Image) {
+func ImageResize(src image.Image, w, h int) image.Image {
 	return resize.Resize(uint(w), uint(h), src, resize.Lanczos3)
 }
-func ImageResizeSaveFile(src image.Image,width,height int,p string) error {
+func ImageResizeSaveFile(src image.Image, width, height int, p string) error {
 	dst := resize.Resize(uint(width), uint(height), src, resize.Lanczos3)
 
-	return SaveImage(p,dst)
-}
+	return SaveImage(p, dst)
+}

+ 8 - 6
graphics/file.go

@@ -2,17 +2,18 @@ package graphics
 
 import (
 	"image"
+	"image/gif"
+	"image/jpeg"
+	"image/png"
 	"os"
 	"path/filepath"
 	"strings"
-	"image/jpeg"
-	"image/png"
-	"image/gif"
 )
+
 // 将图片保存到指定的路径
 func SaveImage(p string, src image.Image) error {
 
-	f, err := os.OpenFile( p, os.O_SYNC | os.O_RDWR | os.O_CREATE, 0666)
+	f, err := os.OpenFile(p, os.O_SYNC|os.O_RDWR|os.O_CREATE, 0666)
 
 	if err != nil {
 		return err
@@ -22,11 +23,12 @@ func SaveImage(p string, src image.Image) error {
 
 	if strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".jpeg") {
 
-		err = jpeg.Encode(f, src, &jpeg.Options{Quality : 100 })
+		err = jpeg.Encode(f, src, &jpeg.Options{Quality: 80})
+
 	} else if strings.EqualFold(ext, ".png") {
 		err = png.Encode(f, src)
 	} else if strings.EqualFold(ext, ".gif") {
-		err = gif.Encode(f, src, &gif.Options{NumColors : 256})
+		err = gif.Encode(f, src, &gif.Options{NumColors: 256})
 	}
 	return err
 }