img

package
v0.0.0-...-de70330 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package img provides constants, types and functions for automatic image generation.

Index

Constants

View Source
const (
	IMAGE_JPG  = "jpg"
	IMAGE_JPEG = "jpeg"
	IMAGE_PNG  = "png"
	IMAGE_TIFF = "tiff"
	IMAGE_WEBP = "webp"
)

Constants for image file extensions

View Source
const PX_TO_PT = 0.75

Pixel to Point value scale factor (1px = 0.75pt)

Variables

This section is empty.

Functions

func DrawText

func DrawText(canvas *gg.Context, text string, color *Color) error

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

func EncodeJPEG(img image.Image, writer io.Writer) error

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

func EncodePNG(img image.Image, writer io.Writer) error

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

func EncodeTIFF(img image.Image, writer io.Writer) error

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

func EncodeWEBP(img image.Image, writer io.Writer) error

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

func FillBackground(canvas *gg.Context, color *Color)

FillBackground fills the canvas with given color.

Types

type Color

type Color struct {
	R uint8
	G uint8
	B uint8
}

A Color represents the pixel color of an image. Each color component is stored as a separate uint8 value.

type ImageEncoderFunc

type ImageEncoderFunc func(image.Image, io.Writer) error

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.

type Size

type Size struct {
	Width  int
	Height int
}

A Size represents the dimensions of an image i.e. image width and image height.

Jump to

Keyboard shortcuts

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