From bd530da3dfae657b7f62f9a65c42187f2965a2dc Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Sat, 14 Nov 2020 22:58:23 +0800 Subject: [PATCH 1/2] fix group gif --- client/client.go | 8 ++++++-- message/elements.go | 32 +++++++++++++++++--------------- message/pack.go | 14 ++++++++------ 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/client/client.go b/client/client.go index 841354f5..fc86b7d5 100644 --- a/client/client.go +++ b/client/client.go @@ -644,7 +644,11 @@ func (c *QQClient) UploadGroupImage(groupCode int64, img []byte) (*message.Group } return nil, errors.New("upload failed") ok: - return message.NewGroupImage(binary.CalculateImageResourceId(h[:]), h[:], rsp.FileId, int32(len(img)), rsp.Width, rsp.Height), nil + var imageType int32 = 1000 + if bytes.HasPrefix(img, []byte{0x47, 0x49, 0x46, 0x38}) { + imageType = 2000 + } + return message.NewGroupImage(binary.CalculateImageResourceId(h[:]), h[:], rsp.FileId, int32(len(img)), rsp.Width, rsp.Height, imageType), nil } func (c *QQClient) UploadPrivateImage(target int64, img []byte) (*message.FriendImageElement, error) { @@ -699,7 +703,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, rsp.Width, rsp.Height), nil + return message.NewGroupImage(binary.CalculateImageResourceId(hash), hash, rsp.FileId, size, rsp.Width, rsp.Height, 1000), nil } return nil, errors.New("image not exists") } diff --git a/message/elements.go b/message/elements.go index 2666c1af..41588050 100644 --- a/message/elements.go +++ b/message/elements.go @@ -23,13 +23,14 @@ type ImageElement struct { } type GroupImageElement struct { - ImageId string - FileId int64 - Size int32 - Width int32 - Height int32 - Md5 []byte - Url string + ImageId string + FileId int64 + ImageType int32 + Size int32 + Width int32 + Height int32 + Md5 []byte + Url string } type VoiceElement struct { @@ -143,15 +144,16 @@ func NewImage(data []byte) *ImageElement { } } -func NewGroupImage(id string, md5 []byte, fid int64, size, width, height 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, - Width: width, - Height: height, - Url: "http://gchat.qpic.cn/gchatpic_new/1/0-0-" + strings.ReplaceAll(binary.CalculateImageResourceId(md5)[1:37], "-", "") + "/0?term=2", + ImageId: id, + FileId: fid, + Md5: md5, + Size: size, + ImageType: imageType, + Width: width, + Height: height, + Url: "http://gchat.qpic.cn/gchatpic_new/1/0-0-" + strings.ReplaceAll(binary.CalculateImageResourceId(md5)[1:37], "-", "") + "/0?term=2", } } diff --git a/message/pack.go b/message/pack.go index 4ff91bf1..6c6fbd42 100644 --- a/message/pack.go +++ b/message/pack.go @@ -75,12 +75,14 @@ func (e *GroupImageElement) Pack() (r []*msg.Elem) { CustomFace: &msg.CustomFace{ FileType: 66, Useful: 1, - Origin: 1, - FileId: int32(e.FileId), - FilePath: e.ImageId, - Size: e.Size, - Md5: e.Md5[:], - Flag: make([]byte, 4), + //Origin: 1, + BizType: 5, + FileId: int32(e.FileId), + FilePath: e.ImageId, + ImageType: e.ImageType, + Size: e.Size, + Md5: e.Md5[:], + Flag: make([]byte, 4), //OldData: imgOld, }, }) From f65eda3c2039770a592b568b6f9d4e47e4d7753b Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Sat, 14 Nov 2020 23:51:12 +0800 Subject: [PATCH 2/2] fix height width --- client/client.go | 4 +++- message/pack.go | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/client/client.go b/client/client.go index b1e33c4b..a864eb80 100644 --- a/client/client.go +++ b/client/client.go @@ -7,6 +7,7 @@ import ( "encoding/json" "errors" "fmt" + "image" "io" "math" "math/rand" @@ -645,11 +646,12 @@ func (c *QQClient) UploadGroupImage(groupCode int64, img []byte) (*message.Group } return nil, errors.New("upload failed") ok: + i, _, _ := image.DecodeConfig(bytes.NewReader(img)) var imageType int32 = 1000 if bytes.HasPrefix(img, []byte{0x47, 0x49, 0x46, 0x38}) { imageType = 2000 } - return message.NewGroupImage(binary.CalculateImageResourceId(h[:]), h[:], rsp.FileId, int32(len(img)), rsp.Width, rsp.Height, imageType), nil + return message.NewGroupImage(binary.CalculateImageResourceId(h[:]), h[:], rsp.FileId, int32(len(img)), int32(i.Width), int32(i.Height), imageType), nil } func (c *QQClient) UploadPrivateImage(target int64, img []byte) (*message.FriendImageElement, error) { diff --git a/message/pack.go b/message/pack.go index 6c6fbd42..51e8db0b 100644 --- a/message/pack.go +++ b/message/pack.go @@ -77,6 +77,8 @@ func (e *GroupImageElement) Pack() (r []*msg.Elem) { Useful: 1, //Origin: 1, BizType: 5, + Width: e.Width, + Height: e.Height, FileId: int32(e.FileId), FilePath: e.ImageId, ImageType: e.ImageType,