GoLang缩略图+beego项目

import (
       "Server_sevenstudio/controllers"
       "github.com/astaxie/beego"
       "mime/multipart"
       "os"
       "time"
       "strconv"
       "strings"
       "math/rand"
       "crypto/md5"
       "encoding/hex"
       "regexp"
       "image"
       "code.google.com/p/graphics-go/graphics"
       _"image/jpeg"
       "image/png"
       "fmt"
)

f,h,err_img := this.GetFile("avatar")
if err_img == nil{
       //定义原图,中图,大图路径
       var filepath  = ""
       var filepath_m  = ""
       var filepath_s  = ""
       //定义图片的name属性
       var par_name = "avatar"
       err,filepath,filepath_m,filepath_s = this.SaveFile(f,h,par_name)
....

//上传图片,返回值为原图及缩略图路径
func (this *BaseApiController) SaveFile(f multipart.File,h *multipart.FileHeader,par_name string)(err error,l string,m string,s string){

       defer f.Close()
       now := time.Now()
       upload_path := beego.AppConfig.String("uploadpath")
       dir := upload_path+ strconv.Itoa(now.Year()) + "-" + strconv.Itoa(int(now.Month())) + "/" + strconv.Itoa(now.Day())
       dirf := strconv.Itoa(now.Year()) + "-" + strconv.Itoa(int(now.Month())) + "/" + strconv.Itoa(now.Day())+ "/"
       err = os.MkdirAll(dir, 0755)

       if err != nil {
              return
       }
       filename := h.Filename
       newname := this.FormatFileName(filename)
       imgpath := dir+"/"+newname
       err = this.SaveToFile(par_name, imgpath)

       if err != nil{
              return
       }else {
              image,_ := beego.AppConfig.GetSection("image")
              width_m,_ := strconv.Atoi(image["width_m"])
              width_s,_ := strconv.Atoi(image["width_s"])
              height_m,_ := strconv.Atoi(image["height_m"])
              height_s,_ := strconv.Atoi(image["height_s"])
              err = MakeMilThumb(imgpath,width_m,height_m)
              err = MakeSmallThumb(imgpath,width_s,height_s)
              if err != nil {
                     return
              }else {
                     l = dirf+newname
                     m = dirf+strings.Replace(newname, "_l.", "_m.",1)
                     s = dirf+strings.Replace(newname, "_l.", "_s.",1)
                     return err,l,m,s
              }
       }

}

//middle image
func MakeMilThumb(orgpath string,width int,height int) error {

       src, err := LoadImage(orgpath)

       if err != nil {
              return err
       }
       dst := image.NewRGBA(image.Rect(0, 0, width, height))
       err = graphics.Scale(dst, src)   //缩小图片

       if err != nil {
              return err
       }
       filepath_m := strings.Replace(orgpath, "_l.", "_m.",1)
       err = SaveImage(filepath_m, dst)
       return err
}

//small image
func MakeSmallThumb(orgpath string,width int,height int) error {

       src, err := LoadImage(orgpath)

       if err != nil {
              return err
       }
       dst := image.NewRGBA(image.Rect(0, 0, width, height))
       err = graphics.Scale(dst, src)   //缩小图片

       if err != nil {
              return err
       }
       filepath_s := strings.Replace(orgpath, "_l.", "_s.",1)
       err = SaveImage(filepath_s, dst)
       return err
}

//读取文件
func LoadImage(path string) (img image.Image, err error) {

       file, err := os.Open(path)
       if err != nil {
              return
       }

       defer file.Close()
       img, _, err = image.Decode(file) //解码图片
       fmt.Println(err)
       return
}

//保存文件
func SaveImage(path string, img image.Image) (err error) {

       imgfile, err := os.Create(path)

       defer imgfile.Close()
       err = png.Encode(imgfile, img)   //编码图片
       return
}

//重新生成文件名
func (this *BaseApiController)FormatFileName(filename string) string {

       filepath := strings.Split(filename, ".")
       t := time.Now().UnixNano()
       rndNum := rand.Int63()
       t1 :=  this.Md5(strconv.FormatInt(t,10)+strconv.FormatInt(rndNum,10))
       newname := t1+"_l."+filepath[1]
       return newname
}

你可能感兴趣的:(GoLang)