downsize

package module
v0.0.0-...-43531b0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2017 License: MIT Imports: 9 Imported by: 0

README

downsize

=======================================

Build Status GoDoc

Reduces an image to a specified file size.

Installation

$ go get github.com/lelenanam/downsize

Usage

import "github.com/lelenanam/downsize"

The downsize package provides a function downsize.Encode:

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

This function:

  • reduces an image's dimensions to achieve a specified file size Options.Size in bytes
  • writes result Image m to writer w with the given options
  • default parameters are used if a nil *Options is passed

Sample usage:

package main

import (
	"image"
	_ "image/gif"
	_ "image/jpeg"
	_ "image/png"
	"log"
	"os"

	"github.com/lelenanam/downsize"
)

func main() {
	file, err := os.Open("img.png")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()

	img, format, err := image.Decode(file)
	if err != nil {
		log.Fatalf("Error: %v, cannot decode file %v\n", err, file.Name())
	}

	out, err := os.Create("resized.png")
	if err != nil {
		log.Fatal(err)
	}
	defer out.Close()

	opt := &downsize.Options{Size: 1048576, Format: format}
	if err = downsize.Encode(out, img, opt); err != nil {
		log.Fatalf("Error: %v, cannot downsize image to size: %v\n", err, opt.Size)
	}
}

License

MIT License

Documentation

Index

Constants

View Source
const Accuracy = 0.05

Accuracy for calculating specified file size Options.Size for Options.Size result might be in range [Options.Size - Options.Size*Accuracy; Options.Size]

Variables

This section is empty.

Functions

func Encode

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

Encode changes size of Image m (result size<=o.Size) and writes the Image m to w with the given options. Default parameters are used if a nil *Options is passed.

Types

type Options

type Options struct {
	// Size is desired output file size in bytes
	Size int
	// Format is image format to encode
	Format string
	// JpegOptions are the options for jpeg format
	JpegOptions *jpeg.Options
	// GifOptions are the options for gif format
	GifOptions *gif.Options
}

Options are the encoding parameters.

Jump to

Keyboard shortcuts

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