You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
925 B
43 lines
925 B
package util
|
|
|
|
import (
|
|
"image/gif"
|
|
"image/jpeg"
|
|
"image/png"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
/**
|
|
@author: sre
|
|
@date: 2022/8/21 0021
|
|
@desc: https://mp.weixin.qq.com/s/5oz0F2Fbn7Fh5edD8_zD5w
|
|
**/
|
|
|
|
// CheckImageFile 同时支持三种图片格式的真实性校验。
|
|
func CheckImageFile(path string) (string, error) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
log.Println("打开文件失败:", err.Error())
|
|
}
|
|
strings.LastIndex(path, ".")
|
|
lastIndex := strings.LastIndex(path, ".")
|
|
extend := path[lastIndex+1:]
|
|
extendUpper := strings.ToUpper(extend)
|
|
switch {
|
|
case strings.Contains(extendUpper, "JPEG") || strings.Contains(extendUpper, "JPG"):
|
|
_, err = jpeg.Decode(f)
|
|
case strings.Contains(extendUpper, "JPEG"):
|
|
_, err = png.Decode(f)
|
|
case strings.Contains(extendUpper, "GIF"):
|
|
_, err = gif.Decode(f)
|
|
}
|
|
|
|
if err != nil {
|
|
log.Println("校验文件类型失败:", err.Error())
|
|
return "", err
|
|
}
|
|
return "", nil
|
|
}
|