kike
kike

Reputation: 738

How to render/show images in HTML created on the fly with Golang and gin-gonic

I'am generating QR codes and right after I need to show them in an HTML without saving them as images.

So far I can generate them but I have problems rendering them in the HTML

I have here the golang code that generates the QR and the HTML template that try to render them

golang

import (
    "fmt"
    "github.com/gin-gonic/gin"
    qrcode "github.com/skip2/go-qrcode"
)


func renderQRExport(c *gin.Context){
    var images [][]byte
    var img []byte
    var err error
    for i := 0; i < 25; i++ {
        img, err = qrcode.Encode("https://example.org", qrcode.Medium, 256)
        images = append(images, img)
        if err != nil {
            fmt.Print(err)
        }
    }
    render(c, gin.H{
        "images":              images,
    }, "qr.html")
}

HTML Template

<!doctype html>
<html class="no-js" lang="">
<head>
  <title>QR export</title>
  {{template "imports.html"}}
</head>
<body>
  <div class="margin-body-log-in-worker">
        <div class="row">
          {{range .images}}
          <div class="col-4 col-sm-4 col-md-3 col-xl-2 center">
                <img src="data:image/png;base64,{{.image}}" class="img-fluid image-dashboard" />
          </div>
          {{end}}
        </div>
      </div>
</body>
</html>

One of the things that I'am not able to do, as they say in this article: https://www.sanarias.com/blog/1214PlayingwithimagesinHTTPresponseingolang and also the Encode method itself says "To serve over HTTP, remember to send a Content-Type: image/png header."

How can I set properly the content type in the request using gingonic and how to decode properly in the HTML?

Upvotes: 3

Views: 3937

Answers (1)

kike
kike

Reputation: 738

I solved my own question thanks to this article: https://www.socketloop.com/tutorials/golang-encode-image-to-base64-example

The proble is that i needed to Encode it to String with 'imgBase64Str := base64.StdEncoding.EncodeToString(buf)'

So the final code to work is as follows:

Golang

func renderQRExport(c *gin.Context){
    var images []string
    var img []byte
    var err error
    for i := 0; i < 25; i++ {
        img, err = qrcode.Encode("https://example.org", qrcode.Medium, 256)
        img2 := base64.StdEncoding.EncodeToString(img)
        images = append(images, img2)
        if err != nil {
            fmt.Print(err)
        }
    }
    render(c, gin.H{
        "images":              images,
    }, "qr.html")
}

HTML

<!doctype html>
<html class="no-js" lang="">
<head>
  <title>QR export</title>
  {{template "imports.html"}}
</head>
<body>
  <div class="margin-body-log-in-worker">
        <div class="row">
          {{range .images}}
          <div class="col-4 col-sm-4 col-md-3 col-xl-2 center">
                <img src="data:image/png;base64,{{.}}" class="img-fluid image-dashboard" />
          </div>
          {{end}}
        </div>
      </div>
</body>
</html>

Upvotes: 6

Related Questions