Go Base64 图像解码:解决“未知图像格式”错误
在 Go 中,您可以解码 Base64 图像以获得其宽度和宽度使用图像包的高度。但是,在此过程中您可能会遇到“未知图像格式”错误。
解码注意事项:
要正确解码图像,特定图像格式处理程序必须是挂号的。例如,要解码 PNG 图像,应使用以下命令导入 PNG 格式处理程序:
import _ "image/png"
或者,您可以使用格式特定的函数 png.DecodeConfig().
数据 URI 方案
您收到的 Base64 图像数据通常是数据 URI 方案的一部分,其中包括of:
- 数据方案类型:数据:
- 媒体类型:image/png
- Base64 编码的图像数据
到隔离base64编码的数据,将字符串从媒体后面的逗号中分割出来类型:
input := "data:image/png;base64,iVkhdfjdAjdfirtn=" b64data := input[strings.IndexByte(input, ',')+1:]
示例代码:
使用提供的信息,这是修改后的代码:
package main import ( "fmt" "image/png" "log" "os" "strings" "github.com/nfnt/resize" ) func main() { dataURL := os.Getenv("IMAGE_DATA_URL") if dataURL == "" { log.Fatal("Missing IMAGE_DATA_URL environment variable") } // Remove Data URI scheme prefix b64data := dataURL[strings.IndexByte(dataURL, ',')+1:] // Decode base64 string reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(b64data)) // Register PNG format handler and decode the image config _ = png.RegisterFormat("png", "png", png.PNGConfig{}) config, _, err := image.DecodeConfig(reader) if err != nil { log.Fatal(err) } // Resize the image newSize := resize.Thumbnail(0, 0, config.Width, config.Height) scaledImage := resize.Resize(uint(newSize.Width), uint(newSize.Height), reader, resize.Lanczos3) // Save the resized image outFile, err := os.Create("resized-image.png") if err != nil { log.Fatal(err) } err = png.Encode(outFile, scaledImage) if err != nil { log.Fatal(err) } fmt.Printf("Image saved to: %s", "resized-image.png") }
请注意,此示例代码还可以使用 resize 包调整图像大小,但这对于解码图像配置来说不是必需的。
以上是为什么在 Go 中解码 Base64 图像时出现'未知图像格式”错误?的详细内容。更多信息请关注PHP中文网其他相关文章!

Go的"strings"包提供了丰富的功能,使字符串操作高效且简单。1)使用strings.Contains()检查子串。2)strings.Split()可用于解析数据,但需谨慎使用以避免性能问题。3)strings.Join()适用于格式化字符串,但对小数据集,循环使用 =更有效。4)对于大字符串,使用strings.Builder构建字符串更高效。

Go语言使用"strings"包进行字符串操作。1)拼接字符串使用strings.Join函数。2)查找子串使用strings.Contains函数。3)替换字符串使用strings.Replace函数,这些函数高效且易用,适用于各种字符串处理任务。

资助bytespackageingoisesential foreffited byteSemanipulation,uperingFunctionsLikeContains,index,andReplaceForsearchingangingAndModifyingBinaryData.itenHancesperformanceNandCoderAceAnibility,MakeitiTavitalToolToolToolToolToolToolToolToolToolForhandLingBinaryData,networkProtocols,networkProtocoLss,networkProtocols,andetFilei

Go语言使用"encoding/binary"包进行二进制编码与解码。1)该包提供binary.Write和binary.Read函数,用于数据的写入和读取。2)需要注意选择正确的字节序(如BigEndian或LittleEndian)。3)数据对齐和错误处理也是关键,确保数据的正确性和性能。

1)usebybytes.joinforconcatenatinges,2)bytes.bufferforincrementalWriter,3)bytes.indexorbytes.indexorbytes.indexbyteforsearching bytes.bytes.readereforrednerncretinging.isnchunk.ss.ind.inc.softes.4)

theencoding/binarypackageingoiseforporptimizingBinaryBinaryOperationsDuetoitssupportforendiannessessandefficityDatahandling.toenhancePerformance:1)usebinary.nativeendiandiandiandiandiandiandiandian nessideendian toavoid avoidByteByteswapping.2)

Go的bytes包主要用于高效处理字节切片。1)使用bytes.Buffer可以高效进行字符串拼接,避免不必要的内存分配。2)bytes.Equal函数用于快速比较字节切片。3)bytes.Index、bytes.Split和bytes.ReplaceAll函数可用于搜索和操作字节切片,但需注意性能问题。

字节包提供了多种功能来高效处理字节切片。1)使用bytes.Contains检查字节序列。2)用bytes.Split分割字节切片。3)通过bytes.Replace替换字节序列。4)用bytes.Join连接多个字节切片。5)利用bytes.Buffer构建数据。6)结合bytes.Map进行错误处理和数据验证。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3汉化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版