This repository has been archived on 2024-11-28. You can view files and clone it, but cannot push or open issues or pull requests.
Files
schisandra-cloud-album/utils/generate_avatar.go
landaiqing 16616e3755 🔨 refactor code
2024-10-03 02:10:14 +08:00

52 lines
1.1 KiB
Go

package utils
import (
"net/http"
"time"
)
// 获取网络图片
var client = &http.Client{
Timeout: 5 * time.Second, // 超时时间
}
// GenerateAvatar 用于生成用户头像
func GenerateAvatar(userId string) (baseImg string) {
path := "https://api.multiavatar.com/" + userId + ".png"
//// 创建请求
//request, err := http.NewRequest("GET", path, nil)
//if err != nil {
// return "", errors.New("image request error")
//}
//
//// 发送请求并获取响应
//respImg, err := client.Do(request)
//if err != nil {
// return "", errors.New("failed to fetch image")
//}
//defer func(Body io.ReadCloser) {
// err := Body.Close()
// if err != nil {
// return
// }
//}(respImg.Body)
//
//// 读取图片数据
//imgByte, err := io.ReadAll(respImg.Body)
//if err != nil {
// return "", errors.New("failed to read image data")
//}
//
//// 判断文件类型,生成一个前缀
//mimeType := http.DetectContentType(imgByte)
//switch mimeType {
//case "image/png":
// baseImg = "data:image/png;base64," + base64.StdEncoding.EncodeToString(imgByte)
//default:
// return "", errors.New("unsupported image type")
//}
return path
}