Go-whatsapp: how can i create thumbnail image

Created on 2 Jun 2020  Â·  10Comments  Â·  Source: Rhymen/go-whatsapp

I would like to know a simple solution to create a thumbnail to insert in the Thumbnail: field of the whatsapp.ImageMessage method. any solution!

 msg := whatsapp.ImageMessage{
 Info: whatsapp.MessageInfo{
       RemoteJid: numero_destino+"@s.whatsapp.net",
 },
         Type:    "image/jpeg",
         Caption: "Hello Gopher!",
         Content: img,
         Thumbnail: ??? ,
 }
 id_wpp,err := wac.Send(msg)

what image format does he need?
I tried base64 but it asks for a byte, but I don't know how to generate this format. can you help me?

All 10 comments

https://github.com/Rhymen/go-whatsapp/blob/master/binary/proto/def.proto#L119 = bytes jpegThumbnail ?

r := bytes.NewReader(byteData)

I use

import "github.com/disintegration/imaging"

func getThumbnail(input io.Reader) ([]byte, error) {

    img, _, err := image.Decode(input)
    if err != nil {
        l.Println("getThumbnail 1", err)
        return nil, err
    }

    b := img.Bounds()
    imgWidth := b.Max.X
    imgHeight := b.Max.Y

    thumbWidth := 100
    thumbHeight := 100

    if imgWidth > imgHeight {
        thumbHeight = 56
    } else {
        thumbWidth = 56
    }

    thumb := imaging.Thumbnail(img, thumbWidth, thumbHeight, imaging.CatmullRom)

    buf := new(bytes.Buffer)
    err = jpeg.Encode(buf, thumb, nil)
    if err != nil {
        l.Println("getThumbnail 2", err)
        return nil, err
    }

    return buf.Bytes(), nil
}

It wotks for me

How can we implement this for the mp4 video?

On Fri, Jun 5, 2020 at 7:49 AM ramacatur notifications@github.com wrote:

I use

import "github.com/disintegration/imaging"

func getThumbnail(input io.Reader) ([]byte, error) {

img, _, err := image.Decode(input)
if err != nil {
l.Println("getThumbnail 1", err)
return nil, err
}

b := img.Bounds()
imgWidth := b.Max.X
imgHeight := b.Max.Y

thumbWidth := 100
thumbHeight := 100

if imgWidth > imgHeight {
thumbHeight = 56
} else {
thumbWidth = 56
}

thumb := imaging.Thumbnail(img, thumbWidth, thumbHeight, imaging.CatmullRom)

buf := new(bytes.Buffer)
err = jpeg.Encode(buf, thumb, nil)
if err != nil {
l.Println("getThumbnail 2", err)
return nil, err
}

return buf.Bytes(), nil
}

It wotks for me

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/Rhymen/go-whatsapp/issues/383#issuecomment-639256651,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ABDLT25YTJWXTZGN7YCKE33RVB2OJANCNFSM4NQJG7SQ
.

How can we implement this for the mp4 video?
…
On Fri, Jun 5, 2020 at 7:49 AM ramacatur @.*> wrote: I use import "github.com/disintegration/imaging" func getThumbnail(input io.Reader) ([]byte, error) { img, _, err := image.Decode(input) if err != nil { l.Println("getThumbnail 1", err) return nil, err } b := img.Bounds() imgWidth := b.Max.X imgHeight := b.Max.Y thumbWidth := 100 thumbHeight := 100 if imgWidth > imgHeight { thumbHeight = 56 } else { thumbWidth = 56 } thumb := imaging.Thumbnail(img, thumbWidth, thumbHeight, imaging.CatmullRom) buf := new(bytes.Buffer) err = jpeg.Encode(buf, thumb, nil) if err != nil { l.Println("getThumbnail 2", err) return nil, err } return buf.Bytes(), nil } It wotks for me — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#383 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABDLT25YTJWXTZGN7YCKE33RVB2OJANCNFSM4NQJG7SQ .

I'm still looking for references

How can we implement this for the mp4 video?
…
On Fri, Jun 5, 2020 at 7:49 AM ramacatur _@_.*> wrote: I use import "github.com/disintegration/imaging" func getThumbnail(input io.Reader) ([]byte, error) { img, _, err := image.Decode(input) if err != nil { l.Println("getThumbnail 1", err) return nil, err } b := img.Bounds() imgWidth := b.Max.X imgHeight := b.Max.Y thumbWidth := 100 thumbHeight := 100 if imgWidth > imgHeight { thumbHeight = 56 } else { thumbWidth = 56 } thumb := imaging.Thumbnail(img, thumbWidth, thumbHeight, imaging.CatmullRom) buf := new(bytes.Buffer) err = jpeg.Encode(buf, thumb, nil) if err != nil { l.Println("getThumbnail 2", err) return nil, err } return buf.Bytes(), nil } It wotks for me — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#383 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABDLT25YTJWXTZGN7YCKE33RVB2OJANCNFSM4NQJG7SQ .

I'm still looking for references

@ramacatur I'm trying to get the participant properties (I want to know whether a participant is an admin or not). Could you help me? https://github.com/Rhymen/go-whatsapp/issues/384 Thank you.

I use

import "github.com/disintegration/imaging"

func getThumbnail(input io.Reader) ([]byte, error) {

  img, _, err := image.Decode(input)
  if err != nil {
      l.Println("getThumbnail 1", err)
      return nil, err
  }

  b := img.Bounds()
  imgWidth := b.Max.X
  imgHeight := b.Max.Y

  thumbWidth := 100
  thumbHeight := 100

  if imgWidth > imgHeight {
      thumbHeight = 56
  } else {
      thumbWidth = 56
  }

  thumb := imaging.Thumbnail(img, thumbWidth, thumbHeight, imaging.CatmullRom)

  buf := new(bytes.Buffer)
  err = jpeg.Encode(buf, thumb, nil)
  if err != nil {
      l.Println("getThumbnail 2", err)
      return nil, err
  }

  return buf.Bytes(), nil
}

It wotks for me

how do i implement? I'm new to go and I don't know how to implement it!

I use

import "github.com/disintegration/imaging"

func getThumbnail(input io.Reader) ([]byte, error) {

    img, _, err := image.Decode(input)
    if err != nil {
        l.Println("getThumbnail 1", err)
        return nil, err
    }

    b := img.Bounds()
    imgWidth := b.Max.X
    imgHeight := b.Max.Y

    thumbWidth := 100
    thumbHeight := 100

    if imgWidth > imgHeight {
        thumbHeight = 56
    } else {
        thumbWidth = 56
    }

    thumb := imaging.Thumbnail(img, thumbWidth, thumbHeight, imaging.CatmullRom)

    buf := new(bytes.Buffer)
    err = jpeg.Encode(buf, thumb, nil)
    if err != nil {
        l.Println("getThumbnail 2", err)
        return nil, err
    }

    return buf.Bytes(), nil
}

It wotks for me

how do i implement? I'm new to go and I don't know how to implement it!

Call this function and set to Thumbnail

I use

import "github.com/disintegration/imaging"

func getThumbnail(input io.Reader) ([]byte, error) {

  img, _, err := image.Decode(input)
  if err != nil {
      l.Println("getThumbnail 1", err)
      return nil, err
  }

  b := img.Bounds()
  imgWidth := b.Max.X
  imgHeight := b.Max.Y

  thumbWidth := 100
  thumbHeight := 100

  if imgWidth > imgHeight {
      thumbHeight = 56
  } else {
      thumbWidth = 56
  }

  thumb := imaging.Thumbnail(img, thumbWidth, thumbHeight, imaging.CatmullRom)

  buf := new(bytes.Buffer)
  err = jpeg.Encode(buf, thumb, nil)
  if err != nil {
      l.Println("getThumbnail 2", err)
      return nil, err
  }

  return buf.Bytes(), nil
}

It wotks for me

how do i implement? I'm new to go and I don't know how to implement it!

Call this function and set to Thumbnail

img, err := os.Open("/home/whatsapp/www/image.jpg")
thumb, err := getThumbnail(img)

                            msg := whatsapp.ImageMessage{
                                Info: whatsapp.MessageInfo{
                                    RemoteJid: numero_destino+"@s.whatsapp.net",
                                },
                                Type:    "image/jpeg",
                                Caption: "Hello Gopher!",
                                Content: img,
                                Thumbnail: thumb,
                            }

                            id_wpp,err := wac.Send(msg)

I'm doing so but without success, the following error appears on the console:
image.Decode undefined (type io.Reader has no field or method Decode)

I use

import "github.com/disintegration/imaging"

func getThumbnail(input io.Reader) ([]byte, error) {

    img, _, err := image.Decode(input)
    if err != nil {
        l.Println("getThumbnail 1", err)
        return nil, err
    }

    b := img.Bounds()
    imgWidth := b.Max.X
    imgHeight := b.Max.Y

    thumbWidth := 100
    thumbHeight := 100

    if imgWidth > imgHeight {
        thumbHeight = 56
    } else {
        thumbWidth = 56
    }

    thumb := imaging.Thumbnail(img, thumbWidth, thumbHeight, imaging.CatmullRom)

    buf := new(bytes.Buffer)
    err = jpeg.Encode(buf, thumb, nil)
    if err != nil {
        l.Println("getThumbnail 2", err)
        return nil, err
    }

    return buf.Bytes(), nil
}

It wotks for me

how do i implement? I'm new to go and I don't know how to implement it!

Call this function and set to Thumbnail

img, err := os.Open("/home/whatsapp/www/image.jpg")
thumb, err := getThumbnail(img)

                            msg := whatsapp.ImageMessage{
                                Info: whatsapp.MessageInfo{
                                    RemoteJid: numero_destino+"@s.whatsapp.net",
                                },
                                Type:    "image/jpeg",
                                Caption: "Hello Gopher!",
                                Content: img,
                                Thumbnail: thumb,
                            }

                            id_wpp,err := wac.Send(msg)

I'm doing so but without success, the following error appears on the console:
image.Decode undefined (type io.Reader has no field or method Decode)

Use import "image"

Check https://golang.org/pkg/image/

And don't forget open image twice.

img, err := os.Open("/home/whatsapp/www/image.jpg")

img for Content

And

img2, err := os.Open("/home/whatsapp/www/image.jpg")

For getThumbnail

I use

import "github.com/disintegration/imaging"

func getThumbnail(input io.Reader) ([]byte, error) {

  img, _, err := image.Decode(input)
  if err != nil {
      l.Println("getThumbnail 1", err)
      return nil, err
  }

  b := img.Bounds()
  imgWidth := b.Max.X
  imgHeight := b.Max.Y

  thumbWidth := 100
  thumbHeight := 100

  if imgWidth > imgHeight {
      thumbHeight = 56
  } else {
      thumbWidth = 56
  }

  thumb := imaging.Thumbnail(img, thumbWidth, thumbHeight, imaging.CatmullRom)

  buf := new(bytes.Buffer)
  err = jpeg.Encode(buf, thumb, nil)
  if err != nil {
      l.Println("getThumbnail 2", err)
      return nil, err
  }

  return buf.Bytes(), nil
}

It wotks for me

how do i implement? I'm new to go and I don't know how to implement it!

Call this function and set to Thumbnail

img, err := os.Open("/home/whatsapp/www/image.jpg")
thumb, err := getThumbnail(img)

                            msg := whatsapp.ImageMessage{
                                Info: whatsapp.MessageInfo{
                                    RemoteJid: numero_destino+"@s.whatsapp.net",
                                },
                                Type:    "image/jpeg",
                                Caption: "Hello Gopher!",
                                Content: img,
                                Thumbnail: thumb,
                            }

                            id_wpp,err := wac.Send(msg)

I'm doing so but without success, the following error appears on the console:
image.Decode undefined (type io.Reader has no field or method Decode)

Use import "image"

Check https://golang.org/pkg/image/

And don't forget open image twice.

img, err := os.Open("/home/whatsapp/www/image.jpg")

img for Content

And

img2, err := os.Open("/home/whatsapp/www/image.jpg")

For getThumbnail

jewel, it worked, thank you very much. the problem was that i was not loading the thumb correctly. Perfect.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Leonardo-Crespo picture Leonardo-Crespo  Â·  6Comments

it-sumanp picture it-sumanp  Â·  8Comments

ribeiroferreiralucas picture ribeiroferreiralucas  Â·  8Comments

0xhex picture 0xhex  Â·  9Comments

anton-rs picture anton-rs  Â·  10Comments