Added support for re-encoding GIFs, hopefully this'll work with animated avatars.

Fixed a bug where jpgs wouldn't get re-encoded.
This commit is contained in:
Azareal 2018-07-29 00:51:24 +10:00
parent 0a628f7201
commit 3aeee419c1
2 changed files with 13 additions and 9 deletions

View File

@ -2,7 +2,7 @@ package common
import ( import (
"image" "image"
_ "image/gif" "image/gif"
"image/jpeg" "image/jpeg"
_ "image/png" _ "image/png"
"os" "os"
@ -11,18 +11,18 @@ import (
var Thumbnailer ThumbnailerInt var Thumbnailer ThumbnailerInt
type ThumbnailerInt interface { 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 { 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 // TODO: Sniff the aspect ratio of the image and calculate the dest height accordingly, bug make sure it isn't excessively high
return nil 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 return nil
} }
@ -34,7 +34,7 @@ func NewCaireThumbnailer() *CaireThumbnailer {
return &CaireThumbnailer{} return &CaireThumbnailer{}
} }
func precodeImage(inPath string, tmpPath string) error { func precodeImage(format string, inPath string, tmpPath string) error {
imageFile, err := os.Open(inPath) imageFile, err := os.Open(inPath)
if err != nil { if err != nil {
return err return err
@ -52,11 +52,15 @@ func precodeImage(inPath string, tmpPath string) error {
} }
defer outFile.Close() 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) return jpeg.Encode(outFile, img, nil)
} }
func (thumb *CaireThumbnailer) Resize(inPath string, tmpPath string, outPath string, width int) error { func (thumb *CaireThumbnailer) Resize(format string, inPath string, tmpPath string, outPath string, width int) error {
err := precodeImage(inPath, tmpPath) err := precodeImage(format, inPath, tmpPath)
if err != nil { if err != nil {
return err return err
} }

View File

@ -392,11 +392,11 @@ func main() {
/*if user.RawAvatar == ".gif" { /*if user.RawAvatar == ".gif" {
return nil 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 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 { if err != nil {
return errors.WithStack(err) return errors.WithStack(err)
} }