将图像 blob 转换为 base64。

huangapple go评论110阅读模式
英文:

Image blob to base64

问题

我正在尝试从数据库中读取一个 blob,并将其作为 base64 图像显示在浏览器中(我认为这是唯一的显示方式)。

我的代码如下:

rows, err := database.Conn.Query("SELECT a.id, a.name, a.creationdata, b.logo, c.name FROM guilds a, cloaka_guilds b, players c WHERE a.ownerid = c.id AND b.guild_id = a.id ORDER BY creationdata DESC")
if err != nil {
    return nil, err
}
guild_list := []*Guild{}
for rows.Next() {
    guild := &Guild{}
    rows.Scan(&guild.Id, &guild.Name, &guild.Creationdata, &guild.Logo, &guild.Owner)
    guild.Logo_str = base64.StdEncoding.EncodeToString(guild.Logo)
    guild_list = append(guild_list, guild)
}
return guild_list, nil

b.logo 是一个图像的 []byte。我对此非常确定。为了更加确定,IntelliJ IDEA 在数据库行上显示:

88x88 Image 1.68K PNG

在浏览器中,我使用以下方式进行渲染:

<img src="data:image/jpeg;base64,[[ $element.Logo ]]">

但是显然有些问题,因为我得到了这个:

<img src="data:image/jpeg;base64,[137%2080%2078%2071%2013%2010%2026%20.........]">

至于结构体 guild.Logo 是 []byte 类型。

英文:

Im trying to read a blob from database and show it to browser as a base64 image (I think its the only way to show it)

My code is the following

    rows, err := database.Conn.Query(&quot;SELECT a.id, a.name, a.creationdata, b.logo, c.name FROM guilds a, cloaka_guilds b, players c WHERE a.ownerid = c.id AND b.guild_id = a.id ORDER BY creationdata DESC&quot;)
	if err != nil {
		return nil, err
	}
	guild_list := []*Guild{}
	for rows.Next() {
		guild := &amp;Guild{}
		rows.Scan(&amp;guild.Id, &amp;guild.Name, &amp;guild.Creationdata, &amp;guild.Logo, &amp;guild.Owner)
		guild.Logo_str = base64.StdEncoding.EncodeToString(guild.Logo)
		guild_list = append(guild_list, guild)
	}
	return guild_list, nil

b.logo holds an image []byte. Im sure of that. To be even more sure IntelliJ IDEA shows on the database row

> 88x88 Image 1.68K PNG

On the browser im rendering with something like

&lt;img src=&quot;data:image/jpeg;base64,[[ $element.Logo ]]&quot;&gt;

But something is clearly wrong since I get this

&lt;img src=&quot;data:image/jpeg;base64,[137%2080%2078%2071%2013%2010%2026%20.........]&quot;&gt;

As for the struct guild.Logo is []byte type

答案1

得分: 1

将图像呈现为base64格式时,不需要使用[[符号。这是一个示例:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQV....

并且这是它的显示效果

查看从数据库获取的数据。它应该是一个byteArray或者可以转换为byteArray的东西。有了byteArray,你可以使用以下代码对其进行base64编码:

sEnc := b64.StdEncoding.EncodeToString(byteArr)

请查看我的kbd,了解我是如何得到上述的base64图像的。

英文:

To present image as a base64 you do not need [[ symbols. Here is an example:

&lt;img src=&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQV....

And here is how it will look.

Take a look at the data you get from your database. It should be a byteArray or something that you can convert to byteArray. Having a byteArray you can base64 encode it with:

sEnc := b64.StdEncoding.EncodeToString(byteArr)

Take a look at my <kbd>Go playground</kbd> to see how I got the abovementioned base64 image.

huangapple
  • 本文由 发表于 2015年12月4日 10:38:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/34080015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定