beego项目实现多图上传

来源:互联网 发布:南京智推数据是干嘛的 编辑:程序博客网 时间:2024/05/16 23:47
func (this *UploadImageController)Post()  {       var imgType ,_=this.GetInt("type",0)       var allfiles map[string][]*multipart.FileHeader  = this.Ctx.Request.MultipartForm.File       var keys []string       var files []*multipart.FileHeader       for k, vals := range allfiles {              keys = append(keys,k)              files=append(files,vals...)       }       var retArray []interface{}       for i, h := range files {              f ,err:= h.Open()              defer f.Close()              if err != nil{                     this.WriteResponse(api.ResponseUploadFileError,nil)                     return              }              imageId,err:=this.SaveFile(f,h,imgType)              if err != nil{                     this.WriteResponse(api.ResponseUploadFileError,nil)                     return              }              var imageItem =map[string]interface{} {"image_id":imageId,"file_name":h.Filename,"field":keys[i]}              retArray=append(retArray,imageItem)       }       this.WriteResponse(api.ResponseSuccess,api.FmtResponseItems(retArray))

}

//上传图片,返回值为图片idfunc (this *BaseApiController) SaveFile(file multipart.File, fheader *multipart.FileHeader,imgType int) (int64, error) {       var imageId int64 = 0       str := "image/"+MakeDir()       upload_path := beego.AppConfig.String("uploadpath")       dir := upload_path + str       vpath := str + "/"       err := os.MkdirAll(dir, 0755)       if err != nil {              return imageId, err       }       filename := fheader.Filename       radomName:=utils.RandomName()       extName :=utils.ExtName(filename)       largePath := dir + "/" + radomName+"_l"+extName       middlePath := dir + "/" + radomName+"_m"+extName       smallPath := dir + "/" + radomName+"_s"+extName       dst, err := os.Create(largePath)       defer dst.Close()       if err != nil {              return imageId, err       }       if _, err := io.Copy(dst, file); err != nil {              return imageId, err       }       imageSize, _ := beego.AppConfig.GetSection("image")       width_m, _ := strconv.Atoi(imageSize["width_m"])       height_m, _ := strconv.Atoi(imageSize["height_m"])       width_s, _ := strconv.Atoi(imageSize["width_s"])       height_s, _ := strconv.Atoi(imageSize["height_s"])       //加载图片对象       imgObj, err := ReadImage(largePath)       if err != nil {              return imageId, err       }       err = ZoomImage(imgObj,middlePath, width_m, height_m)       if err != nil {              return imageId, err       }       err = Thumbnail(imgObj,smallPath, width_s, height_s)       if err != nil {              return imageId, err       } else {              l := vpath + radomName+"_l"+extName              m := vpath + radomName+"_m"+extName              s := vpath + radomName+"_s"+extName              i := models.NewImage()              rect:=imgObj.Bounds()              imgWidth:=rect.Max.X              imgHeight:=rect.Max.Y              imageId, err = i.InsertImage(filename,l, m, s,imgWidth,imgHeight,imgType)              return imageId, err       }}func MakeDir() string {       return time.Now().Format("2006/01/02")}//等比缩放图片func ZoomImage(src image.Image,savePath string,maxWidth int,maxHeight int) error {       rect := src.Bounds()       width := rect.Max.X       height := rect.Max.Y       _scale := float32(width) / float32(height)       bw := width       bh := height       if bw > maxWidth {              bw = maxWidth              bh = int( float32(bw) / _scale)       } else if bh > maxHeight {              bh = maxHeight              bw = int(float32(bh) * _scale)       }       dst := image.NewRGBA(image.Rect(0, 0, bw, bh))       err := graphics.Scale(dst, src) //缩小图片       if err != nil {              return err       }       err = SaveImage(savePath, dst)       return err}//裁剪图片func Thumbnail(src image.Image,savePath string,maxWidth int,maxHeight int) error {       rect := src.Bounds()       width := rect.Max.X       height := rect.Max.Y       if width < maxWidth{              maxWidth=width       }       if height < maxHeight{              maxHeight=height       }       dst := image.NewRGBA(image.Rect(0, 0, maxWidth, maxHeight))       err := graphics.Thumbnail(dst, src) //缩小图片       if err != nil {              return err       }       err = SaveImage(savePath, dst)       return err}//读取文件func ReadImage(path string) (img image.Image, err error) {       file, err := os.Open(path)       if err != nil {              return       }       defer file.Close()       img, _, err = image.Decode(file) //解码图片       return}//保存文件func SaveImage(path string, img image.Image) (err error) {       imgfile, err := os.Create(path)       defer imgfile.Close()       err = png.Encode(imgfile, img) //编码图片       return}

0 0
原创粉丝点击