Documentation ¶
Overview ¶
Package img provides constants, types and functions for automatic image generation.
Index ¶
- Constants
- func DrawText(canvas *gg.Context, text string, color *Color) error
- func EncodeJPEG(img image.Image, writer io.Writer) error
- func EncodePNG(img image.Image, writer io.Writer) error
- func EncodeTIFF(img image.Image, writer io.Writer) error
- func EncodeWEBP(img image.Image, writer io.Writer) error
- func FillBackground(canvas *gg.Context, color *Color)
- type Color
- type ImageEncoderFunc
- type ImageParams
- type ImageResult
- type Size
Constants ¶
const ( IMAGE_JPG = "jpg" IMAGE_JPEG = "jpeg" IMAGE_PNG = "png" IMAGE_TIFF = "tiff" IMAGE_WEBP = "webp" )
Constants for image file extensions
const PX_TO_PT = 0.75
Pixel to Point value scale factor (1px = 0.75pt)
Variables ¶
This section is empty.
Functions ¶
func DrawText ¶
DrawText draws the given text on the canvas with given color.
Dynamic font size is used to draw text and is calcuated by canvas height * PX_TO_PT * 0.2.
The text is anchored at the image centre. It also wraps around when overflows the canvas width.
func EncodeJPEG ¶
EncodeJPEG encodes the given image.Image into JPEG format bytes and writes those bytes in io.Writer. If an error occurs while encoding or writing, it returns that error.
It uses 75% quality while encoding JPEG image.
EncodeJPEG is compatible with ImageEncoderFunc type.
func EncodePNG ¶
EncodePNG encodes the given image.Image into PNG format bytes and writes those bytes in io.Writer. If an error occurs while encoding or writing, it returns that error.
EncodePNG is compatible with ImageEncoderFunc type.
func EncodeTIFF ¶
EncodeTIFF encodes the given image.Image into TIFF format bytes and writes those bytes in io.Writer. If an error occurs while encoding or writing, it returns that error.
It uses deflate compression and a predictor while encoding TIFF image.
EncodeTIFF is compatible with ImageEncoderFunc type.
func EncodeWEBP ¶
EncodeWEBP encodes the given image.Image into WEBP format bytes and writes those bytes in io.Writer. If an error occurs while encoding or writing, it returns that error.
EncodeWEBP is compatible with ImageEncoderFunc type.
func FillBackground ¶
FillBackground fills the canvas with given color.
Types ¶
type Color ¶
A Color represents the pixel color of an image. Each color component is stored as a separate uint8 value.
type ImageEncoderFunc ¶
ImageEncoderFunc represents encoding function for any image type.
It should encode the image.Image and write the data to the io.Writer. If any error occurs during encoding process, it should return that error otherwise it should return nil.
type ImageParams ¶
type ImageParams struct { Format string // Image extension *Size // Image Size BackgroundColor *Color // Color to use for background TextColor *Color // Color to use for text Scale float64 // Value by which to scale Size Text string // Text to write on the image }
An ImageParams stores parameters for image generation.
type ImageResult ¶
type ImageResult struct { Bytes []byte // Encoded bytes of generated image Length int // Length = len(Bytes) MimeType string // Image mime-type in format "image/<extension>" e.g. image/png, for png image. }
An ImageResult stores data of generated image.
func Generate ¶
func Generate(params *ImageParams) (*ImageResult, error)
Generate generates an image with given parameters.
It returns ImageResult, nil when image is generated successfully. If error occurs while generating image, it returns nil, error.
The actual dimensions of image is calculated by scaling width and height with scale factor in ImageParams.