压缩头像

pull/39/head
Minho 2017-05-13 14:43:03 +08:00
parent d3427ba2a7
commit d02768aad0
3 changed files with 22 additions and 20 deletions

View File

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

View File

@ -1,9 +1,9 @@
package graphics package graphics
import ( import (
"errors"
"image" "image"
"os" "os"
"errors"
"github.com/nfnt/resize" "github.com/nfnt/resize"
) )
@ -39,7 +39,7 @@ func ImageCopyFromFile(p string,x, y ,w, h int) (image.Image,error) {
return ImageCopy(src, x, y, w, h) 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) 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 {

View File

@ -2,13 +2,14 @@ package graphics
import ( import (
"image" "image"
"image/gif"
"image/jpeg"
"image/png"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"image/jpeg"
"image/png"
"image/gif"
) )
// 将图片保存到指定的路径 // 将图片保存到指定的路径
func SaveImage(p string, src image.Image) error { func SaveImage(p string, src image.Image) error {
@ -22,7 +23,8 @@ func SaveImage(p string, src image.Image) error {
if strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".jpeg") { 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") { } else if strings.EqualFold(ext, ".png") {
err = png.Encode(f, src) err = png.Encode(f, src)
} else if strings.EqualFold(ext, ".gif") { } else if strings.EqualFold(ext, ".gif") {