diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index 8353694..f2c9e5d 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -9,7 +9,6 @@ import ( "image" "image/jpeg" - "image/png" "io" "mime" "mime/multipart" @@ -20,7 +19,7 @@ import ( "strings" "time" - "github.com/nfnt/resize" + "github.com/disintegration/imaging" "github.com/urfave/cli/v2" ) @@ -149,28 +148,10 @@ func cleanUpTempFile(fileLocation string) error { func resizeImage(fileLocation string, format string) (string, error) { // load the image - file, err := os.Open(fileLocation) - if err != nil { - return "", fmt.Errorf("error opening file | %w", err) - } - defer file.Close() - var img image.Image - fileMimeType := mime.TypeByExtension(path.Ext(fileLocation)) - if fileMimeType == "image/jpeg" { - // decode the image - img, err = jpeg.Decode(file) - if err != nil { - return "", fmt.Errorf("error decoding jpeg image | %w", err) - } - } else if fileMimeType == "image/png" { - // decode the image - img, err = png.Decode(file) - if err != nil { - return "", fmt.Errorf("error decoding png image | %w", err) - } - } else { - return "", fmt.Errorf("unsupported file type | %v", fileMimeType) + img, err := imaging.Open(fileLocation, imaging.AutoOrientation(true)) + if err != nil { + return "", fmt.Errorf("error opening image | %w", err) } // check if the image is already the correct size @@ -202,47 +183,7 @@ func resizeImage(fileLocation string, format string) (string, error) { y1 = 768 } - inFormat := "wide" - if width < height { - inFormat = "tall" - } - if width == height { - inFormat = "square" - } - - // if not, resize the image - // scale the original image to the new size - var resizedImage image.Image - if format == "wide" { - resizedImage = resize.Resize(uint(x1), 0, img, resize.Lanczos3) - } - if format == "tall" { - resizedImage = resize.Resize(0, uint(y1), img, resize.Lanczos3) - } - if format == "square" { - if inFormat == "wide" { - resizedImage = resize.Resize(0, uint(y1), img, resize.Lanczos3) - } - if inFormat == "tall" { - resizedImage = resize.Resize(uint(x1), 0, img, resize.Lanczos3) - } - if inFormat == "square" { - resizedImage = resize.Resize(uint(x1), uint(y1), img, resize.Lanczos3) - } - } - - // crop the image to the final correct size - - // start by getting the center of the image - tempBounds := resizedImage.Bounds() - x0 := tempBounds.Max.X/2 - x1/2 - y0 := tempBounds.Max.Y/2 - y1/2 - xMax := x0 + x1 - yMax := y0 + y1 - - croppedImageRect := image.Rect(x0, y0, xMax, yMax) - rgba := convertToRGBA(resizedImage) - croppedImage := rgba.SubImage(croppedImageRect) + croppedImage := imaging.Fill(img, x1, y1, imaging.Center, imaging.Lanczos) // save the image to a temp file tempFile, err := os.CreateTemp("", "i2v*"+path.Ext(fileLocation)) diff --git a/go.mod b/go.mod index 6288509..d5ceb02 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module i2v go 1.21 require ( + github.com/disintegration/imaging v1.6.2 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/urfave/cli/v2 v2.26.0 ) @@ -11,4 +12,5 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect + golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect ) diff --git a/go.sum b/go.sum index ab4729f..e9fc155 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +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/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= @@ -8,3 +10,6 @@ github.com/urfave/cli/v2 v2.26.0 h1:3f3AMg3HpThFNT4I++TKOejZO8yU55t3JnnSr4S4QEI= github.com/urfave/cli/v2 v2.26.0/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=