Documentation ¶
Index ¶
- Variables
- type Croppable
- type CroppableImage
- type Cropper
- type CropperOption
- func WithEnumerate(enumerate bool) CropperOption
- func WithOutDir(dir string) CropperOption
- func WithOutPrefix(prefix string) CropperOption
- func WithOutSuffix(suffix string) CropperOption
- func WithPadding(padding int) CropperOption
- func WithSkipUnchanged(skip bool) CropperOption
- func WithThreshold(threshold uint32) CropperOption
- type Finder
- type FinderOptions
Constants ¶
This section is empty.
Variables ¶
var ErrImageLoadFailed = errors.New("unable to load image")
var ErrImageUncroppable = errors.New("image does not support cropping")
var ErrUnsupportedFormat = errors.New("unsupported format")
Functions ¶
This section is empty.
Types ¶
type Croppable ¶
type Croppable struct { Path string Image CroppableImage Decode func(r io.Reader) (image.Image, error) Encode func(w io.Writer, m image.Image) error }
Croppable holds the path of the image, the image itself and a proper encoder function for encoding the image.
func Load ¶
Load validates if given image format is supported, if so creates a *Croppable and calls it's Load() method.
func (*Croppable) Load ¶
Load loads the image of the croppable using it's decoder returns an error if image was not successfully decoded or image is not croppable.
func (*Croppable) With ¶
func (c *Croppable) With(ci CroppableImage) *Croppable
With returns a copy of current croppable with Image set to provided image.
type CroppableImage ¶
Croppable image is an extension of image.Image interface to ensure the image is croppable.
type Cropper ¶
type Cropper struct {
// contains filtered or unexported fields
}
Cropper crops and saves images.
func NewCropper ¶
func NewCropper(options ...CropperOption) (*Cropper, error)
NewCropper creates an instance of *Cropped with provided options, returns error if any option fails.
Default Cropper with no options:
- has alpha threshold of 0 and no padding
- saves images under the same name in the same directory as the source image (file will be overwritten). If cropping made no changes it still saves the result.
func (*Cropper) Crop ¶
Crop takes a *Croppable and returns a cropped version of it and a bool flag indicating if cropping was done. If cropping would make no changes to given *Croppable, the provided *Croppable is returned back with false flag.
func (*Cropper) CropAndSave ¶
CropAndSave crops an image and saves it, output directory will be created if it does not exist. Error will be returned if the image was not saved successfully.
type CropperOption ¶
func WithEnumerate ¶
func WithEnumerate(enumerate bool) CropperOption
WithEnumerate enables enumeration of output images. "_n" is appended to it's name (before the suffix), n is an integer incremented by 1 each time an image is saved. Given n of 5, image file name of "image1.png", the output image will be named "image1_5.png".
func WithOutDir ¶
func WithOutDir(dir string) CropperOption
WithOutDir sets the output directory for cropped images.
func WithOutPrefix ¶
func WithOutPrefix(prefix string) CropperOption
WithOutPrefix adds a prefix to an image name. Given prefix "cropped_" and image file name "image1.png", the output image will be named "cropped_image1.png".
func WithOutSuffix ¶
func WithOutSuffix(suffix string) CropperOption
WithOutSuffix adds a suffix to an image name. Given suffix "_cropped" and image file name "image1.png", the output image will be named "image1_cropped.png".
func WithPadding ¶
func WithPadding(padding int) CropperOption
WithPadding sets the number of pixels to add in each direction around the cropped image. If the cropped output is the size 25x25px, with 5px of padding it will be 35x35px with the cropped element centered.
func WithSkipUnchanged ¶
func WithSkipUnchanged(skip bool) CropperOption
WithSkipUnchanged if set to true output image won't be saved if cropping made no changes to original image.
func WithThreshold ¶
func WithThreshold(threshold uint32) CropperOption
WithThreshold sets the alpha channel threshold for cropping. Only pixels that satisfy the condition pixelAlpha > threshold will be used in the process of finding a cropping rectangle.
type Finder ¶
type Finder struct {
// contains filtered or unexported fields
}
Finder is a tool for loading all valid images from given directories. It can apply regex filtering and descend recursively through the directory tree.
func NewFinder ¶
func NewFinder(options ...FinderOptions) (*Finder, error)
NewFinder is a constructor for *Finder, accepts FinderOptions, returns an error if any option fails.
type FinderOptions ¶
func WithRecursive ¶
func WithRecursive(enable bool) FinderOptions
WithRecursive enables Finder to traverse all subdirectories.
func WithRegex ¶
func WithRegex(regex string) FinderOptions
WithRegex attempts to compile given regex string if successful enables Finder regex filtering.