52 lines
981 B
Go
Raw Normal View History

2017-06-12 18:06:31 +00:00
package main
import (
_ "image/gif"
_ "image/jpeg"
_ "image/png"
2017-06-12 22:06:35 +00:00
"strings"
2017-06-12 18:06:31 +00:00
2017-06-12 22:06:35 +00:00
"github.com/animenotifier/arn"
"github.com/fatih/color"
"github.com/nfnt/resize"
2017-06-12 18:06:31 +00:00
)
2017-06-12 22:06:35 +00:00
func makeWebPAvatar(user *arn.User) {
baseName := findOriginalAvatar(user)
2017-06-12 18:06:31 +00:00
2017-06-12 22:06:35 +00:00
if baseName == "" {
return
2017-06-12 18:06:31 +00:00
}
2017-06-12 22:06:35 +00:00
original := avatarsDirectoryOriginal + baseName
outFile := avatarsDirectoryWebP + user.ID + ".webp"
2017-06-12 18:06:31 +00:00
2017-06-12 22:06:35 +00:00
err := avatarToWebP(original, outFile, 80)
2017-06-12 18:06:31 +00:00
2017-06-12 22:06:35 +00:00
if err != nil {
color.Red(user.Nick + " [WebP]")
} else {
color.Green(user.Nick + " [WebP]")
}
}
2017-06-12 18:06:31 +00:00
2017-06-12 22:06:35 +00:00
func avatarToWebP(in string, out string, quality float32) error {
img, _, loadErr := arn.LoadImage(in)
2017-06-12 18:06:31 +00:00
2017-06-12 22:06:35 +00:00
if loadErr != nil {
return loadErr
2017-06-12 18:06:31 +00:00
}
2017-06-12 22:06:35 +00:00
// Big avatar
saveErr := arn.SaveWebP(img, out, quality)
if saveErr != nil {
return saveErr
}
2017-06-12 18:06:31 +00:00
2017-06-12 22:06:35 +00:00
// Small avatar
smallImg := resize.Resize(arn.AvatarSmallSize, 0, img, resize.Lanczos3)
saveErr = arn.SaveWebP(smallImg, strings.Replace(out, ".webp", ".small.webp", 1), quality)
return saveErr
2017-06-12 18:06:31 +00:00
}