imgflipgo

package module
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2023 License: MIT Imports: 9 Imported by: 0

README

imgflipgo

Unit Tests CodeQL Go Report Card

(Golang) Go bindings for the Imgflip API.

Usage

To include this library in your Go project using modules, add the following import and run go mod tidy.

import "github.com/Kardbord/imgflipgo"

Otherwise, you can run the following command.

go get github.com/Kardbord/imgflipgo

The code is fairly self-documenting (said every developer too lazy to write real docs). There are only two API endpoints.

For a concrete example of how to use the library, check out example.go.

Documentation

Index

Constants

View Source
const CaptionMemeEndpoint = "https://api.imgflip.com/caption_image"
View Source
const DefaultMaxFontSizePx uint = 50
View Source
const GetMemesEndpoint string = "https://api.imgflip.com/get_memes"

Variables

This section is empty.

Functions

This section is empty.

Types

type CaptionRequest

type CaptionRequest struct {
	// A template ID as returned by the get_memes response. Any ID that was ever
	// returned from the get_memes response should work for this parameter. For
	// custom template uploads, the template ID can be found in the memegenerator
	// URL, e.g. https://imgflip.com/memegenerator/14859329/Charlie-Sheen-DERP.
	TemplateID string `schema:"template_id,omitempty" json:"template_id,omitempty"`

	// Username of a valid imgflip account. This is used to track where API
	// requests are coming from.
	Username string `schema:"username,omitempty" json:"username,omitempty"`

	// Password for the imgflip account.
	Password string `schema:"password,omitempty" json:"password,omitempty"`

	// Top text for the meme. Do not use this parameter if you are using the
	// boxes parameter below.
	TopText *string `schema:"text0,omitempty" json:"text0,omitempty"`

	// Bottom text for the meme. Do not use this parameter if you are using the
	// boxes parameter below.
	BottomText *string `schema:"text1,omitempty" json:"text1,omitempty"`

	// [optional] The font family to use for the text
	Font *Font `schema:"font,omitempty" json:"font,omitempty"`

	// [optional] Maximum font size in pixels. Defaults to 50px.
	MaxFontSizePx *uint `schema:"max_font_size,omitempty" json:"max_font_size,omitempty"`

	// [optional] For creating memes with more than two text boxes, or for further
	// customization. If TextBoxes is specified, TopText and BototmText will be ignored,
	// and text will not be automatically converted to uppercase, so you'll have to
	// handle capitalization yourself if you want the standard uppercase meme text.
	// The API is currently limited to 20 text boxes per image. The first TextBox in
	// the list may be left empty so that the second box will automatically be used
	// as bottom text.
	TextBoxes []TextBox `schema:"-" json:"boxes,omitempty"`
}

func (*CaptionRequest) BottomTextJSONTag

func (cr *CaptionRequest) BottomTextJSONTag() (string, error)

func (CaptionRequest) CreateHTTPFormBody

func (cr CaptionRequest) CreateHTTPFormBody() (url.Values, error)

func (*CaptionRequest) FontJSONTag

func (cr *CaptionRequest) FontJSONTag() (string, error)

func (*CaptionRequest) MaxFontSizePxJSONTag

func (cr *CaptionRequest) MaxFontSizePxJSONTag() (string, error)

func (*CaptionRequest) PasswordJSONTag

func (cr *CaptionRequest) PasswordJSONTag() (string, error)

func (*CaptionRequest) SetBottomText

func (cr *CaptionRequest) SetBottomText(bottomText string) *CaptionRequest

func (*CaptionRequest) SetFont

func (cr *CaptionRequest) SetFont(font Font) *CaptionRequest

func (*CaptionRequest) SetMaxFontSize

func (cr *CaptionRequest) SetMaxFontSize(maxFontSizePx uint) *CaptionRequest

func (*CaptionRequest) SetTopText

func (cr *CaptionRequest) SetTopText(topText string) *CaptionRequest

func (*CaptionRequest) TemplateIDJSONTag

func (cr *CaptionRequest) TemplateIDJSONTag() (string, error)

func (*CaptionRequest) TextBoxesJSONTag

func (cr *CaptionRequest) TextBoxesJSONTag() (string, error)

func (*CaptionRequest) TopTextJSONTag

func (cr *CaptionRequest) TopTextJSONTag() (string, error)

func (*CaptionRequest) UsernameJSONTag

func (cr *CaptionRequest) UsernameJSONTag() (string, error)

type CaptionResponse

type CaptionResponse struct {
	Success bool `json:"success,omitempty"`
	Data    struct {
		URL     string
		PageURL string
	} `json:"data,omitempty"`

	ErrorMsg string `json:"error_message,omitempty"`
}

func CaptionImage

func CaptionImage(req *CaptionRequest) (CaptionResponse, error)

CaptionImage wraps the caption_image endpoint. It makes a request using the provided parameters, and will return an error if something goes wrong at any point during the process. The errors can come originate in Go or be from the API, depending on where the failure occurred. This was done so that the caller does not have to check both the returned error value, AND CaptionResponse.Success. If the API returns an error, it will be reflected in both CaptionResponse.ErrorMsg and in the returned Go error.

type Font

type Font string
const (
	FontArial  Font = "arial"
	FontImpact Font = "impact"
)

type Meme

type Meme struct {
	ID     string `json:"id,omitempty"`
	Name   string `json:"name,omitempty"`
	URL    string `json:"url,omitempty"`
	Width  uint   `json:"width,omitempty"`
	Height uint   `json:"height,omitempty"`

	// BoxCount describes the number of text boxes the Meme uses.
	BoxCount uint `json:"box_count,omitempty"`
}

func GetMemes

func GetMemes() ([]Meme, error)

type MemesResponse

type MemesResponse struct {
	Success bool `json:"success,omitempty"`
	Data    struct {
		Memes []Meme `json:"memes,omitempty"`
	} `json:"data,omitempty"`
}

func GetMemesWithResponse

func GetMemesWithResponse() (*MemesResponse, error)

type TextBox

type TextBox struct {
	// Text to be displayed
	Text string `json:"text,omitempty"`

	// [optional] X coord of of the top left corner of the TextBox
	// If specified, must also specify Y, Width, Height
	X *uint `json:"x,omitempty"`

	// [optional] Y coord of of the top left corner of the TextBox
	// If specified, must also specify X, Width, Height
	Y *uint `json:"y,omitempty"`

	// [optional] width of the TextBox
	// If specified, must also specify X, Y, Height
	Width *uint `json:"width,omitempty"`

	// [optional] height of the TextBox
	// If specified, must also specify X, Y, Width
	Height *uint `json:"height,omitempty"`

	// [optional] Hex color for Text
	Color *uint `json:"color,omitempty"`

	// [optional] Hex color for Text outline
	OutlineColor *uint `json:"outline_color,omitempty"`
}

TextBox specifies parameters for a CaptionReqeust TextBox. any [optional] parameters that are not set will default to settings provided by imgflip.com/memegenerator.

func (*TextBox) ColorJSONTag

func (t *TextBox) ColorJSONTag() (string, error)

func (*TextBox) HeightJSONTag

func (t *TextBox) HeightJSONTag() (string, error)

func (*TextBox) OutlineColorTextJSONTag

func (t *TextBox) OutlineColorTextJSONTag() (string, error)

func (*TextBox) SetColor

func (t *TextBox) SetColor(color uint) *TextBox

func (*TextBox) SetHeight

func (t *TextBox) SetHeight(height uint) *TextBox

func (*TextBox) SetOutlineColor

func (t *TextBox) SetOutlineColor(outlineColor uint) *TextBox

func (*TextBox) SetWidth

func (t *TextBox) SetWidth(width uint) *TextBox

func (*TextBox) SetX

func (t *TextBox) SetX(x uint) *TextBox

func (*TextBox) SetY

func (t *TextBox) SetY(y uint) *TextBox

func (*TextBox) TextJSONTag

func (t *TextBox) TextJSONTag() (string, error)

func (*TextBox) WidthJSONTag

func (t *TextBox) WidthJSONTag() (string, error)

func (*TextBox) XJSONTag

func (t *TextBox) XJSONTag() (string, error)

func (*TextBox) YJSONTag

func (t *TextBox) YJSONTag() (string, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL