use a library for better image orientation handling

This commit is contained in:
2023-12-23 01:20:20 -07:00
parent f0292c325e
commit 9be49cb871
3 changed files with 12 additions and 64 deletions

View File

@ -9,7 +9,6 @@ import (
"image" "image"
"image/jpeg" "image/jpeg"
"image/png"
"io" "io"
"mime" "mime"
"mime/multipart" "mime/multipart"
@ -20,7 +19,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/nfnt/resize" "github.com/disintegration/imaging"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -149,28 +148,10 @@ func cleanUpTempFile(fileLocation string) error {
func resizeImage(fileLocation string, format string) (string, error) { func resizeImage(fileLocation string, format string) (string, error) {
// load the image // 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 var img image.Image
fileMimeType := mime.TypeByExtension(path.Ext(fileLocation)) img, err := imaging.Open(fileLocation, imaging.AutoOrientation(true))
if fileMimeType == "image/jpeg" { if err != nil {
// decode the image return "", fmt.Errorf("error opening image | %w", err)
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)
} }
// check if the image is already the correct size // check if the image is already the correct size
@ -202,47 +183,7 @@ func resizeImage(fileLocation string, format string) (string, error) {
y1 = 768 y1 = 768
} }
inFormat := "wide" croppedImage := imaging.Fill(img, x1, y1, imaging.Center, imaging.Lanczos)
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)
// save the image to a temp file // save the image to a temp file
tempFile, err := os.CreateTemp("", "i2v*"+path.Ext(fileLocation)) tempFile, err := os.CreateTemp("", "i2v*"+path.Ext(fileLocation))

2
go.mod
View File

@ -3,6 +3,7 @@ module i2v
go 1.21 go 1.21
require ( require (
github.com/disintegration/imaging v1.6.2
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/urfave/cli/v2 v2.26.0 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/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
) )

5
go.sum
View File

@ -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 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= 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 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= 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= 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/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 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= 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=