Skip to content

Commit

Permalink
update 9Image mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Sora233 committed Jun 21, 2021
1 parent ec7f306 commit d6a6912
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 8 deletions.
59 changes: 51 additions & 8 deletions lsp/bilibili/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Sora233/DDBOT/proxy_pool"
localutils "github.com/Sora233/DDBOT/utils"
"strings"
"sync"
)

type NewsInfo struct {
Expand Down Expand Up @@ -300,17 +301,38 @@ func (notify *ConcernNewsNotify) ToMessage() []message.IMessageElement {
continue
}
result = append(result, localutils.MessageTextf("%v\n", origin.GetItem().GetDescription()))
for _, pic := range origin.GetItem().GetPictures() {
var isNorm = false
if pic.GetImgHeight() > 1200 && pic.GetImgWidth() > 1200 {
isNorm = true
var skip = false
if len(origin.GetItem().GetPictures()) == 9 {
var urls = make([]string, 9)
for _, pic := range origin.GetItem().GetPictures() {
urls = append(urls, pic.GetImgSrc())
}
groupImage, err := localutils.UploadGroupImageByUrl(notify.GroupCode, pic.GetImgSrc(), isNorm, proxy_pool.PreferNone)
resultByte, err := urlsTo9ImageMode(urls)
if err != nil {
log.WithField("pic", pic).Errorf("upload group image %v", err)
continue
log.Errorf("urlsTo9ImageMode failed %v", err)
} else {
groupImage, err := localutils.UploadGroupImage(notify.GroupCode, resultByte, false)
if err != nil {
log.Errorf("upload 9Image group image %v", err)
} else {
result = append(result, groupImage)
skip = true
}
}
}
if !skip {
for _, pic := range origin.GetItem().GetPictures() {
var isNorm = false
if pic.GetImgHeight() > 1200 && pic.GetImgWidth() > 1200 {
isNorm = true
}
groupImage, err := localutils.UploadGroupImageByUrl(notify.GroupCode, pic.GetImgSrc(), isNorm, proxy_pool.PreferNone)
if err != nil {
log.WithField("pic", pic).Errorf("upload group image %v", err)
continue
}
result = append(result, groupImage)
}
result = append(result, groupImage)
}
case DynamicDescType_TextOnly:
result = append(result, localutils.MessageTextf("%v转发了%v的动态:\n%v\n%v\n\n原动态:\n", notify.Name, originName, date, cardOrigin.GetItem().GetContent()))
Expand Down Expand Up @@ -714,3 +736,24 @@ func (notify *ConcernLiveNotify) GetGroupCode() int64 {
func (notify *ConcernLiveNotify) GetUid() interface{} {
return notify.Mid
}

func urlsTo9ImageMode(urls []string) ([]byte, error) {
if len(urls) != 9 {
return nil, errors.New("the number of image must be 9")
}
var imgBytes = make([][]byte, 9)
var errs = make([]error, 9)
var wg sync.WaitGroup
for index, url := range urls {
wg.Add(1)
go func(index int, pic string) {
defer wg.Done()
imgBytes[index], errs[index] = localutils.ImageGet(pic, proxy_pool.PreferNone)
}(index, url)
}
wg.Wait()
for _, e := range errs {
return nil, e
}
return localutils.Merge9Images(imgBytes)
}
41 changes: 41 additions & 0 deletions utils/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,44 @@ func ImageSuffix(name string) bool {
}
return false
}

func Merge9Images(images [][]byte) ([]byte, error) {
const TotalSize = 1044
const subImageSize = TotalSize / 3
if len(images) != 9 {
return nil, errors.New("the number of images must be exactly 9")
}
var bg = image.NewRGBA(image.Rect(0, 0, TotalSize, TotalSize))
for i, imgBytes := range images {
cfg, _, err := image.DecodeConfig(bytes.NewReader(imgBytes))
if err != nil {
return nil, fmt.Errorf("DecodeConfig failed %v", err)
}
minSize := cfg.Width
if minSize > cfg.Height {
minSize = cfg.Height
}
img, _, err := image.Decode(bytes.NewReader(imgBytes))
if err != nil {
return nil, fmt.Errorf("Decode failed %v", err)
}
img = resize.Resize(subImageSize, subImageSize, SubImage(img, image.Rect(0, 0, minSize, minSize)), resize.Lanczos3)
si := i / 3
sj := i % 3
fmt.Printf("draw %v %v %v\n", sj*subImageSize, si*subImageSize, img.Bounds())
draw.Draw(bg, img.Bounds().Add(image.Point{X: sj * subImageSize, Y: si * subImageSize}), img, image.Point{}, draw.Src)
}
var result = new(bytes.Buffer)
err := jpeg.Encode(result, bg, &jpeg.Options{Quality: 100})
if err != nil {
return nil, err
}
return result.Bytes(), nil
}

func SubImage(img image.Image, r image.Rectangle) image.Image {
var bgSize = image.Rect(0, 0, r.Dx(), r.Dy())
var bg = image.NewRGBA(bgSize)
draw.Draw(bg, bg.Bounds(), img, r.Min, draw.Src)
return bg
}

0 comments on commit d6a6912

Please sign in to comment.