base642img

package module
Version: v0.0.0-...-83540f3 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2020 License: MIT Imports: 10 Imported by: 0

README

imgbase64

Build Status

Convert an image to its base64 equivalent. API Documentation on go.pkgdoc.org.

Features

  • Fetch an image from an external server and convert to base64
  • Use an image on your local machine to convert to base64
  • Share a bytes.Buffer to the package to detect the mime type and convert the image to base64
  • Ability to set a failover image if desired image throws an error or does not exist.
  • Automatically converts urls with " " to %20. Note: If your target domain uses +, _, etc. for space indicators it is your responsibility to modify the url accordingly.
  • Correctly sets the base64 encoding type to the media type of the image. Example: data:image/jpeg;base64
  • Returned image is ready to be used

Installation

go get github.com/polds/imgbase64

Usage

imgbase64.SetDefaultImage("http://yourdomain.com/defaultimage.png") // Optional - If FromRemote fails it will return " "

// Fetch the image from a remote server
img := imgbase64.FromRemote("http://somedomain.com/animage.jpg")

// Use an image local to machine
img := imgbase64.FromLocal("test.png")

// Pass an image buffer to package
img := imgbase64.FromBuffer(b)

Notes

  • Please make sure your Default Image is a working image url. If your Default Image fails to load the package will panic(). This was deliberate.
  • FromLocal has several instances where it might panic(), these are deliberate.

Todo

  • Better error detection. Websites with custom 404 pages will sometimes "succeed" as being images and the package will base64 the html content of that page.
  • Ability to use local images to convert to base64
  • Cache the Default Image only once during the lifetime of the application. Presently every failed image is repulled.
  • Deprecate NewImage
  • Allow use of DefaultImage with FromBuffer and FromLocal

Change Log

October 24, 2013
  • ADD: FromLocal - Allows you to base64 encode local images
  • ADD: FromBuffer - Allows you to base64 encode from a byte.Buffer
  • CHANGE: Rename function NewImage to FromRemote, please update accordingly. Will remove NewImage in future commits.
  • ADD: Examples

This is my first package in Go. Suggestions or comments please tweet @Peter_Olds

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultImage

func DefaultImage() string

DefaultImage :Get Default Image

func FromBuffer

func FromBuffer(buf bytes.Buffer, hasHeader bool) string

FromBuffer accepts a buffer and returns a base64 encoded string.

func FromLocal

func FromLocal(fname string) (string, error)

FromLocal reads a local file and returns the base64 encoded version.

func FromRemote

func FromRemote(url string, hasHeader bool) string

FromRemote is a better named function that presently calls NewImage which will be deprecated. Function accepts an RFC compliant URL and returns a base64 encoded result.

func New

New optional factory method, should be used if one activity instance per configuration is desired

func NewImage

func NewImage(url string) string

NewImage : Begin a NewImage to fetch TODO: Deprecate NewImage

func SetDefaultImage

func SetDefaultImage(img string)

SetDefaultImage :Set Default Image

func ToBuffer

func ToBuffer(buf []byte) ([]byte, error)

ToBuffer accepts a base64 encoded string

and returns a file buffer can save to image file.

func ToLocal

func ToLocal(buf []byte, fname string) ([]byte, error)

ToLocal accepts a base64 encoded stringreads and save to local file then return save path.

Types

type Activity

type Activity struct {
}

Activity is an sample Activity that can be used as a base to create a custom activity

func (*Activity) Eval

func (a *Activity) Eval(ctx activity.Context) (done bool, err error)

Eval implements api.Activity.Eval - Logs the Message

func (*Activity) Metadata

func (a *Activity) Metadata() *activity.Metadata

Metadata returns the activity's metadata

type Input

type Input struct {
	AnInput string `md:"anInput,required"`
	AnPath  string `md:"anPath"`
}

func (*Input) FromMap

func (r *Input) FromMap(values map[string]interface{}) error

func (*Input) ToMap

func (r *Input) ToMap() map[string]interface{}

type Output

type Output struct {
	AnOutput []byte `md:"anOutput"`
}

func (*Output) FromMap

func (o *Output) FromMap(values map[string]interface{}) error

func (*Output) ToMap

func (o *Output) ToMap() map[string]interface{}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL