This commit is contained in:
Expand-sys 2022-08-18 22:13:02 +10:00
parent ec27b01403
commit 530779ff65
12 changed files with 94 additions and 60 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 74 KiB

BIN
cat.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

BIN
files/enemyfelled.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 364 KiB

1
go.mod
View file

@ -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

2
go.sum
View file

@ -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=

BIN
img.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 101 KiB

BIN
index.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.9 KiB

119
main.go
View file

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

BIN
out.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 750 KiB

After

Width:  |  Height:  |  Size: 355 KiB

View file

@ -7,18 +7,32 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Enemy Felled</title>
<link rel="stylesheet" href="/stylesheets/style.css" />
<script src="/js/script.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/uuid/8.1.0/uuidv4.min.js"></script>
<body>
{{if .}}
<center>
<img src="/enemyfelled.png" /></marquee>
<img src={{.}} style="width:95vw;height:95vh">
<p>{{.}}</p>
</center>
{{else}}
<center>
<img src="/files/enemyfelled.png" style="width: 50vw;">
<form id=upload action="/submit" enctype="multipart/form-data">
<form action="/" method="post" enctype="multipart/form-data">
<h2>a dumb lil elden ring generator i made (ALPHA)</h2>
<input type="file" name="file" required></br></br>
<h3>Enter your text</h3>
<input type="text" name="text" id="text" required>
<h3>COLORS OH MY GHAD!</h3>
<p>Red value(0-255)</p>
<input type="number" name="red" min="0" max="255" value="0">
<p>Green value(0-255)</p>
<input type="number" name="green" min="0" max="255" value="0">
<p>Blue value(0-255)</p>
<input type="number" name="blue" min="0" max="255" value="0">
<button type=submit class="btn btn-primary">Upload</button>
</form>
</center>
@ -26,12 +40,12 @@
<h4>More options coming soon same with some styling to make it not look like another of my projects</h4>
</center>
<div class="mention">
<center>
<p>Big thanks to</p>
<a href="https://TAWAN475.dev/eldenring">tawan475.dev</a></marquee>
<p>btw web design is my passion</p>
</center>
</div>
{{end}}
<center>
<p>made in go because i thought javascript was talkin to the feds</p>
</center>
</body>
</html>

BIN
temp.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.1 KiB

BIN
vote.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 252 KiB