Giter VIP home page Giter VIP logo

go-webpbin's Introduction

WebP Encoder/Decoder for Golang

WebP Encoder/Decoder for Golang based on official libwebp distribution

Install

go get -u github.com/nickalie/go-webpbin

Available env

All env can be override with option functions.

Name Default Desscription
SKIP_DOWNLOAD false Download webp bin automatically. Since there is no precompiled file for alpine, THE SKIP_DOWNLOAD MUST BE true AND ASSIGN A SOURCE FOR RUN.
VENDOR_PATH .bin/webp When there is no lib within and SKIP_DOWNLAOD is not true, it'll be downloaded.
LIBWEBP_VERSION 1.2.0 The latest version for now. (2021/07/16)

Example of usage

package main

import (
	"image"
	"image/color"
	"log"
	"os"
	"github.com/nickalie/go-webpbin"
)

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 := webpbin.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 := webpbin.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 := webpbin.NewDWebP().
		InputFile("image.webp").
		OutputFile("image.png").
		Run()

libwebp distribution

Under the hood library uses official libwebp distribution, so if you're going to use it on not supported platform (arm or alpine), you need to build libwebp from sources and set SKIP_DOWNLOAD=true.

Snippet to build libweb on alpine:

apk add --no-cache --update libpng-dev libjpeg-turbo-dev giflib-dev tiff-dev autoconf automake make gcc g++ wget

wget https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-0.6.0.tar.gz && \
tar -xvzf libwebp-0.6.0.tar.gz && \
mv libwebp-0.6.0 libwebp && \
rm libwebp-0.6.0.tar.gz && \
cd /libwebp && \
./configure && \
make && \
make install && \
rm -rf libwebp

go-webpbin's People

Contributors

ainsleyclark avatar calvin-huang avatar nickalie avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.