// 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 }