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 ¶
- func Debug()
- func DetectImageType(uri string) (ImageType, *ImageSize, error)
- func DetectImageTypeFromReader(r io.Reader) (ImageType, *ImageSize, error)
- func DetectImageTypeFromResponse(resp *http.Response) (ImageType, *ImageSize, error)
- func DetectImageTypeWithTimeout(uri string, timeout uint) (ImageType, *ImageSize, error)
- type ImageSize
- type ImageType
- type ImageTypeParser
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 ¶
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 ¶
DetectImageTypeFromReader detects the type and size from a stream of bytes.
Only check ImageType and ImageSize if error is not nil.
func DetectImageTypeFromResponse ¶
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 ¶
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 ImageType ¶
type ImageType uint
ImageType represents the type of the image detected, or `Unknown`.
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.