转换 base64字符串 为图片文件

 

package test

import (
	"encoding/base64"
	"io"
	"os"
	"regexp"
	"strings"
)

// data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAD4AAAAjCAYAAADMibkBAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAFiUAABYlAUlSJPAAAAO0SURBVGhD7ZjNS1RRGMb7jiDaRYuiaFcQ0aJFmygmWrbwT2gVMYsWQYsWLYJaziJsRk3zozKLamhAlIoQog8SmShBTYjUYgwUQ5RMx3l7nvueo+Nt7nxcZ2Rq5oGXe77n/s577nvOmXVSoaqCV5qq4JWmKnilyT94alGfo89E7u4R+RLVfCqlzzLX6sF7zot0YJiuGs0vmvIylz9w69XZcZH2gyJN60Va9opMj2j5P+D11YFzmTcCOrJFpGGDyECzli8u6LOM5X+pU68uKHj9NpFmDPXyrPkE1tLjCYkFAxKMJUzeKi6hQEBCcZN1yRucAKmktyV/i7Ttg7cBfnMjnhiqFfmZb/A46+cz97OxoWjyAE8kUKPwgWAM6ZXy7/HvPSJhQtOwzMOb8K1juK+dpsFaycvjy4qH3PCJLOBTQ/oNj71QG32uNgYb6RaJnRapM9CED8PrDXhGT2r9ij5mDJZPDZofKFyJWFAC9GABlmlCOI43eCui9C1UcwnXweqNMc1yph1v09LSLGe9u08Ydht27wB2A2/vFC4vj3uUx0POhOBNPPTuMl54s75wZKt6lEYPR7CsnTTqlqBtOq3emtMfdQ0of3NJZOGX+ZFiKDu4O7jF4e0gCvE2WcTTGAOY43lOglnWS0bQTE9jbB9GP/Zv2y0ydMcMXEx5gfuO6mZL+jks0nkG2xahYA48nxbUbabcThKhn54SmezX8YquYoNTduvh1tV3zXjdQNUS0sNq6W2T5iczP6PjcJvzKScym4Dly1xbGt4sh9L33ZEueHCHett6/i9DeQTWuF1k+KHpCBV9/7by8viy3NtZ9qjupY7DADOAGcERzBjJ76Pd3JT2KRk0lSe4WfO6cgoFnxpApEeEJiCXswNK7zPC2zwmhPkmnN/H32q/MgK3yg/cvnjf9TRvE9QsebunLwU+c4rrvar9Sqpc4FrvD9y5dMCiJ/RSQjB7PqfxaspgxoOK43EYT3UPjqBbqS8sOcATMQkiuLmje25w+8fC5Cd4cSdACY7oTi83IdD1h3ExGRMZbMENDfXcvpyJYTtMzsQH7V8yucDNyWyFZdjTcoMnzd364w0cOQFCKMI9Oiryo1frrCYwOdHjOKERnl5H+/dXtK5k/8zk/sYzKTc4lZwT6a4RaUFzerznHPbmaa3j92+NYtvXF7EisCrY/vExPaKWbMkXCq4Hm/zA52dFnsCTbbtEPrebQsgNk57ncbdjP+zQ8gGmjJQf+H+oKnilqQpeaapQcJE/hAXwinS1D/AAAAAASUVORK5CYII=

func chkBs64ImgStr(str string) bool {

	r := "^data:image/(png|jpg|jpeg){1};base64,[0-9a-zA-Z+/=]+=$"
	b, err := regexp.MatchString(r, str)
	if err != nil {
		return false
	}
	return b
}

func base64Img2File(bs64 string) {
	if !chkBs64ImgStr(bs64) {
		return
	}

	i := strings.Index(bs64, ",")

	dec := base64.NewDecoder(base64.StdEncoding, strings.NewReader(bs64[i+1:]))
	fn := func() string {
		l := strings.Split(bs64, ";")
		ll := strings.Split(l[0], "/")
		return ll[1]

	}()
	bs, err := io.ReadAll(dec)
	if err != nil {
		panic(err)
	}
	os.WriteFile("t."+fn, bs, 0777)

}


package test

import (
	"testing"
)

func Test_chkBs64ImgStr(t *testing.T) {
	type args struct {
		str string
	}
	s1 := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAD4AAAAjCAYAAADMibkBAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAFiUAABYlAUlSJPAAAAO0SURBVGhD7ZjNS1RRGMb7jiDaRYuiaFcQ0aJFmygmWrbwT2gVMYsWQYsWLYJaziJsRk3zozKLamhAlIoQog8SmShBTYjUYgwUQ5RMx3l7nvueo+Nt7nxcZ2Rq5oGXe77n/s577nvOmXVSoaqCV5qq4JWmKnilyT94alGfo89E7u4R+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inS1D/AAAAAASUVORK5CYII="
	tests := []struct {
		name string
		args args
		want bool
	}{
		// TODO: Add test cases.
		{name: "1", args: args{str: s1}, want: false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := chkBs64ImgStr(tt.args.str); got != tt.want {
				t.Errorf("chkBs64ImgStr() = %v, want %v", got, tt.want)
			}
		})
	}
}

func Test_base64Img2File(t *testing.T) {
	type args struct {
		bs64 string
	}
	s1 := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAD4AAAAjCAYAAADMibkBAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAFiUAABYlAUlSJPAAAAO0SURBVGhD7ZjNS1RRGMb7jiDaRYuiaFcQ0aJFmygmWrbwT2gVMYsWQYsWLYJaziJsRk3zozKLamhAlIoQog8SmShBTYjUYgwUQ5RMx3l7nvueo+Nt7nxcZ2Rq5oGXe77n/s577nvOmXVSoaqCV5qq4JWmKnilyT94alGfo89E7u4R+RLVfCqlzzLX6sF7zot0YJiuGs0vmvIylz9w69XZcZH2gyJN60Va9opMj2j5P+D11YFzmTcCOrJFpGGDyECzli8u6LOM5X+pU68uKHj9NpFmDPXyrPkE1tLjCYkFAxKMJUzeKi6hQEBCcZN1yRucAKmktyV/i7Ttg7cBfnMjnhiqFfmZb/A46+cz97OxoWjyAE8kUKPwgWAM6ZXy7/HvPSJhQtOwzMOb8K1juK+dpsFaycvjy4qH3PCJLOBTQ/oNj71QG32uNgYb6RaJnRapM9CED8PrDXhGT2r9ij5mDJZPDZofKFyJWFAC9GABlmlCOI43eCui9C1UcwnXweqNMc1yph1v09LSLGe9u08Ydht27wB2A2/vFC4vj3uUx0POhOBNPPTuMl54s75wZKt6lEYPR7CsnTTqlqBtOq3emtMfdQ0of3NJZOGX+ZFiKDu4O7jF4e0gCvE2WcTTGAOY43lOglnWS0bQTE9jbB9GP/Zv2y0ydMcMXEx5gfuO6mZL+jks0nkG2xahYA48nxbUbabcThKhn54SmezX8YquYoNTduvh1tV3zXjdQNUS0sNq6W2T5iczP6PjcJvzKScym4Dly1xbGt4sh9L33ZEueHCHett6/i9DeQTWuF1k+KHpCBV9/7by8viy3NtZ9qjupY7DADOAGcERzBjJ76Pd3JT2KRk0lSe4WfO6cgoFnxpApEeEJiCXswNK7zPC2zwmhPkmnN/H32q/MgK3yg/cvnjf9TRvE9QsebunLwU+c4rrvar9Sqpc4FrvD9y5dMCiJ/RSQjB7PqfxaspgxoOK43EYT3UPjqBbqS8sOcATMQkiuLmje25w+8fC5Cd4cSdACY7oTi83IdD1h3ExGRMZbMENDfXcvpyJYTtMzsQH7V8yucDNyWyFZdjTcoMnzd364w0cOQFCKMI9Oiryo1frrCYwOdHjOKERnl5H+/dXtK5k/8zk/sYzKTc4lZwT6a4RaUFzerznHPbmaa3j92+NYtvXF7EisCrY/vExPaKWbMkXCq4Hm/zA52dFnsCTbbtEPrebQsgNk57ncbdjP+zQ8gGmjJQf+H+oKnilqQpeaapQcJE/hAXwinS1D/AAAAAASUVORK5CYII="

	tests := []struct {
		name string
		args args
	}{
		// TODO: Add test cases.
		{name: "1", args: args{bs64: s1}},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			base64Img2File(tt.args.bs64)
		})
	}
}

  

 

posted @ 2022-03-05 19:49  papering  阅读(151)  评论(0编辑  收藏  举报