Files
zmkgC/api/v1/common/coryCommon/base64ToImg.go
2025-07-07 20:11:59 +08:00

105 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package coryCommon
import (
"encoding/base64"
"errors"
"fmt"
"github.com/gogf/gf/v2/os/gfile"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strings"
"time"
)
// Base64ToImgFunc 将base64转成图片保存在本地
func Base64ToImgFunc(base64Str string, numTyoe string, cdPath string) (outputPath string, err error) {
// 获取当前时间+随机数得到文件名
currentTime := time.Now()
timestamp := currentTime.UnixNano() / int64(time.Millisecond)
randomNum := rand.Intn(1000)
uniqueFileName := fmt.Sprintf("%d_%d", timestamp, randomNum)
// 用户指定的本地文件路径
//ynr := Ynr(Portrait + "/")
ynr := Ynr(cdPath + "/")
path := ynr + uniqueFileName + ".png"
path = filepath.ToSlash(path)
// Base64编码的图像字符串
b64 := "data:image/png;"
base64Image := ""
if strings.Contains(base64Str, "base64,") { // 判断是否有【base64,】如果有就替换
base64Image = strings.Replace(base64Str, base64Str[:strings.Index(base64Str, "base64,")], b64, 1)
} else {
base64Image = b64 + "base64," + base64Str
}
// 调用函数将Base64图像保存到指定路径
err = SaveBase64ImageToFile(base64Image, path)
if err != nil {
return
} else {
if numTyoe == "1" {
outputPath = strings.Replace(path, "resource/public", "file", 1)
return
} else if numTyoe == "2" {
outputPath = strings.Replace(path, "resource/public", "wxfile", 1)
return
} else {
err = errors.New("第二参数只能为1 or 2")
return
}
}
}
func Base64ToFileFunc(base64Str string, filePath string, suffix string, numTyoe string) (outputPath string, err error) {
// 获取当前时间+随机数得到文件名
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
uniqueFileName := fmt.Sprintf("%d_%d", timestamp, rand.Intn(1000))
// 用户指定的本地文件路径,filePath路径最后必须是/
path := filepath.ToSlash(filePath + uniqueFileName + suffix)
// 调用函数将Base64图像保存到指定路径
err = SaveBase64ImageToFile(base64Str, path)
if err != nil {
return
} else {
if numTyoe == "1" {
outputPath = strings.Replace(path, "resource/public", "file", 1)
return
} else if numTyoe == "2" {
outputPath = strings.Replace(path, "resource/public", "wxfile", 1)
return
} else {
err = errors.New("第二参数只能为1 or 2")
return
}
}
}
// SaveBase64ImageToFile 将Base64编码的图像保存到指定的本地文件路径
func SaveBase64ImageToFile(base64Image string, outputPath string) error {
if len(outputPath) > 0 && outputPath[0] == '/' {
outputPath = outputPath[1:]
}
getwd, _ := os.Getwd()
outputPath = gfile.Join(getwd, outputPath)
outputPath = strings.ReplaceAll(outputPath, "\\", "/")
// 1. 解码Base64字符串
parts := strings.Split(base64Image, ",")
if len(parts) != 2 {
return errors.New("Base64字符串格式不正确")
}
data, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
return errors.New("转码错误!")
}
// 2. 将字节数组保存为图像文件
err = ioutil.WriteFile(outputPath, data, 0644)
if err != nil {
return errors.New("报错图像失败!")
}
return nil
}