Documentation
¶
Index ¶
- Variables
- func ExtractDocumentPagesContext(ctx context.Context, input string) (chan *ImageResult, error)
- func ExtractImage(i any) chan *ImageResult
- func ExtractImageFromFile(filePath string, options ...ImageExtractorOption) (chan *ImageResult, error)
- func ExtractVideoFrameContext(ctx context.Context, input string) (chan *ImageResult, error)
- func ExtractWildStringImage(i any) chan *ImageResult
- func GetImageDimensionFromFile(path string) (int, int, error)
- type ImageExtractorConfig
- type ImageExtractorOption
- type ImageResult
Constants ¶
This section is empty.
Variables ¶
View Source
var Exports = map[string]any{ "ExtractImage": ExtractImage, "ExtractImageFromFile": ExtractImageFromFile, "context": WithCtx, }
Functions ¶
func ExtractDocumentPagesContext ¶
func ExtractDocumentPagesContext(ctx context.Context, input string) (chan *ImageResult, error)
func ExtractImage ¶
func ExtractImage(i any) chan *ImageResult
ExtractImage extracts images from various input types, such as io.Reader, []byte, or string.
func ExtractImageFromFile ¶
func ExtractImageFromFile(filePath string, options ...ImageExtractorOption) (chan *ImageResult, error)
ExtractImageFromFile extract images from a file path, we can handle some video formats, PDF, and other files that may contain images.
func ExtractVideoFrameContext ¶
func ExtractVideoFrameContext(ctx context.Context, input string) (chan *ImageResult, error)
func ExtractWildStringImage ¶
func ExtractWildStringImage(i any) chan *ImageResult
ExtractWildStringImage 是一个强大的、多策略的图片提取器,它能从任意输入(字符串、字节切片等)中地毯式搜索并解析出图片。
此函数的核心设计思想是“鲁棒性优先”,它会自动尝试用多种方法识别和解码可能隐藏在各种格式中的图片数据。 函数异步执行,并返回一个通道(channel),所有找到的图片结果都会通过这个通道传出。 为了效率,内部通过SHA256哈希对图片进行去重,确保同一张图片只会被发送一次。
提取过程按以下优先级顺序执行:
- 首先,直接检测输入本身是否为完整的、原始的图片二进制数据(如PNG, JPEG文件内容)。
- 若不是,则检查输入是否为一个完整的Base64字符串,并尝试解码。
- 接着,将输入尝试作为JSON解析,并递归地在所有JSON值中寻找Base64编码的图片。
- 然后,在整个输入中扫描符合 "data:image/..." 格式的Data URI并解码。
- 之后,将输入视作 `multipart/form-data` 载荷进行解析,并对每一个部分递归调用本函数。
- 最后,如果以上所有方法都没有找到图片,则会启用最终回退策略:在文本中通过正则和魔法字节(Magic Bytes)进行启发式扫描,寻找已知的图片格式签名(如 "iVBORw0KG...", "R0lGODlh" 等)。
Example:
func main() {
// 模拟一个复杂的JSON输入,其中混合了Data URI和纯Base64字符串
complexInput := `
{
"request_id": "xyz-123",
"user_avatar": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR42mP8z8AAGwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMAA",
"attachments": [
{
"type": "file",
"data": "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
}
]
}
`
imageChannel := imageutils.ExtractWildStringImage(complexInput)
fmt.Println("Found the following images:")
for img := range imageChannel {
// img.ShortString() 会返回一个data URI格式的缩略字符串
fmt.Printf("- MIME: %s, Size: %d bytes, SHA256: %s\n", img.MIMEType, len(img.RawImage), img.Sha256())
// fmt.Println(img.ImageURL()) // 可以获取完整的Data URI
}
}
@param i any - 任何可以被转为字符串的输入,如 string, []byte 等。 @return chan *ImageResult - 一个用于接收图片结果的通道,当所有搜索完成后该通道会被关闭。
Types ¶
type ImageExtractorConfig ¶
type ImageExtractorConfig struct {
// contains filtered or unexported fields
}
type ImageExtractorOption ¶
type ImageExtractorOption func(*ImageExtractorConfig)
func WithCtx ¶
func WithCtx(ctx context.Context) ImageExtractorOption
type ImageResult ¶
func (*ImageResult) Base64 ¶
func (i *ImageResult) Base64() string
func (*ImageResult) ImageURL ¶
func (i *ImageResult) ImageURL() string
func (*ImageResult) SaveToFile ¶
func (i *ImageResult) SaveToFile() (string, error)
func (*ImageResult) Sha256 ¶
func (i *ImageResult) Sha256() string
func (*ImageResult) ShortString ¶
func (i *ImageResult) ShortString() string
func (*ImageResult) String ¶
func (i *ImageResult) String() string
Click to show internal directories.
Click to hide internal directories.