Documentation
¶
Overview ¶
Package thumbnail provides a method to create thumbnails from images.
Example ¶
var config = Generator{
DestinationPath: "",
Prefix: "thumb_",
//Scaler: "CatmullRom",
}
imagePath := "path/to/image.jpg"
//dest := "path/to/thumb_image.jpg"
gen := NewGenerator(config, []ImageDimension{
defaultImageOutput,
})
i, err := gen.NewImageFromFile(imagePath)
if err != nil {
panic(err)
}
thumbBytes, err := gen.GetProcessedImage(i, defaultImageOutput)
if err != nil {
//t.Error(err)
panic(err)
}
img := i
img.ImageData = thumbBytes
_, err = gen.Save(img)
if err != nil {
panic(err)
}
//
//thumbBytes, err := gen.CreateThumbnail(i)
//if err != nil {
// panic(err)
//}
//
//err = os.WriteFile(dest, thumbBytes, 0644)
//if err != nil {
// panic(err)
//}
Index ¶
- Variables
- func CreateThumbnail(i *Image, dimension ImageDimension) (img image.Image, err error)
- type GenerationResult
- type Generator
- func (gen *Generator) Generate(i *Image) ([]GenerationResult, error)
- func (gen *Generator) GetGeneratorDimension() ImageDimension
- func (gen *Generator) GetProcessedImage(i *Image, dimension ImageDimension) (img image.Image, err error)
- func (gen *Generator) NewImageFromByteArray(path []byte) (*Image, error)
- func (gen *Generator) NewImageFromFile(path string) (*Image, error)
- func (gen *Generator) NewImageFromFilewWithDefault(path string, defaultImg string) (*Image, error)
- func (gen *Generator) Save(i *Image) (result GenerationResult, err error)
- func (gen *Generator) SaveWithDimension(i *Image, imgConf *ImageDimension) (result GenerationResult, err error)
- type Image
- type ImageDimension
- type ImageSize
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidMimeType is returned when a non-image content type is // detected. ErrInvalidMimeType = errors.New("invalid mimetype") ErrInvalidImageData = errors.New("invalid image data ") ErrInvalidNoTransformProvided = errors.New("no transform data was provided ") // ErrInvalidScaler is returned when an unrecognized scaler is // passed to the Generator. ErrInvalidScaler = errors.New("invalid scaler") // DefaultThumbnailPercentage the default value to use on percentage resizing DefaultThumbnailPercentage = 0.4 // DefaultThumbnailSize the default dimensions used for resizing DefaultThumbnailSize = ImageSize{Width: 220, Height: 220} )
Functions ¶
func CreateThumbnail ¶ added in v0.1.1
func CreateThumbnail(i *Image, dimension ImageDimension) (img image.Image, err error)
CreateThumbnail generates a thumbnail.
Types ¶
type GenerationResult ¶
type GenerationResult struct {
// Filename the name of the file
Filename string
// Path the path of the file in the file system
Path string
//Error the error reported by the process of the generation
Error error
}
func SaveRaw ¶ added in v0.1.1
func SaveRaw(i image.Image, path string, format imgconv.FormatOption) (result GenerationResult, err error)
SaveRaw generates a thumbnail.
type Generator ¶
type Generator struct {
// Width is the destination thumbnail width.
Width int
// Height is the destination thumbnail height.
Height int
// The preferred format for exporting the thumbnails
PreferredFormat imgconv.FormatOption
//Name is the game it will output ass
Name string
// DestinationPath is the destination thumbnail path.
DestinationPath string
// Prefix is the prefix for the destination thumbnail
// filename.
Prefix string
// OutputFormats the formats (dimensions), that the image will be exported to.
OutputFormats []ImageDimension
}
Generator registers a generator configuration to be used when creating thumbnails.
func New ¶ added in v0.1.1
NewGenerator returns an instance of a thumbnail generator with a given configuration.
func NewGenerator ¶
func NewGenerator(c Generator, outputFormats []ImageDimension) *Generator
NewGenerator returns an instance of a thumbnail generator with a given configuration.
func (*Generator) Generate ¶
func (gen *Generator) Generate(i *Image) ([]GenerationResult, error)
Generate generates all the images for the specified file with the dimensions on the generator
func (*Generator) GetGeneratorDimension ¶ added in v0.1.1
func (gen *Generator) GetGeneratorDimension() ImageDimension
GetGeneratorDimension return a dimension object based on the values inside the generator.
func (*Generator) GetProcessedImage ¶
func (gen *Generator) GetProcessedImage(i *Image, dimension ImageDimension) (img image.Image, err error)
GetProcessedImage get the processed image from resize.
func (*Generator) NewImageFromByteArray ¶
NewImageFromByteArray reads in an image file from the file system and populates an Image object. That new Image object is returned along with any errors that occur during the operation.
func (*Generator) NewImageFromFile ¶
NewImageFromFile reads in an image file from the file system and populates an Image object. That new Image object is returned along with any errors that occur during the operation.
func (*Generator) NewImageFromFilewWithDefault ¶ added in v0.1.1
NewImageFromFile reads in an image file from the file system and populates an Image object. That new Image object is returned along with any errors that occur during the operation.
func (*Generator) Save ¶
func (gen *Generator) Save(i *Image) (result GenerationResult, err error)
Save save the image
func (*Generator) SaveWithDimension ¶
func (gen *Generator) SaveWithDimension(i *Image, imgConf *ImageDimension) (result GenerationResult, err error)
SaveWithDimension generates a thumbnail.
type Image ¶
type Image struct {
// Path is a path to an image.
Path string
// Data is the image data in a byte-array
ImageData image.Image
// Current stores the existing image's dimensions
Size ImageSize
// Future store the new thumbnail dimensions.
//TODO: compatibility reasons
TargetDimension ImageSize
}
An Image is an image and information about it.
func ImageFromFile ¶ added in v0.1.1
type ImageDimension ¶
type ImageDimension struct {
// Width is the width of an image in pixels.
Width int
// Height is the height on an image in pixels.
Height int
// Percentage
Percentage float64
//For selecting the images there is need for the selection of the names.
// Prefix > Name > Default [ the order of the selection of the namings]
//Prefix
Prefix string
//Name
Name string
//Name
DestinationOverride string
}
ImageDimension stores dimensional information for an Image.