Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created October 6, 2022 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/394bf004aebbd83e9aa509521c1f5474 to your computer and use it in GitHub Desktop.
Save parzibyte/394bf004aebbd83e9aa509521c1f5474 to your computer and use it in GitHub Desktop.
/*
Recibe una cadena en base64 y devuelve un image.Image
Si la cadena inicia con algo como data:image/png;base64, entonces será removido
*/
func convertirBase64AImagen(imagenCodificadaEnBase64 string) (image.Image, error) {
marca := ";base64,"
indice := strings.Index(imagenCodificadaEnBase64, marca)
if indice != -1 {
imagenCodificadaEnBase64 = imagenCodificadaEnBase64[indice+len(marca):]
}
decodificado, err := base64.StdEncoding.DecodeString(imagenCodificadaEnBase64)
if err != nil {
return nil, err
}
imagen, _, err := image.Decode(bytes.NewReader(decodificado))
return imagen, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment