photo

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxWidth  uint = 3840
	DefaultMaxHeight uint = 3840
)
View Source
const DefaultFormat = "jpeg"
View Source
const (
	Original = "original"
)

Variables

This section is empty.

Functions

func DominantImageColor added in v0.6.0

func DominantImageColor(
	ctx context.Context,
	img image.Image,
) (color.Color, error)

DominantImageColor returns the dominant color of the image. This is calculated by creating a histogram of all image pixels, then selecting the color that is used the most after excluding pure black and white (unless they are the only possible choices).

func Encode added in v0.6.0

func Encode(format string, w io.Writer, img image.Image, opts any) error

Encode encodes the image to the given format and writes it to the writer. If the format is empty, the default format is assumed. If the format is not registered, an error is returned.

func FilenameForFormat added in v0.6.0

func FilenameForFormat(format, path string) (string, error)

FilenameForFormat returns a filename for the given format. If the path already has an extension for the format, the path is returned as is. Otherwise, the path is returned with the extension for the format appended. If the format is empty, the default format is assumed. If the format is not registered, an error is returned.

func FilenameWithoutFormat added in v0.6.0

func FilenameWithoutFormat(format, path string) string

FilenameWithoutFormat returns a filename with the format extension removed.

func PreferredExt added in v0.6.0

func PreferredExt(format string) string

func RegisterEncoder added in v0.6.0

func RegisterEncoder(format string, ext []string, enc EncoderFunc)

Types

type Complete added in v0.6.0

type Complete struct {
	// contains filtered or unexported fields
}

Complete provides a complete implementation of the Image interface, providing ImageDecoded and ImageReader.

func (*Complete) Filename added in v0.6.0

func (i *Complete) Filename() string

func (*Complete) Image added in v0.6.0

func (i *Complete) Image() (image.Image, string, error)

func (*Complete) Reader added in v0.6.0

func (i *Complete) Reader() (io.ReadCloser, error)

type Creator

type Creator struct {
	Name string `yaml:"name" json:"name"`
	Link string `yaml:"link" json:"link"`
}

Creator contains the metadata about the creator of a photo.

type Descriptor added in v0.6.0

type Descriptor struct {
	Link    string  `yaml:"link" json:"link"`
	Type    string  `yaml:"type" json:"type"`
	Title   string  `yaml:"title,omitempty" json:"title,omitempty"`
	Color   string  `yaml:"color,omitempty" json:"color,omitempty"`
	Creator Creator `yaml:"creator" json:"creator"`
	// contains filtered or unexported fields
}

Descriptor provides metadata data about the image, which is stored in a serialization format on openscripture.today, and also includes a reference to the image file.

func (*Descriptor) AddImage added in v0.6.0

func (d *Descriptor) AddImage(key string, img Image)

AddImage adds an image to the descriptor.

func (*Descriptor) GetColor added in v0.6.0

func (d *Descriptor) GetColor() (color.Color, error)

GetColor returns a color.Color after decoding the CSS string stored. Returns nil and an error if the color cannot be decoded. Returns nil with no error if the color is not set.

func (*Descriptor) GetImage added in v0.6.0

func (d *Descriptor) GetImage(key string) ImageComplete

GetImage returns an image from the descriptor.

func (*Descriptor) HasImage added in v0.6.0

func (d *Descriptor) HasImage(key string) bool

HasImage returns true if the descriptor has an image with the given key.

func (*Descriptor) RemoveImage added in v0.6.0

func (d *Descriptor) RemoveImage(key string)

RemoveImage removes an image from the descriptor.

func (*Descriptor) SetColor added in v0.6.0

func (d *Descriptor) SetColor(c color.Color)

SetColor sets the color as a CSS string from a color.Color.

type EncoderFunc added in v0.6.0

type EncoderFunc func(w io.Writer, img image.Image, opts any) error

type File added in v0.6.0

type File struct {
	Path string
}

func NewFile added in v0.6.0

func NewFile(path string) *File

func (*File) Filename added in v0.6.0

func (f *File) Filename() string

func (*File) Reader added in v0.6.0

func (f *File) Reader() (io.ReadCloser, error)

type Image added in v0.6.0

type Image interface {
	// Filename is the filename for the image. This may include a directory path
	// as well. It may be useful as a cache key, so a unique name is preferred.
	Filename() string
}

Image is the interface for a photo image. Every Image must also implemented ImageDecoded or ImageReader.

type ImageComplete added in v0.6.0

type ImageComplete interface {
	Image
	ImageReader
	ImageDecoded
}

ImageComplete is the interface for images that implement Image, ImageReader, and ImageDecoded.

func CompleteImage added in v0.6.0

func CompleteImage(img Image) ImageComplete

type ImageDecoded added in v0.6.0

type ImageDecoded interface {
	// Image returns the image and the format of the image. The format is the
	// name of the format, like "jpeg" or "png". It should match the registered
	// format used to decode the image. If the image was not decoded, the format
	// should be empty. An error should be returned if there's a problem getting
	// the iamge data.
	Image() (img image.Image, format string, err error)
}

ImageDecoded is the interface that provides the image data in decoded form.

type ImageReader added in v0.6.0

type ImageReader interface {
	Reader() (io.ReadCloser, error)
}

ImageReader is the interface that provides the image data via io.Reader. After reading the image data, the caller must close the reader.

type Memory added in v0.6.0

type Memory struct {
	Name   string
	Format string
	Img    image.Image
}

func NewMemory added in v0.6.0

func NewMemory(name string, format string, img image.Image) *Memory

func (*Memory) Filename added in v0.6.0

func (m *Memory) Filename() string

func (*Memory) Image added in v0.6.0

func (m *Memory) Image() (image.Image, string, error)

type Option

type Option func(*options)

func FromImage added in v0.6.0

func FromImage(img string) Option

func MaxHeight

func MaxHeight(h uint) Option

func MaxWidth

func MaxWidth(w uint) Option

type Service

type Service struct {
	Source
	Client *http.Client
}

func NewService

func NewService(s Source) *Service

func (*Service) Photo

func (s *Service) Photo(
	ctx context.Context,
	photoUrl string,
) (*Descriptor, error)

Meta returns the photo info for a given photo URL. It will cache the photo info in the local filesystem and in S3.

func (*Service) ResizedImage

func (s *Service) ResizedImage(
	ctx context.Context,
	d *Descriptor,
	opts ...Option,
) (string, error)

ResizedImage returns a resized version of the photo if it is larger than the maximum width and height I have set.

type Source

type Source interface {
	// Meta returns the photo info for a given URL.
	Photo(ctx context.Context, url string) (desc *Descriptor, err error)
}

Source is the interface for a source of photo information.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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