From c7810e9f33c99a3a4cd34de3047c22cba1147a87 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Wed, 29 Apr 2020 15:35:13 -0400 Subject: [PATCH] meme: resize images --- go.mod | 1 + go.sum | 2 ++ plugins/meme/meme.go | 83 ++++++++++++++++++++++++++++---------------- 3 files changed, 56 insertions(+), 30 deletions(-) diff --git a/go.mod b/go.mod index 1961ca4..1c046f3 100644 --- a/go.mod +++ b/go.mod @@ -35,6 +35,7 @@ require ( github.com/mattn/go-sqlite3 v1.11.0 github.com/mmcdole/gofeed v1.0.0-beta2 github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf // indirect + github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/nlopes/slack v0.6.0 github.com/olebedev/when v0.0.0-20190311101825-c3b538a97254 github.com/robertkrimen/otto v0.0.0-20180617131154-15f95af6e78d // indirect diff --git a/go.sum b/go.sum index 9d6236d..ff4476a 100644 --- a/go.sum +++ b/go.sum @@ -88,6 +88,8 @@ github.com/mmcdole/gofeed v1.0.0-beta2 h1:CjQ0ADhAwNSb08zknAkGOEYqr8zfZKfrzgk9Bx github.com/mmcdole/gofeed v1.0.0-beta2/go.mod h1:/BF9JneEL2/flujm8XHoxUcghdTV6vvb3xx/vKyChFU= github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf h1:sWGE2v+hO0Nd4yFU/S/mDBM5plIU8v/Qhfz41hkDIAI= github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf/go.mod h1:pasqhqstspkosTneA62Nc+2p9SOBBYAPbnmRRWPQ0V8= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= github.com/nlopes/slack v0.6.0 h1:jt0jxVQGhssx1Ib7naAOZEZcGdtIhTzkP0nopK0AsRA= github.com/nlopes/slack v0.6.0/go.mod h1:JzQ9m3PMAqcpeCam7UaHSuBuupz7CmpjehYMayT6YOk= github.com/olebedev/when v0.0.0-20190311101825-c3b538a97254 h1:JYoQR67E1vv1WGoeW8DkdFs7vrIEe/5wP+qJItd5tUE= diff --git a/plugins/meme/meme.go b/plugins/meme/meme.go index b4b1bd4..65c2579 100644 --- a/plugins/meme/meme.go +++ b/plugins/meme/meme.go @@ -13,6 +13,7 @@ import ( "github.com/fogleman/gg" "github.com/google/uuid" + "github.com/nfnt/resize" "github.com/rs/zerolog/log" "github.com/velour/catbase/bot" @@ -90,38 +91,40 @@ func (p *MemePlugin) registerWeb(c bot.Connector) { log.Debug().Strs("parts", parts).Msgf("Meme:\n%+v", r.PostForm.Get("text")) w.WriteHeader(200) - - top, bottom := "", message - parts = strings.Split(message, "\n") - if len(parts) > 1 { - top, bottom = parts[0], parts[1] - } - - id := p.genMeme(format, top, bottom) - baseURL := p.c.Get("BaseURL", `https://catbase.velour.ninja`) - u, _ := url.Parse(baseURL) - u.Path = path.Join(u.Path, "meme", "img", id) - - log.Debug().Msgf("image is at %s", u.String()) - p.bot.Send(c, bot.Message, channel, "", bot.ImageAttachment{ - URL: u.String(), - AltTxt: fmt.Sprintf("%s: %s", from, message), - }) w.Write(nil) - m := msg.Message{ - User: &user.User{ - ID: from, - Name: from, - Admin: false, - }, - Channel: channel, - ChannelName: channelName, - Body: message, - Command: isCmd, - Time: time.Now(), - } - p.bot.Receive(c, bot.Message, m) + go func() { + top, bottom := "", message + parts = strings.Split(message, "\n") + if len(parts) > 1 { + top, bottom = parts[0], parts[1] + } + + id := p.genMeme(format, top, bottom) + baseURL := p.c.Get("BaseURL", `https://catbase.velour.ninja`) + u, _ := url.Parse(baseURL) + u.Path = path.Join(u.Path, "meme", "img", id) + + log.Debug().Msgf("image is at %s", u.String()) + p.bot.Send(c, bot.Message, channel, "", bot.ImageAttachment{ + URL: u.String(), + AltTxt: fmt.Sprintf("%s: %s", from, message), + }) + m := msg.Message{ + User: &user.User{ + ID: from, + Name: from, + Admin: false, + }, + Channel: channel, + ChannelName: channelName, + Body: message, + Command: isCmd, + Time: time.Now(), + } + + p.bot.Receive(c, bot.Message, m) + }() }) http.HandleFunc("/meme/img/", func(w http.ResponseWriter, r *http.Request) { @@ -182,10 +185,30 @@ func (p *MemePlugin) genMeme(meme, top, bottom string) string { log.Debug().Msgf("Attempting to download url: %s", u.String()) img := DownloadTemplate(u) + r := img.Bounds() w := r.Dx() h := r.Dy() + maxSz := 750.0 + + if w > h { + scale := maxSz / float64(w) + w = int(float64(w) * scale) + h = int(float64(h) * scale) + } else { + scale := maxSz / float64(h) + w = int(float64(w) * scale) + h = int(float64(h) * scale) + } + + log.Debug().Msgf("trynig to resize to %v, %v", w, h) + img = resize.Resize(uint(w), uint(h), img, resize.Lanczos3) + r = img.Bounds() + w = r.Dx() + h = r.Dy() + log.Debug().Msgf("resized to %v, %v", w, h) + m := gg.NewContext(w, h) m.DrawImage(img, 0, 0) fontLocation := p.c.Get("meme.font", "impact.ttf")