From 4a5b7d52cd29b533935256d34cae16e25b1577f6 Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Tue, 20 Feb 2024 21:41:03 +0100 Subject: [PATCH] Use exiffix library to fix image orientation 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 https://github.com/golang/go/issues/4341 for more information and a link to https://github.com/edwvee/exiffix. --- go.mod | 1 + go.sum | 2 ++ upload_drop.go | 26 ++++++++++++++------------ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 0f9b140..16b9f15 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index ca6a64f..27f8b46 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/upload_drop.go b/upload_drop.go index c3f3678..f341c69 100644 --- a/upload_drop.go +++ b/upload_drop.go @@ -6,6 +6,7 @@ package main import ( _ "github.com/bashdrew/goheif" "github.com/disintegration/imaging" + "github.com/edwvee/exiffix" "io" "log" "net/http" @@ -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 {