1 Star 1 Fork 1

xiaoyutab / xgotool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
image2mimeType.go 865 Bytes
一键复制 编辑 原始数据 按行查看 历史
package xcmd
import (
"errors"
"fmt"
"os"
"strings"
)
// 获取图片的Mime Type值,如:image/webp
//
// input 输入的图片文件
func Image2mimeType(input string) (string, error) {
if err := CheckCommand("awk", "sed", "grep", "bash", "identify"); err != nil {
return "", err
}
// 输入路径为空
if input == "" {
return "", errors.New("图片输入路径不能为空")
}
// 输入文件不存在
if _, err := os.Stat(input); err != nil {
return "", err
}
input = strings.ReplaceAll(input, " ", "\\ ")
// 命令拼接
cmds := fmt.Sprintf(`%s -verbose %s 2>&1 | %s "Mime type" | %s -F ':' '{print $2}' | %s 's/\ //'`,
GetCommand("identify"), input, GetCommand("grep"), GetCommand("awk"), GetCommand("sed"))
// 命令执行
out, err := Exec(GetCommand("bash"), "-c", cmds)
if err != nil {
return "", err
}
return out, nil
}
Go
1
https://gitee.com/xiaoyutab/xgotool.git
git@gitee.com:xiaoyutab/xgotool.git
xiaoyutab
xgotool
xgotool
v0.3.9

搜索帮助