imagegenerator

package module
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: GPL-3.0 Imports: 24 Imported by: 0

README

image-generator

Library for generate image from text structure

all measures are pixel/inch

example:

package main

import (
	"flag"
	"fmt"
	"log"

	ig "github.com/hmmftg/image-generator"
)

var (
	basePath     = flag.String("base", "..", "base path")
	imagesFolder = flag.String("images", "images", "name of the images folder")
	fontFolder   = flag.String("fonts", "fonts", "name of the fonts folder")
	fontfile     = flag.String("fontfile", "arial.ttf", "filename of the ttf font")
	imageWidth   = flag.Int("width", 800, "image width")
	imageHeight  = flag.Int("height", 800, "image height")
)

func main() {
	flag.Parse()

    // first load ttf font files
	fonts, err := ig.LoadFonts(fmt.Sprintf("%s/%s", *basePath, *fontFolder))
	if err != nil {
		log.Fatal(err)
	}

    // load asset images(used as background or logo or etc)
	images, err := ig.LoadImages(fmt.Sprintf("%s/%s", *basePath, *imagesFolder))
	if err != nil {
		log.Fatal(err)
	}

	req := ig.PrintRequest{ // each request contains multiple objects to be printed on all defined outputs  
		Drawings: []ig.Drawable{ // each drawing is a printable element, currently supporting: text, rectangle and image
			ig.Text{ // print text on dimension
				Text:       "Hello World!",
				X:          4,
				Y:          4,
				RightAlign: false, // rtl
				FontFace:   "arial.ttf:30", // font name(should exist in defined fonts folder) : font size
			},
			ig.Rect{ // print rectangle on dimesion
				Thickness: 2,
				X1:        8,
				X2:        *imageWidth - 8,
				Y1:        8,
				Y2:        *imageHeight - 8,
				Color:     ig.GreenRuler,
			},
			&ig.Image{ // print image asset, like logo on dimesion
				ID: "logo1.png", // image asset which should exist in defined images folder
				X:  30,
				Y:  30,
			},
		},
		Images: []ig.ImageData{
			{
				Dpi:            300,
				BackgroundFile: "bg1.png", // background image, if nil white background used by default
				Height:         *imageY,
				Width:          *imageX,
				FileFormat:     ig.Png, // output file type(bmp and png supported)
				File:           "sB.png", // output file name
				NeedB64:        false, // will return base64 string if true
			},
			{
				Dpi:        72,
				Height:     *imageY,
				Width:      *imageX,
				FileFormat: ig.Png,
				File:       "s1.png",
				NeedB64:    false,
			},
		},
	}

    // for each request generate each image and save it and return result containing base64 of images if NeedB64 of them was true
	result, err := ig.ProcessRequest(map[string]ig.PrintRequest{"reuqest1": req}, fonts, images)
}

Documentation

Index

Constants

View Source
const (
	Bmp           = "bmp"
	BmpMonoChrome = "bmp-monochrome"
	Png           = "png"
)
View Source
const (
	BmpAndPng = "bmp+png"
	JustPng   = "png"
)

Variables

View Source
var (
	BlackRuler = color.RGBA{0x00, 0x00, 0x00, 0xff}
	GreenRuler = color.RGBA{0x40, 0xb0, 0xa0, 0xff}
	RedRuler   = color.RGBA{0xB0, 0x40, 0x40, 0xff}
)

Functions

func CoordinationToRelation added in v0.0.7

func CoordinationToRelation(c, v int) float64

func DrawImage

func DrawImage(img draw.Image, target image.Image, x, y int)

func DrawRect

func DrawRect(x1, y1, x2, y2, thickness int, img draw.Image, col color.Color)

Rect draws a rectangle utilizing HLine() and VLine()

func EncodeMonoChrome added in v0.0.15

func EncodeMonoChrome(w io.Writer, buffer io.Reader) error

func GetImage

func GetImage(img *ImageData, resp map[string]string, rgba draw.Image) error

func HLine

func HLine(img draw.Image, col color.Color, x1, y, x2 int)

HLine draws a horizontal line

func Justify added in v0.0.14

func Justify(input string, length int) string

func LoadFonts

func LoadFonts(src string) (map[string]opentype.Font, error)

func LoadImages

func LoadImages(src string) (map[string]image.Image, error)

func NumToPersianWords added in v0.0.9

func NumToPersianWords(number string) string

func NumToWords added in v0.0.9

func NumToWords(number string) string

func ProcessRequest

func ProcessRequest(
	requests map[string]PrintRequest,
	fonts map[string]opentype.Font,
	images map[string]image.Image,
) (map[string]string, error)

func RelationalCoordinate added in v0.0.7

func RelationalCoordinate(r float64, v int) int

func VLine

func VLine(img draw.Image, col color.Color, x, y1, y2 int)

VLine draws a veritcal line

Types

type Drawable

type Drawable interface {
	Draw(tx *PrintTx) int
}

type Image

type Image struct {
	ID         string
	Target     image.Image
	Scale      float64
	X, Y       float64
	RightAlign bool
}

func (Image) Draw

func (i Image) Draw(tx *PrintTx) int

type ImageData

type ImageData struct {
	Dpi             float64
	Height          int
	Width           int
	BackgroundFile  string
	NeedB64         bool
	FileFormat      string
	File            string
	MonoChrome      bool
	MonoChromeColor color.Color
}

func (ImageData) InitializeImage

func (img ImageData) InitializeImage(bgMap map[string]image.Image, tx *PrintTx,
)

type Line added in v0.0.4

type Line struct {
	X1, X2    float64
	Y1, Y2    float64
	Thickness int
	Color     color.Color
}

func (Line) Draw added in v0.0.4

func (l Line) Draw(tx *PrintTx) int

type PrintRequest

type PrintRequest struct {
	Drawings []Drawable
	Images   []ImageData
	Margin   float64
}

type PrintTx

type PrintTx struct {
	MonoChrome      bool
	MonoChromeColor color.Color
	Rgba            draw.Image
	Src             image.Image
	Fg              image.Image
	Bg              *image.Uniform
	Dpi             float64
	Margin          float64
	Fonts           map[string]opentype.Font
	Faces           *map[string]font.Face
	Images          map[string]image.Image
}

func (*PrintTx) AddFace added in v0.0.7

func (tx *PrintTx) AddFace(fontName string, sz float64) (string, font.Face)

func (*PrintTx) CoordinationX added in v0.0.7

func (tx *PrintTx) CoordinationX(c int) float64

func (*PrintTx) CoordinationY added in v0.0.7

func (tx *PrintTx) CoordinationY(c int) float64

func (*PrintTx) RelationalX added in v0.0.7

func (tx *PrintTx) RelationalX(rx float64) int

func (*PrintTx) RelationalY added in v0.0.7

func (tx *PrintTx) RelationalY(rx float64) int

type Rect

type Rect struct {
	Thickness int
	X1, X2    float64
	Y1, Y2    float64
	Color     color.Color
}

func (Rect) Draw

func (r Rect) Draw(tx *PrintTx) int

type Text

type Text struct {
	Text string
	X    float64
	Y    float64
	// TODO:FixedWidth           int
	MaxWidth             float64 // if result width is more than this value, library tries new font face with decreased size
	RightAlign           bool
	NumbersToArabic      bool
	NumbersToPersian     bool
	NumberToWords        bool
	NumberToPersianWords bool
	FontFace             string
}

func (Text) CheckFace

func (s Text) CheckFace(tx *PrintTx) (string, font.Face)

func (Text) Draw

func (s Text) Draw(tx *PrintTx) int

func (Text) Font

func (s Text) Font(dpi float64) string

func (Text) FontData added in v0.0.7

func (s Text) FontData() (string, float64)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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