gifsicle

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2024 License: MIT Imports: 10 Imported by: 0

README

gifsicle-go

A Go wrapper around gifsicle with bundled binaries for quick and easy GIF compression with just a single go get!

gifsicle is one of the most popular GIF compression tools used by many online GIF compression websites like ezgif.com.

Table of Contents

Install

We bundle the binaries through embeds, so all you need is a go get:

go get github.com/munchpass/gifsicle-go

No need to have gifsicle pre-installed!

Usage

Compress

If you just want to quickly compress a GIF and write the result into a buffer:

	var buf bytes.Buffer
	err = gifsicle.Compress(&buf, decodedGif, &gifsicle.Options{
		Lossy:         80,
		OptimizeLevel: gifsicle.OPTIMIZE_LEVEL_THREE,
	})
	if err != nil {
		return err
	}
Gifsicle

This library also provides the Gifsicle struct which is a thin wrapper around the gifsicle CLI tool.

Here are some examples:

Compress an input GIF file and write the result to an output GIF file:

	sourceGif := path.Join("testfiles", "portrait_3mb.gif")
	outputGif := path.Join("testoutput", "portrait_3mb_output.gif")
	err = g.InputFile(sourceGif).
		OutputFile(outputGif).
		Lossy(80).
		OptimizeLevel(gifsicle.OPTIMIZE_LEVEL_THREE).
		Run()

Compress an input GIF object and write the result to an output GIF file:

	err = g.InputGif(sourceGif).
		OutputFile(outputGif).
		Lossy(80).
		OptimizeLevel(gifsicle.OPTIMIZE_LEVEL_THREE).
		Run()

Compress an input GIF object and write the result to an output buffer:

    var buf bytes.Buffer
	err = g.InputGif(sourceGif).
		Output(&buf).
		Lossy(80).
		OptimizeLevel(gifsicle.OPTIMIZE_LEVEL_THREE).
		Run()

Contributing

This library is still in its early stage, but is actively used in production. We welcome any PRs/suggestions!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compress

func Compress(w io.Writer, g *gif.GIF, o *Options) error

Shortcut function to compress GIFs quickly and easily with gifsicle.

func CompressFromReader

func CompressFromReader(w io.Writer, r io.Reader, o *Options) error

Shortcut function to compress GIFs quickly and easily with gifsicle using an io.Reader like a file or buffer.

Example:

testFilePath := path.Join("testfiles", "portrait_3mb.gif")
testFile, err := os.Open(testFilePath)
if err != nil {
	return err
}

var buf bytes.Buffer
err = gifsicle.CompressFromReader(&buf, testFile, &gifsicle.Options{
	Lossy:         200,
	OptimizeLevel: gifsicle.OPTIMIZE_LEVEL_THREE,
	NumColors:     256,
})
if err != nil {
	return err
}

Types

type Gifsicle

type Gifsicle struct {
	// contains filtered or unexported fields
}

Gifsicle wraps the gifsicle CLI tool.

func NewGifsicle

func NewGifsicle() (*Gifsicle, error)

func (*Gifsicle) Debug

func (g *Gifsicle) Debug() *Gifsicle

Turns on debug mode.

func (*Gifsicle) Input

func (g *Gifsicle) Input(reader io.Reader) *Gifsicle

Input sets reader to convert. InputFile or InputImage called before will be ignored.

func (*Gifsicle) InputFile

func (g *Gifsicle) InputFile(file string) *Gifsicle

InputFile sets image file to convert. Input or InputGif called before will be ignored.

func (*Gifsicle) InputGif

func (g *Gifsicle) InputGif(inputGif *gif.GIF) *Gifsicle

InputGif sets gif to convert. InputFile or Input called before will be ignored.

func (*Gifsicle) Lossy

func (g *Gifsicle) Lossy(lossy uint) *Gifsicle

Sets the --lossy parameter.

This parameter ranges from 0-200 (higher value -> more compression).

This is the main compression parameter used in websites like ezgif!

func (*Gifsicle) NumColors

func (g *Gifsicle) NumColors(numColors uint) *Gifsicle

Sets the --colors parameter.

This parameter ranges from 2-256 (lower value -> more compression).

func (*Gifsicle) OptimizeLevel

func (g *Gifsicle) OptimizeLevel(l OptimizeLevel) *Gifsicle

For the -O[level] or --optimize[=level] arguments.

See https://www.lcdf.org/gifsicle/man.html for more information.

func (*Gifsicle) Output

func (g *Gifsicle) Output(writer io.Writer) *Gifsicle

Output specify writer to write jpeg file content. OutputFile called before will be ignored.

func (*Gifsicle) OutputFile

func (g *Gifsicle) OutputFile(file string) *Gifsicle

OutputFile specify the name of the output jpeg file. Output called before will be ignored.

func (*Gifsicle) Reset

func (g *Gifsicle) Reset() *Gifsicle

Reset resets all parameters to default values

func (*Gifsicle) Run

func (g *Gifsicle) Run() error

func (*Gifsicle) Version

func (g *Gifsicle) Version() (string, error)

Version returns gifsicle --version

type OptimizeLevel

type OptimizeLevel string

For the -O[level] or --optimize[=level] arguments.

See https://www.lcdf.org/gifsicle/man.html for more information.

var (
	// Store only the changed portion of each image. This is the default.
	OPTIMIZE_LEVEL_ONE OptimizeLevel = "1"

	// Store only the changed portion of each image, and use transparency.
	OPTIMIZE_LEVEL_TWO OptimizeLevel = "2"

	// Tries several optimization methods (usually slower, sometimes better results).
	OPTIMIZE_LEVEL_THREE OptimizeLevel = "3"

	// Preserve empty transparent frames (they are dropped by default).
	OPTIMIZE_LEVEL_KEEP_EMPTY OptimizeLevel = "keep-empty"
)

type Options

type Options struct {
	// This parameter ranges from 0-200 (higher value -> more compression).
	//
	// This is the main compression parameter used in websites like ezgif!
	Lossy uint

	// Additional compression optimization.
	OptimizeLevel OptimizeLevel

	// Optional parameter to specify the number of colors (2-256) for the GIF
	//
	// Specifying less colors compresses the GIF more.
	// If this is 0, gifsicle will default to using the image's color map.
	NumColors uint
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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