Skip to content

Commit

Permalink
fix: support base64 encoded image_url (songquanpeng#872)
Browse files Browse the repository at this point in the history
- Add support for base64 encoded image in OpenAI's image_url

Co-authored-by: JustSong <39998050+songquanpeng@users.noreply.github.com>
  • Loading branch information
Laisky and songquanpeng authored Jan 1, 2024
1 parent c50c609 commit 7772064
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions common/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
_ "golang.org/x/image/webp"
)

// Regex to match data URL pattern
var dataURLPattern = regexp.MustCompile(`data:image/([^;]+);base64,(.*)`)

func IsImageUrl(url string) (bool, error) {
resp, err := http.Head(url)
if err != nil {
Expand Down Expand Up @@ -44,9 +47,6 @@ func GetImageSizeFromUrl(url string) (width int, height int, err error) {
}

func GetImageFromUrl(url string) (mimeType string, data string, err error) {
// Regex to match data URL pattern
dataURLPattern := regexp.MustCompile(`data:image/([^;]+);base64,(.*)`)

// Check if the URL is a data URL
matches := dataURLPattern.FindStringSubmatch(url)
if len(matches) == 3 {
Expand Down

0 comments on commit 7772064

Please sign in to comment.