Upload 上传图片
()
| 229 | |
| 230 | // Upload 上传图片 |
| 231 | func (this *SettingController) Upload() { |
| 232 | |
| 233 | file, moreFile, err := this.GetFile("image-file") |
| 234 | if err != nil { |
| 235 | logs.Error("", err.Error()) |
| 236 | this.JsonResult(500, "读取文件异常") |
| 237 | } |
| 238 | defer file.Close() |
| 239 | |
| 240 | ext := filepath.Ext(moreFile.Filename) |
| 241 | if !strings.EqualFold(ext, ".png") && !strings.EqualFold(ext, ".jpg") && !strings.EqualFold(ext, ".gif") && !strings.EqualFold(ext, ".jpeg") { |
| 242 | this.JsonResult(500, "不支持的图片格式") |
| 243 | } |
| 244 | |
| 245 | x1, _ := strconv.ParseFloat(this.GetString("x"), 10) |
| 246 | y1, _ := strconv.ParseFloat(this.GetString("y"), 10) |
| 247 | w1, _ := strconv.ParseFloat(this.GetString("width"), 10) |
| 248 | h1, _ := strconv.ParseFloat(this.GetString("height"), 10) |
| 249 | |
| 250 | x := int(x1) |
| 251 | y := int(y1) |
| 252 | width := int(w1) |
| 253 | height := int(h1) |
| 254 | |
| 255 | // fmt.Println(x, x1, y, y1) |
| 256 | |
| 257 | fileName := strconv.FormatInt(time.Now().UnixNano(), 16) |
| 258 | |
| 259 | filePath := filepath.Join("uploads", time.Now().Format("2006/01"), fileName+ext) |
| 260 | |
| 261 | path := filepath.Dir(filePath) |
| 262 | |
| 263 | os.MkdirAll(path, os.ModePerm) |
| 264 | |
| 265 | err = this.SaveToFile("image-file", filePath) |
| 266 | |
| 267 | if err != nil { |
| 268 | logs.Error("", err) |
| 269 | this.JsonResult(500, "图片保存失败") |
| 270 | } |
| 271 | |
| 272 | //剪切图片 |
| 273 | subImg, err := graphics.ImageCopyFromFile(filePath, x, y, width, height) |
| 274 | |
| 275 | if err != nil { |
| 276 | logs.Error("ImageCopyFromFile => ", err) |
| 277 | this.JsonResult(6001, "头像剪切失败") |
| 278 | } |
| 279 | os.Remove(filePath) |
| 280 | |
| 281 | filePath = filepath.Join("uploads", time.Now().Format("200601"), fileName+ext) |
| 282 | |
| 283 | err = graphics.ImageResizeSaveFile(subImg, 120, 120, filePath) |
| 284 | err = graphics.SaveImage(filePath, subImg) |
| 285 | |
| 286 | if err != nil { |
| 287 | logs.Error("保存文件失败 => ", err.Error()) |
| 288 | this.JsonResult(500, "保存文件失败") |
nothing calls this directly
no test coverage detected