fastimage: github.com/rubenfonseca/fastimage Index | Examples | Files

package fastimage

import "github.com/rubenfonseca/fastimage"

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.

Code:

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

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

Code:

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

Package Files

bmp.go doc.go fastimage.go gif.go image_size.go image_type.go image_type_parser.go image_type_string.go jpeg.go logger.go png.go tiff.go util.go

func Debug Uses

func Debug()

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

type ImageSize Uses

type ImageSize struct {
    Width  uint32
    Height uint32
}

ImageSize holds the width and height of an image

type ImageType Uses

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
)

go:generate stringer -type=ImageType -output=image_type_string.go

func DetectImageType Uses

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 Uses

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 Uses

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 Uses

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.

func (ImageType) String Uses

func (i ImageType) String() string

type ImageTypeParser Uses

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.

Package fastimage imports 10 packages (graph) and is imported by 1 packages. Updated 2017-01-30. Refresh now. Tools for package owners.