package main import ( "bytes" "fmt" "github.com/flamego/flamego" "github.com/minio/minio-go/v7" "github.com/nfnt/resize" "image" "image/png" "net/http" "os" "strconv" ) func getImage(c flamego.Context) { path := c.Request().FormValue("path") img, err := s3.GetObject(c.Request().Context(), os.Getenv("S3_BUCKET"), path, minio.GetObjectOptions{}) if err != nil { panic(err) } defer img.Close() imgO, _ , err := image.Decode(img) if err != nil { panic(err) } widthString := c.Request().FormValue("width") heightString := c.Request().FormValue("height") var width uint var height uint height = 0 width = 500 if widthString != "" { w, _ := strconv.ParseUint(widthString, 10, 32) width = uint(w) } if heightString != "" { h, _ := strconv.ParseUint(heightString, 10, 32) height = uint(h) width = 0 } if height == 250 || width == 500 { //cache } m := resize.Resize(width, height, imgO, resize.Lanczos3) buf := new(bytes.Buffer) err = png.Encode(buf, m) if err != nil { panic(err) } send_s3 := buf.Bytes() c.ResponseWriter().WriteHeader(http.StatusOK) c.ResponseWriter().Header().Set("Content-Type", "application/octet-stream") c.ResponseWriter().Header().Set("Content-Length", fmt.Sprintf("%v", len(send_s3))) c.ResponseWriter().Write(send_s3) return }