[Go 语言社区] Golang架构底层函数图片保存-原创

时间:2022-05-04
本文章向大家介绍[Go 语言社区] Golang架构底层函数图片保存-原创,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

PS: StrBase64Data 传入的需要去除:

“data:image/png;base64,”字段
// 保存磁盘的数据的图片处理函数
func SaveFiles(StrPath string, StrBase64Data string, StrPicType string, StrPicName string) bool {
    Log_Eio.Log("Entry SaveFiles!")
    Log_Eio.Log("SaveFiles  path:" + StrPath)
    // 转换下
    StrBase64Data = strings.Replace(StrBase64Data, """, "", -1)
    // 解析
    reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(StrBase64Data))
    // 转换成png格式的图像,需要导入:_“image/png”
    m, _, _ := image.Decode(reader)
    // 输出到磁盘里:包括路径
    // 文件夹操作
    //dir, _ := os.Getwd() // 获取当前的程序路径
    StrPath = "/var/www/html/res/" + StrPath
    err := os.MkdirAll(StrPath, os.ModePerm) //生成多级目录
    if err != nil {
        Log_Eio.Log(err.Error())
        return false
    }
    Log_Eio.Log("创建文件夹" + StrPath + "/a/b/c成功")
    // 保存数据
    StrPicType = StrPath + "/" + StrPicName + "." + StrPicType
    wt, err := os.Create(StrPicType)
    if err != nil {
        Log_Eio.Log("Save Image Error!")
        return false
    }
    defer wt.Close()
    // 转换为jpeg格式的图像,这里质量为30(质量取值是1-100)
    jpeg.Encode(wt, m, &jpeg.Options{30})
    return true
}