fastimage

package module
Version: v0.0.0-...-7e006a2 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2017 License: MIT Imports: 10 Imported by: 2

README

fastimage

GoDoc Build Status wercker status

by Ruben Fonseca (@rubenfonseca)

Golang implementation of fastimage. Finds the type and/or size of an image given its uri by fetching as little as needed.

How?

fastimage parses the image data as it is downloaded. As soon as it finds out the size and type of the image, it stops the download.

Install

$ go get github.com/rubenfonseca/fastimage

Usage

For instance, this is a big 10MB JPEG image on wikipedia:

url := "http://upload.wikimedia.org/wikipedia/commons/9/9a/SKA_dishes_big.jpg"

fastimage.Debug()
imagetype, size, err := fastimage.DetectImageType(url)
if err != nil {
	// Something went wrong, http failed? not an image?
	panic(err)
}

switch imagetype {
case fastimage.JPEG:
	log.Printf("JPEG")
case fastimage.PNG:
	log.Printf("PNG")
case fastimage.GIF:
	log.Printf("GIF")
}

log.Printf("Image type: %s", imagetype.String())
log.Printf("Image size: %v", size)

At the end, you can read something like this:

Closed after reading just 17863 bytes out of 10001439 bytes

If you want to set request timeout for url:

// the second argument is request timeout (milliseconds).
// FYI, DetectImageType() uses default timeout 5000ms.
imagetype, size, err := fastimage.DetectImageTypeWithTimeout(url, 2000)

Supported file types

File type Can detect type? Can detect size?
PNG Yes Yes
JPEG Yes Yes
GIF Yes Yes
BMP Yes No
TIFF Yes No

Project details

License

fastimage is under MIT license. See the LICENSE file for details.

Documentation

Overview

Package fastimage allows you to find the type and/or size of a remote image by downloading as little as possible.

Why? Sometimes you need to know the size of a remote image before downloading it.

How? fastimage parses the image data as it is downloaded. As soon as it finds out the size and type of the image, it stops the download.

Example (LocalBigJPEG)
package main

import (
	"fmt"
	"os"

	"github.com/rubenfonseca/fastimage"
)

func main() {
	f, err := os.Open("example.gif")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	imagetype, size, err := fastimage.DetectImageTypeFromReader(f)
	if err != nil {
		// Something went wrong, not an image?
		panic(err)
	}

	fmt.Printf("Image size: %v\n", size)

	switch imagetype {
	case fastimage.JPEG:
		fmt.Printf("JPEG")
	case fastimage.PNG:
		fmt.Printf("PNG")
	case fastimage.GIF:
		fmt.Printf("GIF")
	}
}
Output:

Image size: &{320 240}
GIF
Example (RemoteBigJPEG)

This example shows basic usage of the package: just pass an url to the detector, and analyze the results.

package main

import (
	"fmt"

	"github.com/rubenfonseca/fastimage"
)

func main() {
	url := "http://upload.wikimedia.org/wikipedia/commons/9/9a/SKA_dishes_big.jpg"

	imagetype, size, err := fastimage.DetectImageType(url)
	if err != nil {
		// Something went wrong, http failed? not an image?
		panic(err)
	}

	fmt.Printf("Image size: %v\n", size)

	switch imagetype {
	case fastimage.JPEG:
		fmt.Println("JPEG")
	case fastimage.PNG:
		fmt.Println("PNG")
	case fastimage.GIF:
		fmt.Println("GIF")
	}
}
Output:

Image size: &{5000 2813}
JPEG

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug()

Debug enables debug logging of the operations done by the library. If called, lots of information will be print to stderr.

func DetectImageType

func DetectImageType(uri string) (ImageType, *ImageSize, error)

DetectImageType is the main function used to detect the type and size of a remote image represented by the url.

Only check ImageType and ImageSize if error is not nil.

If you want to set request timeout for uri, use DetectImageTypeWithTimeout() instead.

func DetectImageTypeFromReader

func DetectImageTypeFromReader(r io.Reader) (ImageType, *ImageSize, error)

DetectImageTypeFromReader detects the type and size from a stream of bytes.

Only check ImageType and ImageSize if error is not nil.

func DetectImageTypeFromResponse

func DetectImageTypeFromResponse(resp *http.Response) (ImageType, *ImageSize, error)

DetectImageTypeFromResponse is a secondary function used to detect the type and size of a remote image represented by the resp. This way you can create your own request and then pass it here. Check examples from http://golang.org/pkg/net/http/

Only check ImageType and ImageSize if error is not nil.

func DetectImageTypeWithTimeout

func DetectImageTypeWithTimeout(uri string, timeout uint) (ImageType, *ImageSize, error)

DetectImageTypeWithTimeout acts same as DetectImageType(), except this function takes additional parameter timeout (millisecond) for setting request timeout.

If timeout < 1, default timeout 5000 is used.

Types

type ImageSize

type ImageSize struct {
	Width  uint32
	Height uint32
}

ImageSize holds the width and height of an image

type ImageType

type ImageType uint

ImageType represents the type of the image detected, or `Unknown`.

const (
	// GIF represents a GIF image
	GIF ImageType = iota
	// PNG represents a PNG image
	PNG
	// JPEG represents a JPEG image
	JPEG
	// BMP represents a BMP image
	BMP
	// TIFF represents a TIFF image
	TIFF
	// Unknown represents an unknown image type
	Unknown
)

func (ImageType) String

func (i ImageType) String() string

type ImageTypeParser

type ImageTypeParser interface {
	// Type returns the type of the image
	Type() ImageType

	// Returns true if the image type can be detected on the byte slice
	Detect([]byte) bool

	// Returns the image size by inspecting the byte slice, or error if it
	// can't be detected (more data needed?)
	GetSize([]byte) (*ImageSize, error)
}

ImageTypeParser is the interface each image type needs to implement to be able to detect images on a buffer stream.

Jump to

Keyboard shortcuts

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