Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -196,9 +195,8 @@ func ImageToBase64(imageURL string) (string, error) {
return "", fmt.Errorf("unsupported image format: %s", ext)
}

//Check if the imageURL is a valid URL or a path to a local file
if _, err := url.ParseRequestURI(imageURL); err == nil {
// If it's a valid URL, download the image and convert it to base64
// Check if the imageURL is a web URL by looking for http(s) prefix
if strings.HasPrefix(imageURL, "http://") || strings.HasPrefix(imageURL, "https://") {
return handleImageFromURL(imageURL)
}

Expand Down
6 changes: 6 additions & 0 deletions image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ func TestImageToBase64Encoding(t *testing.T) {
want: "data:image/png;base64,",
wantErr: false,
},
{
name: "Local image full path",
imgURL: "/Users/vein/Downloads/wallpapers/PINK-FLOYD.png",
want: "data:image/png;base64,",
wantErr: false,
},
{
name: "WebP image from Google",
imgURL: "https://www.gstatic.com/webp/gallery/1.webp",
Expand Down
Loading