images.Text
语法
images.Text TEXT [OPTIONS]
返回
images.filter
选项
尽管没有要求任何选项,但至少您需要将 size
设置为图像高度的某个合理百分比。
- color
- (
string
)字体颜色,可以是 3 位或 6 位十六进制颜色代码。默认值为#ffffff
(白色)。 - font
- (
resource.Resource
)字体可以是全局资源、页面资源或远程资源。默认值为 Go Regular,一种比例无衬线 TrueType 字体。
- linespacing
- (
int
)每行之间的像素数。对于 1.4 的行高,请将linespacing
设置为size
乘以 0.4。默认值为2
。 - size
- (
int
)字体大小,以像素为单位。默认值为20
。 - x
- (
int
)相对于图像左侧的水平偏移量,以像素为单位。默认值为10
。 - y
- (
int
)相对于图像顶部的垂直偏移量,以像素为单位。默认值为10
。 - alignx
- v0.141.0 中的新功能
- (
string
)文本相对于x
位置的水平对齐方式。可以是left
、center
或right
之一。默认值为left
。
用法
将字体捕获为资源
{{ $font := "" }}
{{ $path := "https://github.com/google/fonts/raw/main/ofl/lato/Lato-Regular.ttf" }}
{{ with try (resources.GetRemote $path) }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else with .Value }}
{{ $font = . }}
{{ else }}
{{ errorf "Unable to get resource %q" $path }}
{{ end }}
{{ end }}
创建选项映射
{{ $opts := dict
"color" "#fbfaf5"
"font" $font
"linespacing" 8
"size" 40
"x" 25
"y" 190
}}
设置文本
{{ $text := "Zion National Park" }}
创建过滤器
{{ $filter := images.Text $text $opts }}
使用 images.Filter
函数应用过滤器
{{ with resources.Get "images/original.jpg" }}
{{ with . | images.Filter $filter }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
{{ end }}
您还可以使用 Resource
对象上的 Filter
方法应用过滤器
{{ with resources.Get "images/original.jpg" }}
{{ with .Filter $filter }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
{{ end }}
示例
原始

已处理
