media

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2020 License: MIT Imports: 4 Imported by: 0

README

Media

A Golang package for determining information about media without decoding the entire file.

Currently it supports finding the dimensions of png, gif and bmp media by only reading 32 bytes.

Note: The API is in active development and may change.

Install

go get github.com/montanaflynn/media

Example Usage

From cmd/media-info-http:

giphy := "https://media1.giphy.com/media/l0ErxFClZX9L3bgBi/giphy.gif"
r, err := http.Get(giphy)
if err != nil {
	log.Fatal(err)
}
defer r.Body.Close()
m, err := media.Parse(r.Body)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("media dimensions: %v\n", m.Size())
// media dimensions: {480 270}

fmt.Printf("content type: %q\n", m.Type())
// content type: "image/gif"

j, err := json.Marshal(m)
if err != nil {
	log.Fatal(err)
}
fmt.Printf("%s\n", j)
// {"ContentType":"image/gif","Dimensions":{"Width":480,"Height":270}}

MIT License

Copyright (c) 2020 Montana Flynn (https://montanaflynn.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORpublicS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package media is for determining information about media without decoding the entire file.

Example Usage:

giphy := "https://media1.giphy.com/media/l0ErxFClZX9L3bgBi/giphy.gif"
r, err := http.Get(giphy)
if err != nil {
	log.Fatal(err)
}
defer r.Body.Close()
m, err := media.Parse(r.Body)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("media dimensions: %v\n", m.Size())
// media dimensions: {480 270}

fmt.Printf("content type: %q\n", m.Type())
// content type: "image/gif"

MIT License Copyright (c) 2020 Montana Flynn (https://montanaflynn.com)

Index

Constants

View Source
const (
	// PNGType media type
	PNGType ContentType = "image/png"

	// GIFType media type
	GIFType = "image/gif"

	// BMPType media type
	BMPType = "image/bmp"

	// JPEGType media type
	JPEGType = "image/jpeg"
)

Variables

View Source
var (
	// ErrUnknownContentType is when media is an unknown type
	ErrUnknownContentType = errors.New("media: unknown media type")

	// ErrUnsupportedSize is when media type doesn't implement size
	ErrUnsupportedSize = errors.New("media: unsupported size")

	// ErrPNGMissingIHDR is when a png is missing the HDR header
	ErrPNGMissingIHDR = errors.New("media: invalid png missing IHDR")

	// ErrBMPInvalidHeaderLength is when a bmp has invalid header length
	ErrBMPInvalidHeaderLength = errors.New("media: invalid bmp header length")
)

Functions

This section is empty.

Types

type BMP

type BMP struct {
	ContentType ContentType
	Dimensions  Dimensions
	Format      BMPFormat
}

BMP holds the BMP

func (BMP) Size

func (b BMP) Size() Dimensions

Size returns the BMP size in width and height

func (BMP) Type

func (b BMP) Type() ContentType

Type returns the BMP content type

type BMPFormat

type BMPFormat string

BMPFormat is the type of the media

const (
	// OS2Format bmp format
	OS2Format BMPFormat = "OS/2"

	// WindowsFormat bmp format
	WindowsFormat = "Windows"
)

type ContentType

type ContentType string

ContentType is the type of the media

func DetectContentType

func DetectContentType(r io.Reader) (ContentType, []byte, error)

DetectContentType returns the ContentType from the first 32 bytes

type Dimensions

type Dimensions struct {
	Width  int
	Height int
}

Dimensions holds the Dimensions

type GIF

type GIF struct {
	ContentType ContentType
	Dimensions  Dimensions
}

GIF holds the GIF

func (GIF) Size

func (b GIF) Size() Dimensions

Size returns the GIF size in width and height

func (GIF) Type

func (b GIF) Type() ContentType

Type returns the GIF media type

type Media

type Media interface {
	Size() Dimensions
	Type() ContentType
}

Media is an interface for getting information from media

func Parse

func Parse(r io.Reader) (Media, error)

Parse returns the media information including type and dimensions

type PNG

type PNG struct {
	ContentType ContentType
	Dimensions  Dimensions
}

PNG holds the PNG

func (PNG) Size

func (b PNG) Size() Dimensions

Size returns the PNG size in width and height

func (PNG) Type

func (b PNG) Type() ContentType

Type returns the PNG content type

Directories

Path Synopsis
size module

Jump to

Keyboard shortcuts

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