Skip to content

Commit

Permalink
Use exiffix library to fix image orientation
Browse files Browse the repository at this point in the history
If you upload an unedited image from an iPhone, the EXIF data
indicates how it should be oriented. Oddmu used to resize images strip
the EXIF data, resulting in resized images that were not oriented
correctly. If the EXIF data is stripped, the image has to be rotated.
In order to do this, the exiffix single-function library is used to
work around "image/jpeg: correct for EXIF orientation? #4341", opened
in 2012. See golang/go#4341 for more
information and a link to https://github.com/edwvee/exiffix.
  • Loading branch information
kensanata committed Feb 20, 2024
1 parent c65f3ea commit 4a5b7d5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/bashdrew/goheif v0.0.0-20230406184952-7a08ca9c9bdd
github.com/charmbracelet/lipgloss v0.9.1
github.com/disintegration/imaging v1.6.2
github.com/edwvee/exiffix v0.0.0-20210922235313-0f6cbda5e58f
github.com/fsnotify/fsnotify v1.7.0
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47
github.com/google/subcommands v1.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ 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/edwvee/exiffix v0.0.0-20210922235313-0f6cbda5e58f h1:RMnUwTnNR070mFAEIoqMYjNirHj8i0h79VXTYyBCyVA=
github.com/edwvee/exiffix v0.0.0-20210922235313-0f6cbda5e58f/go.mod h1:KoE3Ti1qbQXCb3s/XGj0yApHnbnNnn1bXTtB5Auq/Vc=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gomarkdown/markdown v0.0.0-20231222211730-1d6d20845b47 h1:k4Tw0nt6lwro3Uin8eqoET7MDA4JnT8YgbCjc/g5E3k=
Expand Down
26 changes: 14 additions & 12 deletions upload_drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
_ "github.com/bashdrew/goheif"
"github.com/disintegration/imaging"
"github.com/edwvee/exiffix"
"io"
"log"
"net/http"
Expand Down Expand Up @@ -160,22 +161,23 @@ func dropHandler(w http.ResponseWriter, r *http.Request, dir string) {
}
defer dst.Close()
if mw > 0 {
img, err := imaging.Decode(file)
// do not use imaging.Decode(file, imaging.AutoOrientation(true)) because that only works for JPEG files
img, fmt, err := exiffix.Decode(file)
if err != nil {
http.Error(w, "The image could not be decoded (only PNG, JPG and HEIC formats are supported for resizing)", http.StatusBadRequest)
return
}
rect := img.Bounds()
if rect.Max.X - rect.Min.X > mw {
img = imaging.Resize(img, mw, 0, imaging.Lanczos) // preserve aspect ratio
imaging.Encode(dst, img, format, imaging.JPEGQuality(quality))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
http.Error(w, "The file is too small for this", http.StatusBadRequest)
log.Println("Decoded", fmt, "file")
res := imaging.Resize(img, mw, 0, imaging.Lanczos) // preserve aspect ratio
// imaging functions don't return errors but empty images…
if !res.Rect.Empty() {
img = res
}
// images are always reencoded, so image quality goes down
err = imaging.Encode(dst, img, format, imaging.JPEGQuality(quality))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else {
Expand Down

0 comments on commit 4a5b7d5

Please sign in to comment.