Skip to content

Commit

Permalink
Revert "message: remove GroupImage Width & Height"
Browse files Browse the repository at this point in the history
This reverts commit 9d618e2.

handle zero width and height in another way.
  • Loading branch information
wdvxdr1123 authored and fumiama committed Jan 15, 2022
1 parent e22965e commit 7bec167
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
13 changes: 9 additions & 4 deletions client/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"bytes"
"encoding/hex"
"image"
"io"
"math/rand"
"os"
Expand Down Expand Up @@ -80,14 +81,16 @@ func (c *QQClient) UploadGroupImage(groupCode int64, img io.ReadSeeker) (*messag
}
return nil, errors.Wrap(err, "upload failed")
ok:
_, _ = img.Seek(0, io.SeekStart)
i, _, _ := image.DecodeConfig(img)
var imageType int32 = 1000
_, _ = img.Seek(0, io.SeekStart)
tmp := make([]byte, 4)
_, _ = img.Read(tmp)
if bytes.Equal(tmp, []byte{0x47, 0x49, 0x46, 0x38}) {
imageType = 2000
}
return message.NewGroupImage(binary.CalculateImageResourceId(fh), fh, rsp.FileId, int32(length), imageType), nil
return message.NewGroupImage(binary.CalculateImageResourceId(fh), fh, rsp.FileId, int32(length), int32(i.Width), int32(i.Height), imageType), nil
}

func (c *QQClient) UploadGroupImageByFile(groupCode int64, path string) (*message.GroupImageElement, error) {
Expand Down Expand Up @@ -131,14 +134,16 @@ func (c *QQClient) UploadGroupImageByFile(groupCode int64, path string) (*messag
}
return nil, errors.Wrap(err, "upload failed")
ok:
_, _ = img.Seek(0, io.SeekStart)
i, _, _ := image.DecodeConfig(img)
var imageType int32 = 1000
_, _ = img.Seek(0, io.SeekStart)
tmp := make([]byte, 4)
_, _ = img.Read(tmp)
if bytes.Equal(tmp, []byte{0x47, 0x49, 0x46, 0x38}) {
imageType = 2000
}
return message.NewGroupImage(binary.CalculateImageResourceId(fh), fh, rsp.FileId, int32(length), imageType), nil
return message.NewGroupImage(binary.CalculateImageResourceId(fh), fh, rsp.FileId, int32(length), int32(i.Width), int32(i.Height), imageType), nil
}

func (c *QQClient) UploadPrivateImage(target int64, img io.ReadSeeker) (*message.FriendImageElement, error) {
Expand Down Expand Up @@ -186,7 +191,7 @@ func (c *QQClient) ImageOcr(img interface{}) (*OcrResponse, error) {
}
_ = b.Close()
}
rsp, err := c.sendAndWait(c.buildImageOcrRequestPacket(url, strings.ToUpper(hex.EncodeToString(e.Md5)), e.Size, 480, 720))
rsp, err := c.sendAndWait(c.buildImageOcrRequestPacket(url, strings.ToUpper(hex.EncodeToString(e.Md5)), e.Size, e.Width, e.Height))
if err != nil {
return nil, err
}
Expand All @@ -205,7 +210,7 @@ func (c *QQClient) QueryGroupImage(groupCode int64, hash []byte, size int32) (*m
return nil, errors.New(rsp.Message)
}
if rsp.IsExists {
return message.NewGroupImage(binary.CalculateImageResourceId(hash), hash, rsp.FileId, size, 1000), nil
return message.NewGroupImage(binary.CalculateImageResourceId(hash), hash, rsp.FileId, size, rsp.Width, rsp.Height, 1000), nil
}
return nil, errors.New("image does not exist")
}
Expand Down
18 changes: 15 additions & 3 deletions message/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type GroupImageElement struct {
ImageType int32
ImageBizType ImageBizType
Size int32
Width int32
Height int32
Md5 []byte
Url string

Expand Down Expand Up @@ -62,13 +64,15 @@ const (

/* ------ Implementations ------ */

func NewGroupImage(id string, md5 []byte, fid int64, size, imageType int32) *GroupImageElement {
func NewGroupImage(id string, md5 []byte, fid int64, size, width, height, imageType int32) *GroupImageElement {
return &GroupImageElement{
ImageId: id,
FileId: fid,
Md5: md5,
Size: size,
ImageType: imageType,
Width: width,
Height: height,
Url: "https://gchat.qpic.cn/gchatpic_new/1/0-0-" + strings.ReplaceAll(binary.CalculateImageResourceId(md5)[1:37], "-", "") + "/0?term=2",
}
}
Expand All @@ -86,13 +90,21 @@ func (e *GuildImageElement) Type() ElementType {
}

func (e *GroupImageElement) Pack() (r []*msg.Elem) {
// width and height are required, set 720*480 if not set
if e.Width == 0 {
e.Width = 720
}
if e.Height == 0 {
e.Height = 480
}

cface := &msg.CustomFace{
FileType: proto.Int32(66),
Useful: proto.Int32(1),
// Origin: 1,
BizType: proto.Int32(5),
Width: proto.Int32(720),
Height: proto.Int32(480),
Width: &e.Width,
Height: &e.Height,
FileId: proto.Int32(int32(e.FileId)),
FilePath: &e.ImageId,
ImageType: &e.ImageType,
Expand Down
4 changes: 4 additions & 0 deletions message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ func ParseMessageElems(elems []*msg.Elem) []IMessageElement {
FileId: int64(elem.CustomFace.GetFileId()),
ImageId: elem.CustomFace.GetFilePath(),
Size: elem.CustomFace.GetSize(),
Width: elem.CustomFace.GetWidth(),
Height: elem.CustomFace.GetHeight(),
Url: url,
ImageBizType: func() ImageBizType {
if len(elem.CustomFace.PbReserve) == 0 {
Expand Down Expand Up @@ -559,6 +561,8 @@ func ParseMessageElems(elems []*msg.Elem) []IMessageElement {
FileId: int64(flash.FlashTroopPic.GetFileId()),
ImageId: flash.FlashTroopPic.GetFilePath(),
Size: flash.FlashTroopPic.GetSize(),
Width: flash.FlashTroopPic.GetWidth(),
Height: flash.FlashTroopPic.GetHeight(),
Md5: flash.FlashTroopPic.Md5,
Flash: true,
})
Expand Down

0 comments on commit 7bec167

Please sign in to comment.