diff --git a/go.mod b/go.mod index 1fa16b5..58422b5 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/alecthomas/chroma/v2 v2.14.0 github.com/swaggo/echo-swagger v1.4.1 github.com/swaggo/swag v1.16.4 + golang.org/x/image v0.21.0 ) require ( @@ -25,7 +26,6 @@ require ( github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect - golang.org/x/image v0.21.0 // indirect golang.org/x/net v0.30.0 // indirect golang.org/x/text v0.19.0 // indirect golang.org/x/time v0.7.0 // indirect @@ -34,7 +34,6 @@ require ( ) require ( - github.com/disintegration/imaging v1.6.2 github.com/gookit/color v1.5.4 github.com/gorilla/feeds v1.2.0 github.com/joho/godotenv v1.5.1 diff --git a/go.sum b/go.sum index 971d94e..f9a77c4 100644 --- a/go.sum +++ b/go.sum @@ -11,8 +11,6 @@ github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW5 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= -github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo= @@ -83,7 +81,6 @@ golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= -golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s= golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78= golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0= @@ -96,7 +93,6 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ= diff --git a/lib/img.go b/lib/img.go index 22c4c1c..cdd8806 100644 --- a/lib/img.go +++ b/lib/img.go @@ -2,38 +2,32 @@ package lib import ( "bytes" - "errors" + "fmt" "image" "image/png" - "io" "mime/multipart" - "github.com/disintegration/imaging" + "golang.org/x/image/draw" ) func ResizeImg(file multipart.File, width int, height int) ([]byte, error) { - // Read file content - fileContent, err := io.ReadAll(file) - if err != nil { - return nil, errors.New("error reading image file") - } + // Read and decode image + img, _, err := image.Decode(file) + if err != nil { + return nil, fmt.Errorf("decode error: %w", err) + } - // Decode image - img, _, err := image.Decode(bytes.NewReader(fileContent)) - if err != nil { - println(err.Error()) - return nil, errors.New("error decoding image") - } + // Create new RGBA image + dst := image.NewRGBA(image.Rect(0, 0, width, height)) - // Resize the image - resizedImg := imaging.Resize(img, width, height, imaging.Lanczos) + // Resize using high-quality interpolation + draw.CatmullRom.Scale(dst, dst.Bounds(), img, img.Bounds(), draw.Over, nil) - // Encode the resized image as PNG - buf := new(bytes.Buffer) - if err := png.Encode(buf, resizedImg); err != nil { - return nil, errors.New("error encoding image to PNG") - } + // Encode to PNG + buf := new(bytes.Buffer) + if err := png.Encode(buf, dst); err != nil { + return nil, fmt.Errorf("encode error: %w", err) + } - // Return the resized image as response - return buf.Bytes(), nil + return buf.Bytes(), nil } diff --git a/pages/templates/partials/navitems.html b/pages/templates/partials/navitems.html index 3769915..ddde84e 100644 --- a/pages/templates/partials/navitems.html +++ b/pages/templates/partials/navitems.html @@ -11,10 +11,10 @@