Skip to content

Commit

Permalink
Use path parts for image
Browse files Browse the repository at this point in the history
  • Loading branch information
matbur committed Nov 25, 2024
1 parent 20ce55c commit 5bbc92c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ require (
github.com/a-h/templ v0.2.793
github.com/go-chi/chi/v5 v5.1.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/hashicorp/go-multierror v1.1.1
github.com/kelseyhightower/envconfig v1.3.0
github.com/stretchr/testify v1.8.4
golang.org/x/image v0.0.0-20191214001246-9130b4cfad52
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/kelseyhightower/envconfig v1.3.0 h1:IvRS4f2VcIQy6j4ORGIf9145T/AsUB+oY8LyvN8BXNM=
github.com/kelseyhightower/envconfig v1.3.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
41 changes: 29 additions & 12 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"image/png"
"io"

"github.com/hashicorp/go-multierror"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
Expand All @@ -28,9 +29,22 @@ type Image struct {
}

func New(size, background, foreground, text string) (*Image, error) {
s, _ := NewSizeFromString(size)
bg, _ := NewBackgroundColorFromString(background)
fg, _ := NewForegroundColorFromString(foreground)
var err error

s, err := NewSizeFromString(size)
if err != nil {
err = multierror.Append(err, fmt.Errorf("failed to create size: %w", err))
}

bg, err := NewBackgroundColorFromString(background)
if err != nil {
err = multierror.Append(err, fmt.Errorf("failed to create background color: %w", err))
}

fg, err := NewForegroundColorFromString(foreground)
if err != nil {
err = multierror.Append(err, fmt.Errorf("failed to create foreground color: %w", err))
}

if text == "" {
text = s.String()
Expand All @@ -44,25 +58,28 @@ func New(size, background, foreground, text string) (*Image, error) {
fg: fg,
text: text,
canvas: canvas,
}, nil
}, err
}

func (img *Image) Draw(w io.Writer) error {
for y := 0; y < img.size.Height(); y++ {
for x := 0; x < img.size.Width(); x++ {
img.canvas.Set(x, y, img.bg)
}
}

img.addLabel()
img.drawBackground()
img.drawLabel()

if err := png.Encode(w, img.canvas); err != nil {
return fmt.Errorf("failed to encode png: %w", err)
}
return nil
}

func (img *Image) addLabel() {
func (img *Image) drawBackground() {
for y := 0; y < img.size.Height(); y++ {
for x := 0; x < img.size.Width(); x++ {
img.canvas.Set(x, y, img.bg)
}
}
}

func (img *Image) drawLabel() {
const scale = .95

height := int(scale * float64(min(img.size.Height(), 2*img.size.Width()/max(len(img.text), 1))))
Expand Down
22 changes: 8 additions & 14 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewServer() chi.Router {
r.Post("/post", handlePost)
r.Get("/favicon.ico", handleFavicon)
r.Get("/docs", handleDocs)
r.Get("/*", handle)
r.Get("/{size}/{bg_color}/{fg_color}", handleImage)

return r
}
Expand Down Expand Up @@ -103,23 +103,17 @@ func handlePost(w http.ResponseWriter, r *http.Request) {
templ.Handler(templates.IndexPage(params)).ServeHTTP(w, r)
}

func handle(w http.ResponseWriter, r *http.Request) {
func handleImage(w http.ResponseWriter, r *http.Request) {
begin := time.Now()

w.Header().Set("Content-Disposition", `inline; filename="image.png"`)

size, bg, fg, err := parsePath(r.URL.Path)
if err != nil {
writeJSON(w, err.Error(), http.StatusBadRequest)
return
}

size := chi.URLParam(r, "size")
bgColor := chi.URLParam(r, "bg_color")
fgColor := chi.URLParam(r, "fg_color")
text := r.URL.Query().Get("text")
if text == "" {
text = size
}

img, err := image.New(size, bg, fg, text)
w.Header().Set("Content-Disposition", `inline; filename="image.png"`)

img, err := image.New(size, bgColor, fgColor, text)
if err != nil {
writeJSON(w, err.Error(), http.StatusBadRequest)
return
Expand Down

0 comments on commit 5bbc92c

Please sign in to comment.