Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ImgBB

ImgBB is an imgbb.com api client.

Installation

go get github.com/JohnNON/ImgBB

Example of usage

package main

import (
    "context"
    "crypto/md5"
    "encoding/hex"
    "fmt"
    "io"
    "log"
    "net/http"
    "os"
    "time"

    "github.com/JohnNON/ImgBB"
)

const (
    key = "your-imgBB-api-key"
)

func main() {
    f, err := os.Open("example.jpg")
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()

    b, err := io.ReadAll(f)
    if err != nil {
        log.Fatal(err)
    }

    img := &imgbb.Image{
        Name: hashSum(b),
        TTL:  60,
        File: b,
    }

    httpClient := &http.Client{
        Timeout: 5 * time.Second,
    }

    imgBBClient := imgbb.NewClient(httpClient, key)

    resp, err := imgBBClient.Upload(context.Background(), img)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("image url: %s\n", resp.Data.URL)
}

func hashSum(b []byte) string {
    sum := md5.Sum(b)

    return hex.EncodeToString(sum[:])
}

About

ImgBB is an imgbb.com api client.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages