goposter

package module
v0.0.0-...-716ed4d Latest Latest
Warning

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

Go to latest
Published: May 20, 2024 License: MIT Imports: 17 Imported by: 0

README

goposter

这是一个通过golang来生成海报的库,通过简单的配置,即可实现美观海报的生成

简单入手

  • 依赖
    • 此程序由于需要支持webp格式图片,是以对webp的环境有些依赖,需要在linux中按照gcc才能跑起来

      也可以切换到无webp图片的分支使用,这样就是纯纯的go程序

    • go版本:乱搞即可
  • 可以参考example中的示例程序

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/danielrqzeng/goposter"
	"os"

	"github.com/spf13/viper"
	"text/template"
)

func main() {
	/*
		手机海报样式
		|-----------------------|
		| + | name              |
		|-----------------------|
		|                       |
		|                       |
		|                       |
		|                       |
		|                       |
		|                       |
		|-----------------------|
		| Title         |   |   |
		| desc          |-- 。--|
		|               |   |   |
		|-----------------------|
	*/

	viper.SetConfigFile("./app.yaml")
	err := viper.ReadInConfig()
	if err != nil {
		fmt.Println(err)
		return
	}
	var posterList []goposter.ImageConfigInfoType
	err = viper.UnmarshalKey("posterList", &posterList)
	if err != nil {
		fmt.Println(err)
		return
	}
	//fmt.Println(fmt.Sprintf("%+v", posterList))

	jsonStr, err := json.Marshal(posterList[0])
	if err != nil {
		fmt.Println(err)
		return
	}
	//fmt.Println(string(jsonStr))

	tmpl := template.New("poster")
	tmpl, err = tmpl.Parse(string(jsonStr))
	if err != nil {
		fmt.Println(err)
		return
	}

	pixelRatio := 2.0 // 逻辑像素和物理像素比例
	width := 320      // iphone5的尺寸
	height := 548
	avatarUrl := "./avatar.jpeg" // 用户头像
	name := "李涉"                 // 用户名称
	mainUrl := "./main.jpg"      // 主图片
	highlight := "闲"             //高亮字
	title := "题鹤林寺僧舍"            //标题
	//desc := "因过竹院逢僧话\n偷得浮生半日闲"    // 描述
	desc1 := "终日昏昏醉梦间,忽闻春尽强登山." // 描述
	desc2 := "因过竹院逢僧话,又得浮生半日闲." // 描述
	qrCodeURL := "./qrcode.png" // 二维码

	params := map[string]string{
		"pixel_ratio": fmt.Sprintf("%f", pixelRatio),
		"width":       fmt.Sprintf("%d", int(float64(width)*pixelRatio)),
		"height":      fmt.Sprintf("%d", int(float64(height)*pixelRatio)),
		"avatar_url":  avatarUrl,
		"name":        name,
		"main_url":    mainUrl,
		"highlight":   highlight,
		"title":       title,
		"desc1":       desc1,
		"desc2":       desc2,
		"qr_code_url": qrCodeURL,
	}
	var buf bytes.Buffer
	err = tmpl.Execute(&buf, params)
	if err != nil {
		return
	}

	imageConfigInfo := &goposter.ImageConfigInfoType{}
	err = json.Unmarshal(buf.Bytes(), imageConfigInfo)
	if err != nil {
		fmt.Println("fail to exit,cuz=" + err.Error())
		return
	}

	imgFile := "output.png"
	buffer, err := goposter.ImageMgr().GenByImageConfig(imageConfigInfo)
	if err != nil {
		fmt.Println("fail to exit,cuz=" + err.Error())
		return
	}
	dstFile, err := os.Create(imgFile)
	if err != nil {
		fmt.Println("fail to exit,cuz=" + err.Error())
		return
	}
	defer dstFile.Close()

	// 将byte数据写入文件
	_, err = dstFile.Write(buffer.Bytes())
	if err != nil {
		fmt.Println("fail to exit,cuz=" + err.Error())
		return
	}

}
  • 运行后效果图

image

原理解析

  • 上面生成的关键在于配置文件example/app.yaml
blendent:
  bgColor: &bgColor "#FFFFFF" # 白色
  hgColor: &hgColor "#FF0000" # 红色
  mainColor: &mainColor "#000000" # 黑色

#  bgColor: &bgColor "#CAD9D1"
#  hgColor: &hgColor  "#133789"
#  mainColor: &mainColor "#000000" # 黑色
#
#  bgColor: &bgColor "#D7ECF1" # 月白
#  hgColor: &hgColor "#0EB0C9" # 孔雀蓝
#  mainColor: &mainColor "#000000" # 黑色
#
#  bgColor: &bgColor "#E4C6D0" # 藕荷色
#  hgColor: &hgColor "#4B5CC4" # 宝蓝
#  mainColor: &mainColor "#000000" # 黑色
#
#  bgColor: &bgColor "#EAECF1" # 浅云
#  hgColor: &hgColor "#8BA3C7" # 东方既白
#  mainColor: &mainColor "#000000" # 黑色
#
#  bgColor: &bgColor "#F9D3E3" # 盈盈
#  hgColor: &hgColor "#4781C3" # 苍苍
#  mainColor: &mainColor "#000000" # 黑色
#
#  bgColor: &bgColor "#8895B5" # 浅蓝灰
#  hgColor: &hgColor "#84533F" # 火岩棕
#  mainColor: &mainColor "#3D5346" # 晶石紫
#
#  bgColor: &bgColor "#BCBEBB" # 月影白
#  hgColor: &hgColor "#466746" # 荷叶绿
#  mainColor: &mainColor "#4E1B24" # 石竹紫
#
#  bgColor: &bgColor "#CCE4DD" # 月影白
#  hgColor: &hgColor "#50381D" # 荷叶绿
#  mainColor: &mainColor "#B9C66A" # 石竹紫
#
#
#  bgColor: &bgColor "#FFA631" # 杏黄
#  hgColor: &hgColor "#003472" # 花青
#  mainColor: &mainColor "#000000" # 黑色
#
#  bgColor: &bgColor "#FFF143" # 鹅黄
#  hgColor: &hgColor "#FF7500" # 橘红
#  mainColor: &mainColor "#000000" # 黑色
#
#  bgColor: &bgColor "#A1AFC9" # 蓝灰色
#  hgColor: &hgColor "#ED5736" # 妃色
#  mainColor: &mainColor "#000000" # 黑色

#  bgColor: &bgColor "#75664D" # 黎
#  hgColor: &hgColor "#4C221B" # 紫檀
#  mainColor: &mainColor "#FFFFFF" # 百色

posterList:
  - ID: "classic_poster"
    Name: "classic_poster"
    Enable: true # 是否启用
    Desc: "经典布局"
    Debug: false # 是否开启调试,如果开启了,则会给子图加入调式色块,以便于辨识面积和位置
    PixelRatio:  "{{.pixel_ratio}}" # 设备像素比,一般是逻辑像素和物理像素的比率,比如iphone5的逻辑像素是(320,568),物理像素是(640,1136),设备像素比为2
    Width: "{{.width}}" # 模板变量-画布的宽度(其实为手机的宽度)
    Height: "{{.height}}"
    CanvasBackgroundColor: *bgColor # 纯白
    OutputBufferType: "png" # 输出图片类型,png|jpeg|webp
    SubImageInfoList:
      - ID: "avatar"
        Name: "avatar"
        Desc: "头像"
        Enable: true # 是否启用
        ActionList:
          - ActionType: "image" # 类型为图片
            ImageType: "jpeg"  # 图片类型,png|jpeg|webp
            ImageLocalFile: "{{.avatar_url}}" # 如果是本地图片,填入图片地址
            ImageURLFile: "" # url图片,填入地址
          - ActionType: "resize" # 调整大小
            ResizeType: 2  # 调整大小的规则,0:none,1:根据指定宽高,2:只依据宽度,3:只依据高度
            ResizeWidth: "10%" # 可为整数或者百分数(如果是百分数,则基于画布宽高来计算)
            ResizeHeight: "" # 可为整数或者百分数(如果是百分数,则基于画布来计算)
          - ActionType: "circle" # 将头像截圆
            CircleOriginX: "0"  # 图的原点x,整数或者百分数(百分数的话,是基于本图,而不是画布的),none代表不指定,由子图自计算
            CircleOriginY: "0" # 图的原点y,整数或者百分数
            CircleRadius: "0" # 图的半径,整数(0代表依据本图计算)
          - ActionType: "location" # 将子图画在画布中
            LocationType: "absolute"  # 定位类型,absolute|relative|mixed
            RelativeToImageID: "" # 相对定位时候子图id
            AbsolutePosition: "3% none none 3%" # 相对于画布的定位值,可为整数|百分数|none,顺序为top right bottom left
            RelativePosition: "none none none none" # 相对于子元素的定位值,可为整数|百分数(相对画布)|none,顺序为top right bottom left

      - ID: "name"
        Name: "name"
        Desc: "名字"
        Enable: true # 是否启用
        ActionList:
          - ActionType: "font" # 类型为文字
            Text: "{{.name}}"  # 文本
            FontBackgroundColor: "" # 文本背景颜色,(只支持#FFFFFF格式)
            FontFile: "../assets/SourceHanSerifSC-Bold.ttf" # 字体文件
            FontColor: *mainColor # 字体颜色(只支持#FFFFFF格式),必需字段
            FontSizeByHeightPercent: "6%" # 字体大小,画布高度的百分比
            MaxWidth: "80%" # 字体最大宽度,整数或者百分数,如是百分数,则基于画布来计算
          - ActionType: "location" # 将子图画在画布中
            LocationType: "mixed"  # 定位类型,absolute|relative|mixed
            RelativeToImageID: "avatar" # 相对定位时候子图id
            AbsolutePosition: "3% none none none" # 相对于画布的定位值,可为整数|百分数|none,顺序为top right bottom left
            RelativePosition: "none none none 2%" # 相对于子图的定位值,可为整数|百分数(相对画布)|none,顺序为top right bottom left

      - ID: "main_image"
        Name: "main_image"
        Desc: "正文图片"
        Enable: true # 是否启用
        ActionList:
          - ActionType: "image" # 类型为图片
            ImageType: "jpeg"  # 图片类型,png|jpeg|webp
            ImageLocalFile: "{{.main_url}}" # 如果是本地图片,填入图片地址
            ImageURLFile: "" # url图片,填入地址
          - ActionType: "resize" # 调整大小
            ResizeType: 1  # 调整大小的规则,0:none,1:根据指定宽高,2:只依据宽度,3:只依据高度
            ResizeWidth: "94%" # 可为整数或者百分数(如果是百分数,则基于画布来计算)
            ResizeHeight: "65%" # 可为整数或者百分数(如果是百分数,则基于画布来计算)
          - ActionType: "round" # 圆角特效
            RoundRadius: "2%" # 图的矩形圆角半径,整数或者百分数,如果是百分数,代表基于本图width的圆角半径
          - ActionType: "location" # 将子图画在画布中
            LocationType: "mixed"  # 定位类型,absolute|relative|mixed
            RelativeToImageID: "avatar" # 相对定位时候子图id
            AbsolutePosition: "none none none 3%" # 相对于画布的定位值,可为整数|百分数|none,顺序为top right bottom left
            RelativePosition: "2% none none none" # 相对于image,下移动3%


      - ID: "highlight"
        Name: "highlight"
        Desc: "高亮字"
        Enable: true # 是否启用
        ActionList:
          - ActionType: "font" # 类型为文字
            Text: " {{.highlight}}.."  # 文本
            FontBackgroundColor: *hgColor # 文本背景颜色,(只支持#FFFFFF格式)
            FontFile: "../assets/SourceHanSerifSC-Bold.ttf" # 字体文件
            FontColor: "#FFFFFF" # 字体颜色(只支持#FFFFFF格式),必需字段
            FontSizeByHeightPercent: "6.5%" # 字体大小,画布高度的百分比
            MaxWidth: "80%" # 字体最大宽度,整数或者百分数,如是百分数,则基于画布来计算
          - ActionType: "circle" # 将头像截圆
            CircleOriginX: "0"  # 图的原点x,整数或者百分数(百分数的话,是基于本图,而不是画布的),none代表不指定,由子图自计算
            CircleOriginY: "0" # 图的原点y,整数或者百分数
            CircleRadius: "0" # 图的半径,整数(0代表依据本图计算)
          - ActionType: "location" # 将子图画在画布中
            LocationType: "mixed"  # 定位类型,absolute|relative|mixed
            RelativeToImageID: "main_image" # 相对定位时候子图id
            AbsolutePosition: "none none none 3%" # 相对于画布的定位值,可为整数|百分数|none,顺序为top right bottom left
            RelativePosition: "3% none none none" # 相对于子元素的定位值,可为整数|百分数(相对父元素)|none,顺序为top right bottom left

      - ID: "title"
        Name: "title"
        Desc: "标题"
        Enable: true # 是否启用
        ActionList:
          - ActionType: "font" # 类型为文字
            Text: "{{.title}}"  # 文本
            FontBackgroundColor: "" # 文本背景颜色,(只支持#FFFFFF格式)
            FontFile: "../assets/SourceHanSerifSC-Light.ttf" # 字体文件
            FontColor: *hgColor # 字体颜色(只支持#FFFFFF格式),必需字段
            FontSizeByHeightPercent: "5.5%" # 字体大小,画布长度的百分比
            MaxWidth: "80%" # 字体最大宽度,整数或者百分数,如是百分数,则基于画布来计算
          - ActionType: "round" # 圆角特效
            RoundRadius: "2%" # 图的矩形圆角半径,整数或者百分数,如果是百分数,代表基于本图width的圆角半径
          - ActionType: "location" # 将子图画在画布中
            #            LocationType: "mixed"  # 定位类型,absolute|relative|mixed
            #            RelativeToImageID: "main_image" # 相对定位时候子图id
            #            AbsolutePosition: "none none none 11%" # 相对于画布的定位值,可为整数|百分数|none,顺序为top right bottom left
            #            RelativePosition: "3% none none none" # 相对于子元素的定位值,可为整数|百分数(相对父元素)|none,顺序为top right bottom left
            LocationType: "relative"  # 定位类型,absolute|relative|mixed
            RelativeToImageID: "highlight" # 相对定位时候子图id
            AbsolutePosition: "none none none none" # 相对于画布的定位值,可为整数|百分数|none,顺序为top right bottom left
            RelativePosition: "-5.5% none none 2%" # 相对于子元素的定位值,可为整数|百分数(相对父元素)|none,顺序为top right bottom left

      - ID: "desc1"
        Name: "desc1"
        Desc: "描述"
        Enable: true # 是否启用
        ActionList:
          - ActionType: "font" # 类型为文字
            Text: " {{.desc1}}."  # 文本
            FontBackgroundColor: "" # 文本背景颜色,(只支持#FFFFFF格式)
            FontFile: "../assets/SourceHanSerifSC-Light.ttf" # 字体文件
            FontColor: *mainColor # 字体颜色(只支持#FFFFFF格式),必需字段
            FontSizeByHeightPercent: "3.1%" # 字体大小,画布长度的百分比
            MaxWidth: "80%" # 字体最大宽度,整数或者百分数,如是百分数,则基于画布来计算
          - ActionType: "location" # 将子图画在画布中
            LocationType: "mixed"  # 定位类型,absolute|relative|mixed
            RelativeToImageID: "highlight" # 相对定位时候子图id
            AbsolutePosition: "none none none 3%" # 相对于画布的定位值,可为整数|百分数|none,顺序为top right bottom left
            RelativePosition: "1% none none none" # 相对于子元素的定位值,可为整数|百分数(相对父元素)|none,顺序为top right bottom left

      - ID: "desc2"
        Name: "desc2"
        Desc: "描述"
        Enable: true # 是否启用
        ActionList:
          - ActionType: "font" # 类型为文字
            Text: " {{.desc2}}."  # 文本
            FontBackgroundColor: "" # 文本背景颜色,(只支持#FFFFFF格式)
            FontFile: "../assets/SourceHanSerifSC-Light.ttf" # 字体文件
            FontColor: *mainColor # 字体颜色(只支持#FFFFFF格式),必需字段
            FontSizeByHeightPercent: "3.1%" # 字体大小,画布长度的百分比
            MaxWidth: "80%" # 字体最大宽度,整数或者百分数,如是百分数,则基于画布来计算
          - ActionType: "location" # 将子图画在画布中
            LocationType: "mixed"  # 定位类型,absolute|relative|mixed
            RelativeToImageID: "desc1" # 相对定位时候子图id
            AbsolutePosition: "none none none 3%" # 相对于画布的定位值,可为整数|百分数|none,顺序为top right bottom left
            RelativePosition: "1% none none none" # 相对于子元素的定位值,可为整数|百分数(相对父元素)|none,顺序为top right bottom left


      - ID: "qr_code"
        Name: "qr_code"
        Desc: "二维码图片"
        Enable: true # 是否启用
        ActionList:
          - ActionType: "image" # 类型为图片
            ImageType: "png"  # 图片类型,png|jpeg|webp
            ImageLocalFile: "{{.qr_code_url}}" # 如果是本地图片,填入图片地址
            ImageURLFile: "" # url图片,填入地址
          - ActionType: "resize" # 调整大小
            ResizeType: 2  # 调整大小的规则,0:none,1:根据指定宽高,2:只依据宽度,3:只依据高度
            ResizeWidth: "30%" # 可为整数或者百分数(如果是百分数,则基于画布来计算)
            ResizeHeight: "" # 可为整数或者百分数(如果是百分数,则基于画布来计算)
          - ActionType: "location" # 将子图画在画布中
            LocationType: "mixed"  # 定位类型,absolute|relative|mixed
            RelativeToImageID: "main_image" # 相对定位时候子图id
            AbsolutePosition: "none 3% none none" # 相对于画布的定位值,可为整数|百分数|none,顺序为top right bottom left
            RelativePosition: "3% none none none" # 相对于子元素的定位值,可为整数|百分数(相对父元素)|none,顺序为top right bottom left
以下为单项解读
  • 画板

画板为相当于一个白纸,主要属性有画板多大,分辨率多少

* Width: 画板宽度,逻辑像素,比如iphone5为320
* Width: 画板宽度
* Debug: 是否开启调试,为了便于辨识子图面积和位置
* PixelRatio: 设备像素比,一般是逻辑像素和物理像素的比率,比如iphone5的逻辑像素是(320,548),物理像素是(640,1136),设备像素比为2
* CanvasBackgroundColor: 画板底色
* OutputBufferType: 输出图片类型,png|jpeg|webp
  • 子图-动作类型
    • image: 即要求加载一个图片,可以为png|jpeg|webp格式的图片,并且可以是本地图片或者图片链接
    • resize: 调整子图的大小,可以填入数字或者画板的百分比数来调整
    • circle: 调整子图的形状为原型,可以填入数字或者子图的百分比数来调整
    • round: 调整子图的圆角,可以填入数字或者子图宽度百分比数来调整
    • font: 文字
      • 文字内容
      • 文字颜色
      • 背景颜色
      • 字体文件:此处提供了免费可用的思源字体(另粗体和正常体是两个字体文件)
      • 字体大小:此处创新性使用画板高度来度量字体大小,以免小白用户被字体大小搞崩溃,也避免了字体大小在不同的尺寸的画板上展示的一致性
    • 定位: 定位是指要将子图放置在画板中的那个位置,必须要有一个

      需要先了解像素坐标系的一个排布

      坐标的分布,请请参考:像素坐标系
      .---------------------->x
      |
      |
      |
      |
      |
      y
      图片的左上角为坐标原点
      
      • 定位类型之绝对定位:其参考系为画板,按照top right bottom left此类html元素布局来展开,可以填入数字或者画板的百分比数字,必须要填入两个维度的(比如x和y的方向)

        举例: AbsolutePosition: "3% none none 3%": 距离画板顶部3%距离,左侧3%距离放置

      • 定位类型之相对定位:其参考系为子图,按照top right bottom left此类html元素布局来展开,可以填入数字或者画板的百分比数字,必须要填入两个维度的(比如x和y的方向)
        # 相对定位示意图
        #                        +------------+
        #                        |   relative |
        #                        +------------+
        #                        |    {top}   |
        #    +----------+--------+------------+--------+------------+
        #    | relative | {left} |    self    | {right}|  relative  |
        #    +----------+--------+------------+--------+------------+
        #                        |   {bottom} |
        #                        +------------+
        #                        |   relative |
        #                        +------------+
        

        举例: RelativePosition: "-5.5% none none 2%": 比如自身高度是5.5%,那么这个意思是和相对子图底部平齐,右移2%

      • 定位类型之混合定位:其参考系为画板和相对子图,是用得比较多的,按照top right bottom left此类html元素布局来展开,可以填入数字或者画板的百分比数字,必须要填入两个维度的(比如x和y的方向)

Documentation

Index

Constants

View Source
const (
	ActionTypeNone     = "none"     // 作图类型-无
	ActionTypeCanvas   = "canvas"   // 作图类型-画布
	ActionTypeFont     = "font"     // 作图类型-画字
	ActionTypeImage    = "image"    // 作图类型-加载子图
	ActionTypeResize   = "resize"   // 作图类型-调整大小
	ActionTypeRound    = "round"    // 作图类型-裁剪矩形圆角
	ActionTypeCircle   = "circle"   // 作图类型-裁剪圆
	ActionTypeLocation = "location" // 作图类型-定位且叠画子图

	ImageTypeNone = "none" //图片类型-无
	ImageTypePNG  = "png"  //图片类型-png
	ImageTypeJPEG = "jpeg" //图片类型-jpg或者jpeg
	ImageTypeWEBP = "webp" //图片类型-webp

	ResizeTypeByNone           = 0 //调整大小-无
	ResizeTypeByWidthAndHeight = 1 //调整大小-根据宽高的调整
	ResizeTypeByWidth          = 2 //调整大小-只根据宽度调整
	ResizeTypeByHeight         = 3 //调整大小-只根据高度调整

	LocationTypeNone     = "none"     //定位类型-无
	LocationTypeAbsolute = "absolute" //定位类型-绝对定位
	LocationTypeRelative = "relative" //定位类型-相对定位
	LocationTypeMixed    = "mixed"    //定位类型-混合定位
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CircleMask

type CircleMask struct {
	// contains filtered or unexported fields
}

圆形效果

func (*CircleMask) At

func (m *CircleMask) At(x, y int) color.Color

func (*CircleMask) Bounds

func (m *CircleMask) Bounds() image.Rectangle

func (*CircleMask) ColorModel

func (m *CircleMask) ColorModel() color.Model

type IImage

type IImage interface {
	LoadFromFile(imgFile string) (err error)                                                                                         // 加载-从文件中加载
	LoadFromBuffer(imgBuffer *bytes.Buffer) (err error)                                                                              // 加载-从buffer中加载
	LoadFromURL(imgURL string) (err error)                                                                                           // 加载-从url中加载
	LoadFromImageRGBA(img *image.RGBA) (err error)                                                                                   // 加载-设置当前实例为实例对象
	LoadFromText(word string, maxWidth int, fontFile string, dpi, fontSize float64, fontColorInHex, bgColorInHex string) (err error) // 加载-根据需要的文字创建一个图像
	LoadFromNew(width, height int, fontColorInHex string) (err error)                                                                // 加载-新建画板
	SetID(id string)                                                                                                                 // 设置id
	ID() string                                                                                                                      // id
	GetImage() (img *image.RGBA)                                                                                                     // 获取-*image.RGBA格式的实例
	Resize(newWidth, newHeight uint) (err error)                                                                                     // 操作-重新设置大小
	DrawImage(sub image.Image, x, y int) (err error)                                                                                 // 操作-在当前对象的(x,y)位置,叠着画上sub
	DrawSubImage(sub IImage, x, y int, debugColorInHex string) (err error)                                                           // 操作-叠画子图
	GetSubImagePosition(subImageID string) (position [4]int, err error)                                                              // 获取子图的位置
	DrawRoundImage(radius float64) (err error)                                                                                       // 操作,给image加上矩形的圆角
	DrawCircleImage(originX, originY, radius int) (err error)                                                                        //操作,裁剪为圆
	SaveToBuffer() (imgBuffer *bytes.Buffer, err error)                                                                              // 保存-图片为buffer
	SaveToWEBPFile(imgFile string) (err error)                                                                                       // 保存-图片为webp格式
	SaveToPNGFile(imgFile string) (err error)                                                                                        // 保存-图片为png格式
	SaveToJPEGFile(imgFile string) (err error)                                                                                       // 保存-图片为jpeg或者jpg格式
}

func NewImageJPGType

func NewImageJPGType() (ImageJPG IImage)

NewImageJPGType 新建一个webp文件加载的图像

func NewImagePNGType

func NewImagePNGType() (ImagePNG IImage)

NewImagePNGType 新建一个webp文件加载的图像

func NewImageWEBPType

func NewImageWEBPType() (ImageWEBP IImage)

NewImageWEBPType 新建一个webp文件加载的图像

type ImageActionType

type ImageActionType struct {
	ActionType string `json:"action_type"` // 类型, font:文字,image:图片加载,resize:重新规划尺寸,round:圆角,circle:圆形,location:摆放在画布位置

	//ActionType==font时候的字段
	Text                    string `json:"text"`                        // 文本
	FontBackgroundColor     string `json:"font_background_color"`       // 文本背景颜色,(只支持#FFFFFF格式)
	FontFile                string `json:"font_file"`                   // 字体文件,必需字段
	FontSizeByHeightPercent string `json:"font_size_by_height_percent"` // 字体大小,画板高度的百分比
	FontColor               string `json:"font_color"`                  // 字体颜色(只支持#FFFFFF格式),必需字段
	MaxWidth                string `json:"max_width"`                   // 字体所占的最大宽度(为0不限制),必需字段

	//ActionType==image时候的字段(image只支持png,jpeg,webp格式)
	ImageType       string `json:"image_type"`        //图片类型,image_type=png|jpeg|webp
	ImageLocalFile  string `json:"image_local_file"`  //图片本地路径(为空代表不是本地图片),比如: /data/image/aa.jpeg
	ImageURLFile    string `json:"image_url_file"`    //图片url路径(为空代表不是本地图片),比如: https://baidu.com/data/image/aa.jpeg
	ImageCustomFile string `json:"image_custom_file"` //图片自定义来源,自定义方法获取,此类别让业务按照要求来使用,此处不启用

	//ActionType==resize时候的字段
	ResizeType   int    `json:"resize_type"`   // 调整大小的规则,0:none,1:根据指定宽高,2:只依据宽度,3:只依据高度
	ResizeWidth  string `json:"resize_width"`  // 图宽度,整数或者画布百分数
	ResizeHeight string `json:"resize_height"` // 图高度度,整数或者画布百分数

	//ActionType==round时候的字段
	RoundRadius string `json:"round_radius"` // 图的矩形圆角半径,整数或者百分数,如果是百分数,代表基于本图width的圆角半径

	//ActionType==circle时候的字段
	CircleOriginX string `json:"circle_origin_x"` // 图的原点x,整数或者百分数(百分数的话,是基于本图,而不是父图的),none代表不指定,由子图自计算
	CircleOriginY string `json:"circle_origin_y"` // 图的原点y,整数或者百分数
	CircleRadius  string `json:"circle_radius"`   // 图的半径,整数或者百分数

	//ActionType==location时候的字段
	LocationType      string `json:"location_type"`     // 定位类型,absolute|relative|mixed
	RelativeToImageID string `json:"sub_image_id"`      // 图id,location_type==relative(即相对定位)为同级图id
	AbsolutePosition  string `json:"absolute_position"` // 绝对定位时候的四部定位(顺时针方向:上右底左),每部可为整数或者百分数(none代表忽略定位),至少需要两维信息,举例:10 10 0 0=> 离父元素顶部10,右部10摆放子图
	RelativePosition  string `json:"relative_position"` // 相对定位时候的四部定位(顺时针方向:上右底左),每部可为整数或者百分数(none代表忽略定位),至少需要两维信息,举例:10 10 0 0=> 离定位元素顶部10,右部10摆放子图在画布中
}

type ImageBaseType

type ImageBaseType struct {
	// contains filtered or unexported fields
}

func (*ImageBaseType) DrawCircleImage

func (o *ImageBaseType) DrawCircleImage(originX, originY, radius int) (err error)

DrawRoundImage 操作-裁剪为圆

func (*ImageBaseType) DrawImage

func (o *ImageBaseType) DrawImage(sub image.Image, x, y int) (err error)

DrawImage 操作-在当前对象的(x,y)位置,叠着画上sub

func (*ImageBaseType) DrawOneLineText

func (o *ImageBaseType) DrawOneLineText(oneLineText string, tf *truetype.Font, dpi, fontSize float64, fontColor color.Color, x int, y int) (err error)

DrawOneLineText 操作-画文字(一行文字)

func (*ImageBaseType) DrawRoundImage

func (o *ImageBaseType) DrawRoundImage(radius float64) (err error)

DrawRoundImage 操作-将sub裁剪为圆角

func (*ImageBaseType) DrawSubImage

func (o *ImageBaseType) DrawSubImage(sub IImage, x, y int, debugColorInHex string) (err error)

DrawImage 操作-在当前对象的(x,y)位置,叠着画上sub

func (*ImageBaseType) GetImage

func (o *ImageBaseType) GetImage() (srdImg *image.RGBA)

GetImage 获取-*image.RGBA格式的实例

func (*ImageBaseType) GetSubImagePosition

func (o *ImageBaseType) GetSubImagePosition(subImageID string) (position [4]int, err error)

GetSubImagePosition 操作-获取子图的位置信息

func (*ImageBaseType) ID

func (o *ImageBaseType) ID() string

ID 获取id

func (*ImageBaseType) LoadFromBuffer

func (o *ImageBaseType) LoadFromBuffer(imgBuffer *bytes.Buffer) (err error)

LoadFromBuffer 加载-从buffer中加载

func (*ImageBaseType) LoadFromFile

func (o *ImageBaseType) LoadFromFile(imgFile string) (err error)

LoadFromFile 加载-从文件中加载

func (*ImageBaseType) LoadFromImageRGBA

func (o *ImageBaseType) LoadFromImageRGBA(srcImg *image.RGBA) (err error)

LoadFromImageRGBA 加载-设置当前实例为实例对象

func (*ImageBaseType) LoadFromNew

func (o *ImageBaseType) LoadFromNew(width, height int, fontColorInHex string) (err error)

LoadFromNew 加载-新建画板,fontColorInHex如果为空代表不需要底色

func (*ImageBaseType) LoadFromText

func (o *ImageBaseType) LoadFromText(word string, maxWidth int, fontFile string, dpi, fontSize float64, fontColorInHex, bgColorInHex string) (err error)

LoadFromText 加载-根据需要的文字创建一个图像

func (*ImageBaseType) LoadFromURL

func (o *ImageBaseType) LoadFromURL(imgURL string) (err error)

LoadFromURL 加载-从url中加载

func (*ImageBaseType) Resize

func (o *ImageBaseType) Resize(newWidth, newHeight uint) (err error)

Resize 操作-重新设置大小

func (*ImageBaseType) SaveToBuffer

func (o *ImageBaseType) SaveToBuffer() (imgBuffer *bytes.Buffer, err error)

SaveToBuffer 保存-图片为buffer

func (*ImageBaseType) SaveToJPEGFile

func (o *ImageBaseType) SaveToJPEGFile(imgFile string) (err error)

SaveToJPEGFile 保存-图片为jpeg或者jpg格式

func (*ImageBaseType) SaveToPNGFile

func (o *ImageBaseType) SaveToPNGFile(imgFile string) (err error)

SaveToPNGFile 保存-图片为png格式

func (*ImageBaseType) SaveToWEBPFile

func (o *ImageBaseType) SaveToWEBPFile(imgFile string) (err error)

SaveToWEBPFile 保存-图片为webp格式

func (*ImageBaseType) SetID

func (o *ImageBaseType) SetID(id string)

SetID 设置id

type ImageConfigInfoType

type ImageConfigInfoType struct {
	ID                    string                   `json:"id"`                      // id
	Name                  string                   `json:"name"`                    // 名称
	Desc                  string                   `json:"desc"`                    // 备注说明
	Enable                bool                     `json:"enable"`                  // 是否启用
	Debug                 bool                     `json:"debug"`                   // 是否开启调试,如果开启了,则会给子图加入调式色块,以便于辨识面积和位置
	PixelRatio            string                   `json:"pixel_ratio"`             // 设备像素比,一个浮点数
	Width                 string                   `json:"width"`                   // 画布宽度,数字
	Height                string                   `json:"height"`                  // 画布高度,数字
	CanvasBackgroundColor string                   `json:"canvas_background_color"` //画布背景颜色,(只支持#FFFFFF格式)
	OutputBufferType      string                   `json:"output_buffer_type"`      // 输出图片类型,png|jpeg|webp
	SubImageInfoList      []SubImageConfigInfoType `json:"sub_image_info_list"`     //子图列表
}

图像

type ImageJPGType

type ImageJPGType struct {
	ImageBaseType
}

func (*ImageJPGType) LoadFromBuffer

func (o *ImageJPGType) LoadFromBuffer(imgBuffer *bytes.Buffer) (err error)

LoadFromBuffer 加载-从buffer中加载

func (*ImageJPGType) LoadFromFile

func (o *ImageJPGType) LoadFromFile(imgFile string) (err error)

LoadFromFile 加载-从文件中加载

func (*ImageJPGType) SaveToBuffer

func (o *ImageJPGType) SaveToBuffer() (imgBuffer *bytes.Buffer, err error)

SaveToBuffer 保存-图片为buffer

type ImageMgrType

type ImageMgrType struct {
	sync.Mutex
	// contains filtered or unexported fields
}

ImageMgrType 实例定义

var (
	ImageMgrInstance *ImageMgrType
	ImageMgrOnce     sync.Once
)

实例

func ImageMgr

func ImageMgr() *ImageMgrType

ImageMgr 获取单例实例

func (*ImageMgrType) GenByImageConfig

func (mgr *ImageMgrType) GenByImageConfig(imageConfigInfo *ImageConfigInfoType) (imgBuffer *bytes.Buffer, err error)

GenPhoneImage 生成海报

func (*ImageMgrType) GetDebugColor

func (mgr *ImageMgrType) GetDebugColor(subImageID string) (debugColor string)

grLoop 默认goroutine

func (*ImageMgrType) Init

func (mgr *ImageMgrType) Init()

Init 初始化

type ImagePNGType

type ImagePNGType struct {
	ImageBaseType
}

func (*ImagePNGType) LoadFromBuffer

func (o *ImagePNGType) LoadFromBuffer(imgBuffer *bytes.Buffer) (err error)

LoadFromBuffer 加载-从buffer中加载

func (*ImagePNGType) LoadFromFile

func (o *ImagePNGType) LoadFromFile(imgFile string) (err error)

LoadFromFile 加载-从文件中加载

func (*ImagePNGType) SaveToBuffer

func (o *ImagePNGType) SaveToBuffer() (imgBuffer *bytes.Buffer, err error)

SaveToBuffer 保存-图片为buffer

type ImageWEBPType

type ImageWEBPType struct {
	ImageBaseType
}

func (*ImageWEBPType) LoadFromBuffer

func (o *ImageWEBPType) LoadFromBuffer(imgBuffer *bytes.Buffer) (err error)

LoadFromBuffer 加载-从buffer中加载

func (*ImageWEBPType) LoadFromFile

func (o *ImageWEBPType) LoadFromFile(imgFile string) (err error)

LoadFromFile 加载-从文件中加载

func (*ImageWEBPType) SaveToBuffer

func (o *ImageWEBPType) SaveToBuffer() (imgBuffer *bytes.Buffer, err error)

SaveToBuffer 保存-图片为buffer

type RoundMask

type RoundMask struct {
	// contains filtered or unexported fields
}

圆角效果

func (*RoundMask) At

func (m *RoundMask) At(x, y int) color.Color

func (*RoundMask) Bounds

func (m *RoundMask) Bounds() image.Rectangle

func (*RoundMask) ColorModel

func (m *RoundMask) ColorModel() color.Model

type SubImageConfigInfoType

type SubImageConfigInfoType struct {
	ID         string            `json:"id"`          // id
	Name       string            `json:"name"`        // 名称
	Desc       string            `json:"desc"`        // 备注说明
	Enable     bool              `json:"enable"`      // 是否启用
	ActionList []ImageActionType `json:"action_list"` // 动作说明

}

子图图像

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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