diff --git a/VAN_CAT.png b/VAN_CAT.png deleted file mode 100644 index 2cbd301..0000000 Binary files a/VAN_CAT.png and /dev/null differ diff --git a/cat.png b/cat.png deleted file mode 100644 index e2137b2..0000000 Binary files a/cat.png and /dev/null differ diff --git a/files/enemyfelled.png b/files/enemyfelled.png new file mode 100644 index 0000000..20da518 Binary files /dev/null and b/files/enemyfelled.png differ diff --git a/go.mod b/go.mod index f6fc0ce..17ad00e 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/fatih/color v1.13.0 // indirect github.com/fogleman/gg v1.3.0 // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect + github.com/google/uuid v1.3.0 github.com/gorilla/mux v1.8.0 // indirect github.com/mattn/go-colorable v0.1.9 // indirect github.com/mattn/go-isatty v0.0.14 // indirect diff --git a/go.sum b/go.sum index 8411110..421cb4a 100644 --- a/go.sum +++ b/go.sum @@ -16,6 +16,8 @@ github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzP github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= diff --git a/img.png b/img.png deleted file mode 100644 index 5e23faa..0000000 Binary files a/img.png and /dev/null differ diff --git a/index.png b/index.png deleted file mode 100644 index 6bfd504..0000000 Binary files a/index.png and /dev/null differ diff --git a/main.go b/main.go index c7e505b..9f8d2f6 100644 --- a/main.go +++ b/main.go @@ -2,77 +2,94 @@ package main import ( "fmt" - "image" + "html/template" "image/color" "io" "log" "net/http" "os" + "path/filepath" + "strconv" "github.com/disintegration/imaging" "github.com/fogleman/gg" - "github.com/gorilla/mux" + "github.com/google/uuid" ) -func routeHandler(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/" { - http.Error(w, "404 not found", http.StatusNotFound) - return - } - if r.Method != "GET" { - http.Error(w, "method is not supported", http.StatusNotFound) - return - } - http.ServeFile(w, r, "static/index.html") -} - -func formHandler(w http.ResponseWriter, r *http.Request) { - file, handler, err := r.FormFile("file") - fileName := r.FormValue("file_name") +func FileSave(r *http.Request) string { + // left shift 32 << 20 which results in 32*2^20 = 33554432 + // x << y, results in x*2^y + err := r.ParseMultipartForm(32 << 20) if err != nil { - panic(err) + fmt.Println("error parsing") + return "" } - defer file.Close() - - f, err := os.OpenFile(handler.Filename, os.O_WRONLY|os.O_CREATE, 0666) + uuid := uuid.NewString() + fmt.Println(uuid) + // Retrieve the file from form data + f, h, err := r.FormFile("file") if err != nil { - panic(err) + fmt.Println("error retrieving file") + + return "" } defer f.Close() - _, _ = io.WriteString(w, "File "+fileName+" Uploaded successfully") - _, _ = io.Copy(f, file) + path := filepath.Join(".", "files") + _ = os.MkdirAll(path, os.ModePerm) + fullPath := path + "/" + uuid + filepath.Ext(h.Filename) + file, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE, os.ModePerm) + if err != nil { + fmt.Println("error ", err) + + return "" - if err := r.ParseMultipartForm(8192); err != nil { - fmt.Fprintf(w, "ParseMultipartForm() err: %v", err) - return } - fmt.Fprintf(w, "POST request successful") - - //text := r.FormValue("text") - + defer file.Close() + // Copy the file to the destination path + _, err = io.Copy(file, f) + if err != nil { + return "" + } + return fullPath } func main() { - text := "this is a test" - testimage := Request{"img.png", "Garamond.ttf", text, 255, 0, 0} - TextOnImg(testimage) - r := mux.NewRouter() + + fs := http.FileServer(http.Dir("files/")) + http.Handle("/files/", http.StripPrefix("/files/", fs)) + + tmpl, err := template.ParseFiles("static/index.html") + fmt.Println(err) // Routes consist of a path and a handler function. - r.HandleFunc("/", routeHandler). - Methods("GET") - r.HandleFunc("/submit", formHandler). - Methods("POST") + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + tmpl.Execute(w, nil) + return + } + filePath := FileSave(r) + fmt.Println(r.FormValue("red")) + red, err := strconv.Atoi(r.FormValue("red")) + green, err := strconv.Atoi(r.FormValue("green")) + blue, err := strconv.Atoi(r.FormValue("blue")) + subject := Requestpic{filePath, r.FormValue("text"), uint8(red), uint8(green), uint8(blue)} + Imgreturned, err := TextOnImg(subject) + if err != nil { + fmt.Println("error") + tmpl.Execute(w, err) + } + fmt.Println(Imgreturned) + tmpl.Execute(w, Imgreturned) + }) // Bind to a port and pass our router in - log.Fatal(http.ListenAndServe(":8000", r)) + log.Fatal(http.ListenAndServe(":8000", nil)) } -type Request struct { +type Requestpic struct { BgImgPath string - FontPath string - Text string + Textinput string TextColorR uint8 TextColorG uint8 TextColorB uint8 @@ -84,7 +101,7 @@ func pxTopt(pt float64) (px float64) { return second } -func TextOnImg(request Request) (image.Image, error) { +func TextOnImg(request Requestpic) (string, error) { bgImage, err := gg.LoadImage(request.BgImgPath) if err != nil { fmt.Println(err) @@ -99,14 +116,14 @@ func TextOnImg(request Request) (image.Image, error) { fontsize := pxTopt(float64(imgHeight) / 10.0) - if err := dc.LoadFontFace(request.FontPath, fontsize); err != nil { + if err := dc.LoadFontFace("Garamond.ttf", fontsize); err != nil { fmt.Println(err) } x := float64(imgWidth / 2) y := float64((imgHeight / 2) - int(fontsize)/2) ac := gg.NewContext(imgHeight, imgWidth) - ac.DrawRectangle(0, 0, float64(imgHeight), float64(imgWidth)) + ac.DrawRectangle(0, 0, float64(imgHeight), float64(imgWidth)*1.2) grad := gg.NewLinearGradient(fontsize, 0, float64(imgHeight), 0) grad.AddColorStop(0, color.RGBA{0, 0, 0, 0}) grad.AddColorStop(0.25, color.RGBA{0, 0, 0, 50}) @@ -127,14 +144,14 @@ func TextOnImg(request Request) (image.Image, error) { maxWidth := float64(imgWidth) - 60.0 dc.SetColor(color.RGBA{request.TextColorR, request.TextColorG, request.TextColorB, 255}) - dc.DrawStringWrapped(request.Text, x, y, 0.5, 0.5, maxWidth, 1.5, gg.AlignCenter) + dc.DrawStringWrapped(request.Textinput, x, y*0.95, 0.5, 0.5, maxWidth, 1.5, gg.AlignCenter) - if err := dc.LoadFontFace(request.FontPath, fontsize*1.05); err != nil { + if err := dc.LoadFontFace("Garamond.ttf", fontsize*1.05); err != nil { fmt.Println(err) } dc.SetColor(color.RGBA{request.TextColorR, request.TextColorG, request.TextColorB, 150}) - dc.DrawStringWrapped(request.Text, x, y, 0.5, 0.5, maxWidth, 1.5, gg.AlignCenter) + dc.DrawStringWrapped(request.Textinput, x, y*0.95, 0.5, 0.5, maxWidth, 1.5, gg.AlignCenter) - dc.SavePNG("./out.png") - return dc.Image(), nil + dc.SavePNG(request.BgImgPath) + return request.BgImgPath, nil } diff --git a/out.png b/out.png index 2438910..4bb54be 100644 Binary files a/out.png and b/out.png differ diff --git a/static/index.html b/static/index.html index af50972..04a44c2 100644 --- a/static/index.html +++ b/static/index.html @@ -7,18 +7,32 @@
+ {{.}}
+
-
Big thanks to
- tawan475.dev -btw web design is my passion
-made in go because i thought javascript was talkin to the feds
+