func uploadUser(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { // No upload; show the upload form. uploadUserTemplate.Execute(w, nil) return } f, _, err := r.FormFile("image") errors.Check(err) defer f.Close() // Grab the image data i, _, err := image.Decode(f) errors.Check(err) var key string = keyOf() // store image i = picstore.Store(key, i, 320, "userimg") i = picstore.Store(key, i, 180, "userthumb") // generate result w.Header().Set("Content-Type", "application/json") w.Header().Set("cache-control", "no-cache") w.Header().Set("Expires", "-1") fmt.Fprintf(w, "{\"profilePicUrl\":\"uimg?id="+key+"\",\"profileThumbUrl\":\"utmb?id="+key+"\"}") }
// read image by key from storage func Read(key string, storagename string) []byte { mu.Lock() storage, _ := gocask.NewGocask("images/" + storagename) buf, err := storage.Get(key) storage.Close() mu.Unlock() errors.Check(err) return buf }
func StoreSubImage(key string, i image.Image, vert int, hor int, storename string) { img := cropImage(i, vert, hor) buf := new(bytes.Buffer) buf.Reset() err := jpeg.Encode(buf, img, nil) errors.Check(err) var barray []byte = buf.Bytes() mu.Lock() storage, _ := gocask.NewGocask("images/" + storename) err = storage.Put(key, barray) storage.Close() mu.Unlock() errors.Check(err) }
// resize image, store and return resized image func Store(key string, i image.Image, maxsize int, storename string) image.Image { // store big image i = resizeImage(i, maxsize) // Масштабируем пропорционально до maxsize пикселей, если какая-либо сторона больше. // Encode as a new JPEG image. buf := new(bytes.Buffer) buf.Reset() err := jpeg.Encode(buf, i, nil) errors.Check(err) var barray []byte = buf.Bytes() mu.Lock() storage, _ := gocask.NewGocask("images/" + storename) err = storage.Put(key, barray) storage.Close() mu.Unlock() errors.Check(err) return i }