webpwrap

package module
v0.0.0-...-53db685 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

README

WebP Encoder/Decoder for Golang

WebP Encoder/Decoder for Golang based on official libwebp distribution

Install

go get -u github.com/chtheiss/go-webp-wrap

Example of usage

package main

import (
	"image"
	"image/color"
	"log"
	"os"
	"github.com/chtheiss/go-webp-wrap"
)

func main() {
	const width, height = 256, 256

	// Create a colored image of the given width and height.
	img := image.NewNRGBA(image.Rect(0, 0, width, height))

	for y := 0; y < height; y++ {
		for x := 0; x < width; x++ {
			img.Set(x, y, color.NRGBA{
				R: uint8((x + y) & 255),
				G: uint8((x + y) << 1 & 255),
				B: uint8((x + y) << 2 & 255),
				A: 255,
			})
		}
	}

	f, err := os.Create("image.webp")
	if err != nil {
		log.Fatal(err)
	}

	if err := webpwrap.Encode(f, img); err != nil {
		f.Close()
		log.Fatal(err)
	}

	if err := f.Close(); err != nil {
		log.Fatal(err)
	}
}

CWebP

CWebP is a wrapper for cwebp command line tool.

Example to convert image.png to image.webp:

err := webpwrap.NewCWebP().
		Quality(80).
		InputFile("image.png").
		OutputFile("image.webp").
		Run()

DWebP

DWebP is a wrapper for dwebp command line tool.

Example to convert image.webp to image.png:

err := webpwrap.NewDWebP().
		InputFile("image.webp").
		OutputFile("image.png").
		Run()

Documentation

Overview

package webpwrap provides a Go wrapper for the WebP image compression tools. It allows for easy conversion of images to WebP format with various options including quality control, cropping, and different input/output methods.

package webpwrap provides a Go wrapper for the WebP image compression tools. It allows for easy conversion of images to WebP format with various options including quality control, cropping, and different input/output methods.

package webpwrap provides a Go wrapper for the WebP image compression tools. It allows for easy conversion of images to WebP format with various options including quality control, cropping, and different input/output methods.

package webpwrap provides a Go wrapper for the WebP image compression tools. It allows for easy conversion of images to WebP format with various options including quality control, cropping, and different input/output methods.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(r io.Reader) (image.Image, error)

Decode reads a WebP image from r and returns it as an image.Image. It is a convenience function that wraps the DWebP decoder.

Parameters:

  • r: The io.Reader containing the WebP image data

Returns:

  • image.Image: The decoded image
  • error: Any error encountered during decoding

func DecodeWithContext

func DecodeWithContext(ctx context.Context, r io.Reader) (image.Image, error)

DecodeWithContext reads a WebP image from r and returns it as an image.Image. The context can be used to cancel the operation. It is a convenience function that wraps the DWebP decoder.

Parameters:

  • ctx: The context for cancellation
  • r: The io.Reader containing the WebP image data

Returns:

  • image.Image: The decoded image
  • error: Any error encountered during decoding

func DetectUnsupportedPlatforms

func DetectUnsupportedPlatforms()

func Encode

func Encode(w io.Writer, m image.Image) error

Encode writes the Image m to w in WebP format using default settings. It is a convenience function that creates an Encoder with default quality (75). Any Image type may be encoded.

Parameters:

  • w: The io.Writer to write the encoded WebP data
  • m: The image.Image to encode

Returns:

  • error: Any error encountered during encoding

func EncodeWithContext

func EncodeWithContext(ctx context.Context, w io.Writer, m image.Image) error

EncodeWithContext writes the Image m to w in WebP format using default settings and context support. The context can be used to cancel the operation. It is a convenience function that creates an Encoder with default quality (75). Any Image type may be encoded.

Parameters:

  • ctx: The context for cancellation
  • w: The io.Writer to write the encoded WebP data
  • m: The image.Image to encode

Returns:

  • error: Any error encountered during encoding

Types

type CWebP

type CWebP struct {
	*binwrapper.BinWrapper
	// contains filtered or unexported fields
}

CWebP wraps the cwebp command-line tool for compressing images to WebP format. It supports various input formats including PNG, JPEG, TIFF, WebP, and raw Y'CbCr samples. For more information, see: https://developers.google.com/speed/webp/docs/cwebp

func NewCWebP

func NewCWebP(optionFuncs ...OptionFunc) *CWebP

NewCWebP creates a new CWebP instance with the given options. It initializes the binary wrapper and sets default values. The quality is set to -1 by default, which means the default cwebp quality will be used.

func (*CWebP) Crop

func (c *CWebP) Crop(x, y, width, height int) *CWebP

Crop sets the cropping parameters for the source image. The cropping area must be fully contained within the source rectangle. Parameters:

  • x: x-coordinate of the top-left corner
  • y: y-coordinate of the top-left corner
  • width: width of the crop area
  • height: height of the crop area

Returns the CWebP instance for method chaining.

func (*CWebP) Input

func (c *CWebP) Input(reader io.Reader) *CWebP

Input sets the reader to convert. Any previous calls to InputFile or InputImage will be ignored. Returns the CWebP instance for method chaining.

func (*CWebP) InputFile

func (c *CWebP) InputFile(file string) *CWebP

InputFile sets the input image file to convert. Any previous calls to Input or InputImage will be ignored. Returns the CWebP instance for method chaining.

func (*CWebP) InputImage

func (c *CWebP) InputImage(img image.Image) *CWebP

InputImage sets the image to convert. Any previous calls to InputFile or Input will be ignored. Returns the CWebP instance for method chaining.

func (*CWebP) Output

func (c *CWebP) Output(writer io.Writer) *CWebP

Output specifies the writer to write WebP file content. Any previous call to OutputFile will be ignored. Returns the CWebP instance for method chaining.

func (*CWebP) OutputFile

func (c *CWebP) OutputFile(file string) *CWebP

OutputFile specifies the name of the output WebP file. Any previous call to Output will be ignored. Returns the CWebP instance for method chaining.

func (*CWebP) Quality

func (c *CWebP) Quality(quality uint) *CWebP

Quality specifies the compression factor for RGB channels. The value must be between 0 and 100, where: - A small factor produces a smaller file with lower quality - A value of 100 achieves the best quality - The default is 75 Returns the CWebP instance for method chaining.

func (*CWebP) Reset

func (c *CWebP) Reset() *CWebP

Reset restores all parameters to their default values. Returns the CWebP instance for method chaining.

func (*CWebP) Run

func (c *CWebP) Run() error

Run executes the cwebp command with the specified parameters. Returns an error if the command fails or if input/output is not properly configured.

func (*CWebP) RunWithContext

func (c *CWebP) RunWithContext(ctx context.Context) error

RunWithContext executes the cwebp command with the specified parameters and context. The context can be used to cancel the operation. Returns an error if the command fails or if input/output is not properly configured.

func (*CWebP) Version

func (c *CWebP) Version() (string, error)

Version returns the version of the cwebp binary. Returns the version string and any error encountered.

type DWebP

type DWebP struct {
	*binwrapper.BinWrapper
	// contains filtered or unexported fields
}

DWebP wraps the dwebp command-line tool for decompressing WebP files into PNG format. It provides various options for input/output handling and supports both file and stream-based operations. For more information, see: https://developers.google.com/speed/webp/docs/dwebp

func NewDWebP

func NewDWebP(optionFuncs ...OptionFunc) *DWebP

NewDWebP creates a new DWebP instance with the given options. It initializes the binary wrapper and sets up the dwebp executable.

func (*DWebP) Input

func (c *DWebP) Input(reader io.Reader) *DWebP

Input sets the reader to convert. Any previous calls to InputFile will be ignored. Returns the DWebP instance for method chaining.

func (*DWebP) InputFile

func (c *DWebP) InputFile(file string) *DWebP

InputFile sets the WebP file to convert. Any previous calls to Input will be ignored. Returns the DWebP instance for method chaining.

func (*DWebP) Output

func (c *DWebP) Output(writer io.Writer) *DWebP

Output specifies the writer to write PNG file content. Any previous call to OutputFile will be ignored. Returns the DWebP instance for method chaining.

func (*DWebP) OutputFile

func (c *DWebP) OutputFile(file string) *DWebP

OutputFile specifies the name of the output PNG file. Any previous call to Output will be ignored. Returns the DWebP instance for method chaining.

func (*DWebP) Run

func (c *DWebP) Run() (image.Image, error)

Run executes the dwebp command with the specified parameters. Returns the decoded image and any error encountered during the process. If no output is specified, returns the decoded image as an image.Image. If an output is specified (file or writer), returns nil, nil.

func (*DWebP) RunWithContext

func (c *DWebP) RunWithContext(ctx context.Context) (image.Image, error)

RunWithContext executes the dwebp command with the specified parameters and context. The context can be used to cancel the operation. Returns the decoded image and any error encountered during the process. If no output is specified, returns the decoded image as an image.Image. If an output is specified (file or writer), returns nil, nil.

func (*DWebP) Version

func (c *DWebP) Version() (string, error)

Version returns the version of the dwebp binary. Returns the version string and any error encountered.

type Encoder

type Encoder struct {
	// Quality specifies the compression factor for RGB channels.
	// The value must be between 0 and 100, where:
	// - A small factor produces a smaller file with lower quality
	// - A value of 100 achieves the best quality
	// - The default is 75
	Quality uint
}

Encoder encodes image.Image into WebP format using cwebp. It provides control over the encoding quality and other parameters.

func (*Encoder) Encode

func (e *Encoder) Encode(w io.Writer, m image.Image) error

Encode writes the Image m to w in WebP format. Any Image type may be encoded.

Parameters:

  • w: The io.Writer to write the encoded WebP data
  • m: The image.Image to encode

Returns:

  • error: Any error encountered during encoding

func (*Encoder) EncodeWithContext

func (e *Encoder) EncodeWithContext(ctx context.Context, w io.Writer, m image.Image) error

EncodeWithContext writes the Image m to w in WebP format with context support. The context can be used to cancel the operation. Any Image type may be encoded.

Parameters:

  • ctx: The context for cancellation
  • w: The io.Writer to write the encoded WebP data
  • m: The image.Image to encode

Returns:

  • error: Any error encountered during encoding

type OptionFunc

type OptionFunc func(binWrapper *binwrapper.BinWrapper) error

func SetSkipDownload

func SetSkipDownload(isSkipDownload bool) OptionFunc

func SetVendorPath

func SetVendorPath(path string) OptionFunc

Jump to

Keyboard shortcuts

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