|
|
@ -16,7 +16,6 @@ import (
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/fogleman/gg"
|
|
|
|
"github.com/fogleman/gg"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
|
|
|
"github.com/nfnt/resize"
|
|
|
|
"github.com/nfnt/resize"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
|
|
|
@ -45,6 +44,23 @@ type memeText struct {
|
|
|
|
Caps bool `json:"c"`
|
|
|
|
Caps bool `json:"c"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type specification struct {
|
|
|
|
|
|
|
|
ImageURL string
|
|
|
|
|
|
|
|
StampURL string
|
|
|
|
|
|
|
|
Configs []memeText
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s specification) toJSON() string {
|
|
|
|
|
|
|
|
out, _ := json.Marshal(s)
|
|
|
|
|
|
|
|
return string(out)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func SpecFromJSON(input []byte) (specification, error) {
|
|
|
|
|
|
|
|
out := specification{}
|
|
|
|
|
|
|
|
err := json.Unmarshal(input, &out)
|
|
|
|
|
|
|
|
return out, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var horizon = 24 * 7
|
|
|
|
var horizon = 24 * 7
|
|
|
|
|
|
|
|
|
|
|
|
type cachedImages map[string]*cachedImage
|
|
|
|
type cachedImages map[string]*cachedImage
|
|
|
@ -96,13 +112,13 @@ func (p *MemePlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, a
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *MemePlugin) bully(c bot.Connector, format, id string) (image.Image, string) {
|
|
|
|
func (p *MemePlugin) stamp(c bot.Connector, format, id string) string {
|
|
|
|
bullyIcon := ""
|
|
|
|
iconURL := ""
|
|
|
|
|
|
|
|
|
|
|
|
for _, bully := range p.c.GetArray("meme.bully", []string{}) {
|
|
|
|
for _, bully := range p.c.GetArray("meme.bully", []string{}) {
|
|
|
|
if format == bully {
|
|
|
|
if format == bully {
|
|
|
|
if u, err := c.Profile(bully); err == nil {
|
|
|
|
if u, err := c.Profile(bully); err == nil {
|
|
|
|
bullyIcon = u.Icon
|
|
|
|
iconURL = u.Icon
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
log.Debug().Err(err).Msgf("could not get profile for %s", format)
|
|
|
|
log.Debug().Err(err).Msgf("could not get profile for %s", format)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -113,22 +129,11 @@ func (p *MemePlugin) bully(c bot.Connector, format, id string) (image.Image, str
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if u, err := c.Profile(id); bullyIcon == "" && err == nil {
|
|
|
|
if u, err := c.Profile(id); iconURL == "" && err == nil {
|
|
|
|
if u.IconImg != nil {
|
|
|
|
iconURL = u.Icon
|
|
|
|
return u.IconImg, format
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bullyIcon = u.Icon
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
u, err := url.Parse(bullyIcon)
|
|
|
|
return iconURL
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Error().Err(err).Msg("error with bully URL")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bullyImg, err := DownloadTemplate(u)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Error().Err(err).Msg("error downloading bully icon")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return bullyImg, format
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *MemePlugin) sendMeme(c bot.Connector, channel, channelName, msgID string, from *user.User, text string) {
|
|
|
|
func (p *MemePlugin) sendMeme(c bot.Connector, channel, channelName, msgID string, from *user.User, text string) {
|
|
|
@ -143,7 +148,6 @@ func (p *MemePlugin) sendMeme(c bot.Connector, channel, channelName, msgID strin
|
|
|
|
|
|
|
|
|
|
|
|
log.Debug().Strs("parts", parts).Msgf("Meme:\n%+v", text)
|
|
|
|
log.Debug().Strs("parts", parts).Msgf("Meme:\n%+v", text)
|
|
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
var config []memeText
|
|
|
|
var config []memeText
|
|
|
|
|
|
|
|
|
|
|
|
message = strings.TrimPrefix(message, "`")
|
|
|
|
message = strings.TrimPrefix(message, "`")
|
|
|
@ -186,9 +190,23 @@ func (p *MemePlugin) sendMeme(c bot.Connector, channel, channelName, msgID strin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bullyImg, format := p.bully(c, format, from.ID)
|
|
|
|
formats := p.c.GetMap("meme.memes", defaultFormats)
|
|
|
|
|
|
|
|
imgURL, ok := formats[format]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
imgURL = format
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
id, w, h, err := p.genMeme(format, bullyImg, config)
|
|
|
|
stampURL := p.stamp(c, format, from.ID)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
spec := specification{
|
|
|
|
|
|
|
|
ImageURL: imgURL,
|
|
|
|
|
|
|
|
StampURL: stampURL,
|
|
|
|
|
|
|
|
Configs: config,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
encodedSpec, _ := json.Marshal(spec)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w, h, err := p.checkMeme(imgURL)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("Hey %v, I couldn't download that image you asked for.", from.Name)
|
|
|
|
msg := fmt.Sprintf("Hey %v, I couldn't download that image you asked for.", from.Name)
|
|
|
|
p.bot.Send(c, bot.Ephemeral, channel, from.ID, msg)
|
|
|
|
p.bot.Send(c, bot.Ephemeral, channel, from.ID, msg)
|
|
|
@ -196,7 +214,10 @@ func (p *MemePlugin) sendMeme(c bot.Connector, channel, channelName, msgID strin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
baseURL := p.c.Get("BaseURL", ``)
|
|
|
|
baseURL := p.c.Get("BaseURL", ``)
|
|
|
|
u, _ := url.Parse(baseURL)
|
|
|
|
u, _ := url.Parse(baseURL)
|
|
|
|
u.Path = path.Join(u.Path, "meme", "img", id)
|
|
|
|
u.Path = path.Join(u.Path, "meme", "img")
|
|
|
|
|
|
|
|
q := u.Query()
|
|
|
|
|
|
|
|
q.Add("spec", string(encodedSpec))
|
|
|
|
|
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
|
|
|
|
log.Debug().Msgf("image is at %s", u.String())
|
|
|
|
log.Debug().Msgf("image is at %s", u.String())
|
|
|
|
_, err = p.bot.Send(c, bot.Message, channel, "", bot.ImageAttachment{
|
|
|
|
_, err = p.bot.Send(c, bot.Message, channel, "", bot.ImageAttachment{
|
|
|
@ -224,7 +245,6 @@ func (p *MemePlugin) sendMeme(c bot.Connector, channel, channelName, msgID strin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p.bot.Receive(c, bot.Message, m)
|
|
|
|
p.bot.Receive(c, bot.Message, m)
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -274,13 +294,13 @@ func DownloadTemplate(u *url.URL) (image.Image, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var defaultFormats = map[string]string{
|
|
|
|
var defaultFormats = map[string]string{
|
|
|
|
"fry": "Futurama-Fry.jpg",
|
|
|
|
"fry": "https://imgflip.com/s/meme/Futurama-Fry.jpg",
|
|
|
|
"aliens": "Ancient-Aliens.jpg",
|
|
|
|
"aliens": "https://imgflip.com/s/meme/Ancient-Aliens.jpg",
|
|
|
|
"doge": "Doge.jpg",
|
|
|
|
"doge": "https://imgflip.com/s/meme/Doge.jpg",
|
|
|
|
"simply": "One-Does-Not-Simply.jpg",
|
|
|
|
"simply": "https://imgflip.com/s/meme/One-Does-Not-Simply.jpg",
|
|
|
|
"wonka": "Creepy-Condescending-Wonka.jpg",
|
|
|
|
"wonka": "https://imgflip.com/s/meme/Creepy-Condescending-Wonka.jpg",
|
|
|
|
"grumpy": "Grumpy-Cat.jpg",
|
|
|
|
"grumpy": "https://imgflip.com/s/meme/Grumpy-Cat.jpg",
|
|
|
|
"raptor": "Philosoraptor.jpg",
|
|
|
|
"raptor": "https://imgflip.com/s/meme/Philosoraptor.jpg",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *MemePlugin) findFontSize(config []memeText, w, h int, sizes []float64) float64 {
|
|
|
|
func (p *MemePlugin) findFontSize(config []memeText, w, h int, sizes []float64) float64 {
|
|
|
@ -333,23 +353,31 @@ func defaultFormatConfigJSON() string {
|
|
|
|
return string(c)
|
|
|
|
return string(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *MemePlugin) genMeme(meme string, bully image.Image, config []memeText) (string, int, int, error) {
|
|
|
|
func (p *MemePlugin) checkMeme(imgURL string) (int, int, error) {
|
|
|
|
fontSizes := []float64{48, 36, 24, 16, 12}
|
|
|
|
u, err := url.Parse(imgURL)
|
|
|
|
formats := p.c.GetMap("meme.memes", defaultFormats)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
path := uuid.New().String()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
imgName, ok := formats[meme]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
imgName = meme
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u, err := url.Parse(imgName)
|
|
|
|
|
|
|
|
if err != nil || u.Scheme == "" {
|
|
|
|
if err != nil || u.Scheme == "" {
|
|
|
|
log.Debug().Err(err).Str("imgName", imgName).Msgf("url not detected")
|
|
|
|
log.Debug().Err(err).Str("imgName", imgURL).Msgf("url not detected")
|
|
|
|
if u, err = url.Parse("https://imgflip.com/s/meme/" + imgName); err != nil {
|
|
|
|
return 0, 0, fmt.Errorf("URL not valid (%s): %w", imgURL, err)
|
|
|
|
return "", 0, 0, err
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
img, err := DownloadTemplate(u)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return img.Bounds().Dx(), img.Bounds().Dy(), err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (p *MemePlugin) genMeme(spec specification) ([]byte, error) {
|
|
|
|
|
|
|
|
fontSizes := []float64{48, 36, 24, 16, 12}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
jsonSpec := spec.toJSON()
|
|
|
|
|
|
|
|
if cached, ok := p.images[jsonSpec]; ok {
|
|
|
|
|
|
|
|
log.Debug().Msgf("Returning cached image for %s", jsonSpec)
|
|
|
|
|
|
|
|
return cached.repr, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u, err := url.Parse(spec.ImageURL)
|
|
|
|
|
|
|
|
if err != nil || u.Scheme == "" {
|
|
|
|
|
|
|
|
log.Debug().Err(err).Str("imgName", spec.ImageURL).Msgf("url not detected")
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("Error with meme URL: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Debug().Msgf("Attempting to download url: %s", u.String())
|
|
|
|
log.Debug().Msgf("Attempting to download url: %s", u.String())
|
|
|
@ -357,15 +385,13 @@ func (p *MemePlugin) genMeme(meme string, bully image.Image, config []memeText)
|
|
|
|
img, err := DownloadTemplate(u)
|
|
|
|
img, err := DownloadTemplate(u)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Msgf("failed to download image: %s", err)
|
|
|
|
log.Debug().Msgf("failed to download image: %s", err)
|
|
|
|
return "", 0, 0, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
r := img.Bounds()
|
|
|
|
r := img.Bounds()
|
|
|
|
w := r.Dx()
|
|
|
|
w := r.Dx()
|
|
|
|
h := r.Dy()
|
|
|
|
h := r.Dy()
|
|
|
|
|
|
|
|
|
|
|
|
// /meme2 5guys [{"x": 0.1, "y": 0.1, "t": "test"}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
maxSz := p.c.GetFloat64("maxImgSz", 750.0)
|
|
|
|
maxSz := p.c.GetFloat64("maxImgSz", 750.0)
|
|
|
|
|
|
|
|
|
|
|
|
if w > h {
|
|
|
|
if w > h {
|
|
|
@ -385,18 +411,24 @@ func (p *MemePlugin) genMeme(meme string, bully image.Image, config []memeText)
|
|
|
|
h = r.Dy()
|
|
|
|
h = r.Dy()
|
|
|
|
log.Debug().Msgf("resized to %v, %v", w, h)
|
|
|
|
log.Debug().Msgf("resized to %v, %v", w, h)
|
|
|
|
|
|
|
|
|
|
|
|
if bully != nil {
|
|
|
|
if spec.StampURL != "" {
|
|
|
|
img = p.applyBully(img, bully)
|
|
|
|
img, err = p.applyStamp(img, spec.StampURL)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Error().
|
|
|
|
|
|
|
|
Err(err).
|
|
|
|
|
|
|
|
Interface("spec", spec).
|
|
|
|
|
|
|
|
Msg("could not apply stamp")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m := gg.NewContext(w, h)
|
|
|
|
m := gg.NewContext(w, h)
|
|
|
|
m.DrawImage(img, 0, 0)
|
|
|
|
m.DrawImage(img, 0, 0)
|
|
|
|
fontLocation := p.c.Get("meme.font", "impact.ttf")
|
|
|
|
fontLocation := p.c.Get("meme.font", "impact.ttf")
|
|
|
|
m.LoadFontFace(fontLocation, p.findFontSize(config, w, h, fontSizes))
|
|
|
|
m.LoadFontFace(fontLocation, p.findFontSize(spec.Configs, w, h, fontSizes))
|
|
|
|
|
|
|
|
|
|
|
|
for i, c := range config {
|
|
|
|
for i, c := range spec.Configs {
|
|
|
|
if c.Caps {
|
|
|
|
if c.Caps {
|
|
|
|
config[i].Text = strings.ToUpper(c.Text)
|
|
|
|
spec.Configs[i].Text = strings.ToUpper(c.Text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -409,7 +441,7 @@ func (p *MemePlugin) genMeme(meme string, bully image.Image, config []memeText)
|
|
|
|
if dx*dx+dy*dy >= strokeSize*strokeSize {
|
|
|
|
if dx*dx+dy*dy >= strokeSize*strokeSize {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, c := range config {
|
|
|
|
for _, c := range spec.Configs {
|
|
|
|
x := float64(w)*c.XPerc + float64(dx)
|
|
|
|
x := float64(w)*c.XPerc + float64(dx)
|
|
|
|
y := float64(h)*c.YPerc + float64(dy)
|
|
|
|
y := float64(h)*c.YPerc + float64(dy)
|
|
|
|
m.DrawStringAnchored(c.Text, x, y, 0.5, 0.5)
|
|
|
|
m.DrawStringAnchored(c.Text, x, y, 0.5, 0.5)
|
|
|
@ -419,7 +451,7 @@ func (p *MemePlugin) genMeme(meme string, bully image.Image, config []memeText)
|
|
|
|
|
|
|
|
|
|
|
|
// Apply white fill
|
|
|
|
// Apply white fill
|
|
|
|
m.SetHexColor("#FFF")
|
|
|
|
m.SetHexColor("#FFF")
|
|
|
|
for _, c := range config {
|
|
|
|
for _, c := range spec.Configs {
|
|
|
|
x := float64(w) * c.XPerc
|
|
|
|
x := float64(w) * c.XPerc
|
|
|
|
y := float64(h) * c.YPerc
|
|
|
|
y := float64(h) * c.YPerc
|
|
|
|
m.DrawStringAnchored(c.Text, x, y, 0.5, 0.5)
|
|
|
|
m.DrawStringAnchored(c.Text, x, y, 0.5, 0.5)
|
|
|
@ -427,18 +459,23 @@ func (p *MemePlugin) genMeme(meme string, bully image.Image, config []memeText)
|
|
|
|
|
|
|
|
|
|
|
|
i := bytes.Buffer{}
|
|
|
|
i := bytes.Buffer{}
|
|
|
|
png.Encode(&i, m.Image())
|
|
|
|
png.Encode(&i, m.Image())
|
|
|
|
p.images[path] = &cachedImage{time.Now(), i.Bytes()}
|
|
|
|
p.images[jsonSpec] = &cachedImage{time.Now(), i.Bytes()}
|
|
|
|
|
|
|
|
|
|
|
|
log.Debug().Msgf("Saved to %s\n", path)
|
|
|
|
log.Debug().Msgf("Saved to %s\n", jsonSpec)
|
|
|
|
|
|
|
|
|
|
|
|
return path, w, h, nil
|
|
|
|
return p.images[jsonSpec].repr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *MemePlugin) applyBully(img, bullyImg image.Image) image.Image {
|
|
|
|
func (p *MemePlugin) applyStamp(img image.Image, bullyURL string) (image.Image, error) {
|
|
|
|
|
|
|
|
u, _ := url.Parse(bullyURL)
|
|
|
|
|
|
|
|
bullyImg, err := DownloadTemplate(u)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
dst := image.NewRGBA(img.Bounds())
|
|
|
|
dst := image.NewRGBA(img.Bounds())
|
|
|
|
|
|
|
|
|
|
|
|
scaleFactor := p.c.GetFloat64("meme.bullyScale", 0.1)
|
|
|
|
scaleFactor := p.c.GetFloat64("meme.stampScale", 0.1)
|
|
|
|
position := p.c.GetString("meme.bullyPosition", "botright")
|
|
|
|
position := p.c.GetString("meme.stampPosition", "botright")
|
|
|
|
|
|
|
|
|
|
|
|
scaleFactor = float64(img.Bounds().Max.X) * scaleFactor / float64(bullyImg.Bounds().Max.X)
|
|
|
|
scaleFactor = float64(img.Bounds().Max.X) * scaleFactor / float64(bullyImg.Bounds().Max.X)
|
|
|
|
|
|
|
|
|
|
|
@ -464,7 +501,7 @@ func (p *MemePlugin) applyBully(img, bullyImg image.Image) image.Image {
|
|
|
|
rect := image.Rect(pt.X, pt.Y, srcSz.X, srcSz.Y)
|
|
|
|
rect := image.Rect(pt.X, pt.Y, srcSz.X, srcSz.Y)
|
|
|
|
|
|
|
|
|
|
|
|
draw.DrawMask(dst, rect, bullyImg, image.Point{}, &circle{image.Point{w / 2, h / 2}, w / 2}, image.Point{}, draw.Over)
|
|
|
|
draw.DrawMask(dst, rect, bullyImg, image.Point{}, &circle{image.Point{w / 2, h / 2}, w / 2}, image.Point{}, draw.Over)
|
|
|
|
return dst
|
|
|
|
return dst, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// the following is ripped off of https://blog.golang.org/image-draw
|
|
|
|
// the following is ripped off of https://blog.golang.org/image-draw
|
|
|
|