From 3aeee419c1e82dabc4f63a14a1faed348c1e81ea Mon Sep 17 00:00:00 2001 From: Azareal Date: Sun, 29 Jul 2018 00:51:24 +1000 Subject: [PATCH] Added support for re-encoding GIFs, hopefully this'll work with animated avatars. Fixed a bug where jpgs wouldn't get re-encoded. --- common/thumbnailer.go | 18 +++++++++++------- main.go | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/common/thumbnailer.go b/common/thumbnailer.go index a9d22bd9..0ae8e607 100644 --- a/common/thumbnailer.go +++ b/common/thumbnailer.go @@ -2,7 +2,7 @@ package common import ( "image" - _ "image/gif" + "image/gif" "image/jpeg" _ "image/png" "os" @@ -11,18 +11,18 @@ import ( var Thumbnailer ThumbnailerInt type ThumbnailerInt interface { - Resize(inPath string, tmpPath string, outPath string, width int) error + Resize(format string, inPath string, tmpPath string, outPath string, width int) error } type RezThumbnailer struct { } -func (thumb *RezThumbnailer) Resize(inPath string, tmpPath string, outPath string, width int) error { +func (thumb *RezThumbnailer) Resize(format string, inPath string, tmpPath string, outPath string, width int) error { // TODO: Sniff the aspect ratio of the image and calculate the dest height accordingly, bug make sure it isn't excessively high return nil } -func (thumb *RezThumbnailer) resize(inPath string, outPath string, width int, height int) error { +func (thumb *RezThumbnailer) resize(format string, inPath string, outPath string, width int, height int) error { return nil } @@ -34,7 +34,7 @@ func NewCaireThumbnailer() *CaireThumbnailer { return &CaireThumbnailer{} } -func precodeImage(inPath string, tmpPath string) error { +func precodeImage(format string, inPath string, tmpPath string) error { imageFile, err := os.Open(inPath) if err != nil { return err @@ -52,11 +52,15 @@ func precodeImage(inPath string, tmpPath string) error { } defer outFile.Close() + // TODO: Make sure animated gifs work after being encoded + if format == "gif" { + return gif.Encode(outFile, img, nil) + } return jpeg.Encode(outFile, img, nil) } -func (thumb *CaireThumbnailer) Resize(inPath string, tmpPath string, outPath string, width int) error { - err := precodeImage(inPath, tmpPath) +func (thumb *CaireThumbnailer) Resize(format string, inPath string, tmpPath string, outPath string, width int) error { + err := precodeImage(format, inPath, tmpPath) if err != nil { return err } diff --git a/main.go b/main.go index a5e48e5e..3cb3f50d 100644 --- a/main.go +++ b/main.go @@ -392,11 +392,11 @@ func main() { /*if user.RawAvatar == ".gif" { return nil }*/ - if user.RawAvatar != ".png" && user.RawAvatar != "jpg" && user.RawAvatar != "jpeg" && user.RawAvatar != "gif" { + if user.RawAvatar != ".png" && user.RawAvatar != ".jpg" && user.RawAvatar != ".jpeg" && user.RawAvatar != ".gif" { return nil } - err = common.Thumbnailer.Resize("./uploads/avatar_"+strconv.Itoa(user.ID)+user.RawAvatar, "./uploads/avatar_"+strconv.Itoa(user.ID)+"_tmp"+user.RawAvatar, "./uploads/avatar_"+strconv.Itoa(user.ID)+"_w48"+user.RawAvatar, 48) + err = common.Thumbnailer.Resize(user.RawAvatar[1:], "./uploads/avatar_"+strconv.Itoa(user.ID)+user.RawAvatar, "./uploads/avatar_"+strconv.Itoa(user.ID)+"_tmp"+user.RawAvatar, "./uploads/avatar_"+strconv.Itoa(user.ID)+"_w48"+user.RawAvatar, 48) if err != nil { return errors.WithStack(err) }