压缩头像

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,32 +1,32 @@
package graphics package graphics
import ( import (
"errors"
"image" "image"
"os" "os"
"errors"
"github.com/nfnt/resize" "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 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 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 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 subImg = rgbImg.SubImage(image.Rect(x, y, x+w, y+h)).(*image.NRGBA) //图片裁剪x0 y0 x1 y1
} else { } 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 var src image.Image
file, err := os.Open(p) 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) 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 {
dst := resize.Resize(uint(width), uint(height), src, resize.Lanczos3) dst := resize.Resize(uint(width), uint(height), src, resize.Lanczos3)
return SaveImage(p,dst) return SaveImage(p, dst)
} }

View File

@ -2,17 +2,18 @@ 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 {
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 { if err != nil {
return err return err
@ -22,11 +23,12 @@ 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") {
err = gif.Encode(f, src, &gif.Options{NumColors : 256}) err = gif.Encode(f, src, &gif.Options{NumColors: 256})
} }
return err return err
} }