This commit is contained in:
Atridad Lahiji 2024-02-22 14:14:11 -07:00
parent 044bd9820e
commit 3bebca6b48
No known key found for this signature in database
8 changed files with 196 additions and 0 deletions

10
api/resize.go Normal file
View file

@ -0,0 +1,10 @@
package api
import (
"atri.dad/lib"
"github.com/labstack/echo/v4"
)
func ResizeHandler(c echo.Context) error {
return lib.ResizeImg(c)
}

61
lib/img.go Normal file
View file

@ -0,0 +1,61 @@
package lib
import (
"bytes"
"image"
"image/png"
"io"
"net/http"
"strconv"
"github.com/anthonynsimon/bild/transform"
"github.com/labstack/echo/v4"
)
func ResizeImg(c echo.Context) error {
// Extract file from request
file, _, err := c.Request().FormFile("image")
if err != nil {
return c.String(http.StatusBadRequest, "Error getting image file")
}
defer file.Close()
// Read file content
fileContent, err := io.ReadAll(file)
if err != nil {
return c.String(http.StatusBadRequest, "Error reading image file")
}
// Decode image
img, _, err := image.Decode(bytes.NewReader(fileContent))
if err != nil {
return c.String(http.StatusBadRequest, "Error decoding image")
}
// Get dimensions from form data parameters
widthStr := c.FormValue("width")
heightStr := c.FormValue("height")
// Validate and convert dimensions to integers
width, err := strconv.Atoi(widthStr)
if err != nil {
return c.String(http.StatusBadRequest, "Invalid width parameter")
}
height, err := strconv.Atoi(heightStr)
if err != nil {
return c.String(http.StatusBadRequest, "Invalid height parameter")
}
// Resize the image
resizedImg := transform.Resize(img, width, height, transform.Linear)
// Encode the resized image as PNG
buf := new(bytes.Buffer)
if err := png.Encode(buf, resizedImg); err != nil {
return c.String(http.StatusInternalServerError, "Error encoding image to PNG")
}
// Return the resized image as response
return c.Blob(http.StatusOK, "image/png", buf.Bytes())
}

39
pages/resize.go Normal file
View file

@ -0,0 +1,39 @@
package pages
import (
"atri.dad/lib"
"github.com/labstack/echo/v4"
)
type ResizeProps struct {
Talks []lib.CardLink
}
func Resize(c echo.Context) error {
talks := []lib.CardLink{
{
Name: "How to ship less JavaScript",
Description: "A talk on building websites while being mindful of the JavaScript we ship. Presented at the Dev Edmonton July 2023 JS/Ruby/Python Meetup",
Href: "https://github.com/atridadl/devedmonton-july-2023",
Tags: []string{"astro", "ssr"},
Date: "July 06, 2023",
},
{
Name: "Hypermedia as the engine of application state - an Introduction",
Description: "A talk on building reactive websites using tools like HTMX instead of JSON + JS. Will be presented at the Dev Edmonton Fabruary 2024 JS/Ruby/Python Meetup",
Href: lib.GeneratePublicURL("hypermedia_talk_atridad.pdf"),
Tags: []string{"golang", "htmx", "ssr"},
Date: "February 01, 2024",
},
}
props := TalkProps{
Talks: talks,
}
// Specify the partials used by this page
partials := []string{"header", "navitems", "cardlinks"}
// Render the template
return lib.RenderTemplate(c.Response().Writer, "base", partials, props)
}

View file

@ -0,0 +1,31 @@
{{define "title"}}
Atridad Lahiji // Tools // Resizer
{{end}}
{{define "headercontent"}}
Atridad Lahiji // Tools // Resizer
{{end}}
{{define "head"}}
<link rel="stylesheet" href="/public/css/styles.resize.css" />
{{end}}
{{define "main"}}
<h2 class="text-2xl font-extrabold tracking-tight text-white sm:text-[2rem]">Image Resizer</h2>
<form action="/api/resize" method="post" enctype="multipart/form-data" class="flex-col flex gap-4">
Select image to resize:
<input type="file" name="image" accept="image/*"
class="file-input file-input-bordered file-input-secondary w-full max-w-xs" required />
<br>
New width (px):
<input type="number" id="newWidth" name="width" min="1" class="input input-bordered w-full max-w-xs" required>
<br>
New height (px):
<input type="number" id="newHeight" name="height" min="1" class="input input-bordered w-full max-w-xs" required>
<br>
<button type="submit" class="btn btn-secondary">Resize Image</button>
</form>
{{end}}
{{define "foot"}}
{{end}}

View file

@ -0,0 +1,22 @@
{{define "title"}}
Atridad Lahiji // Tools
{{end}}
{{define "headercontent"}}
Atridad Lahiji // Tools
{{end}}
{{define "head"}}
<link rel="stylesheet" href="/public/css/styles.tools.css" />
{{end}}
{{define "main"}}
<section class="flex flex-row flex-wrap gap-2 justify-center align-middle">
{{range .Tools}}
{{template "cardlinks" .}}
{{end}}
</section>
{{end}}
{{define "foot"}}
{{end}}

31
pages/tools.go Normal file
View file

@ -0,0 +1,31 @@
package pages
import (
"atri.dad/lib"
"github.com/labstack/echo/v4"
)
type ToolsProps struct {
Tools []lib.CardLink
}
func Tools(c echo.Context) error {
tools := []lib.CardLink{
{
Name: "Image Resizer",
Description: "Image Resizer Tool",
Href: "/tools/resize",
Internal: true,
},
}
props := ToolsProps{
Tools: tools,
}
// Specify the partials used by this page
partials := []string{"header", "navitems", "cardlinks"}
// Render the template
return lib.RenderTemplate(c.Response().Writer, "base", partials, props)
}

1
public/css/styles.resize.css vendored Normal file

File diff suppressed because one or more lines are too long

1
public/css/styles.tools.css vendored Normal file

File diff suppressed because one or more lines are too long