MCPcopy Create free account
hub / github.com/astercloud/aster / NewImageFromFile

Function NewImageFromFile

pkg/media/types.go:71–101  ·  view source on GitHub ↗

NewImageFromFile 从文件创建图片

(path string)

Source from the content-addressed store, hash-verified

69
70// NewImageFromFile 从文件创建图片
71func NewImageFromFile(path string) (*Image, error) {
72 data, err := os.ReadFile(path)
73 if err != nil {
74 return nil, fmt.Errorf("failed to read image file: %w", err)
75 }
76
77 img := &Image{
78 Name: filepath.Base(path),
79 Size: int64(len(data)),
80 Data: base64.StdEncoding.EncodeToString(data),
81 }
82
83 // 根据扩展名设置 MIME 类型
84 ext := strings.ToLower(filepath.Ext(path))
85 switch ext {
86 case ".jpg", ".jpeg":
87 img.MimeType = "image/jpeg"
88 case ".png":
89 img.MimeType = "image/png"
90 case ".gif":
91 img.MimeType = "image/gif"
92 case ".webp":
93 img.MimeType = "image/webp"
94 case ".svg":
95 img.MimeType = "image/svg+xml"
96 default:
97 img.MimeType = "image/*"
98 }
99
100 return img, nil
101}
102
103// ToDataURL 转换为 Data URL
104func (i *Image) ToDataURL() string {

Callers 1

NewImageFunction · 0.85

Calls 1

ReadFileMethod · 0.45

Tested by

no test coverage detected