代码拉取完成,页面将自动刷新
/*
* Copyright (c) 2019-2022
* Author: LIU Xiangyu
* File: imageGenerator.go
*/
package media
import (
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"image"
"image/color"
"io/ioutil"
"os"
"time"
)
var font *truetype.Font
var FontFilePath = []string{"/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf", "/Library/Fonts/Arial Unicode.ttf"}
func ImageGen(width, height int, str string) image.Image {
i := image.NewAlpha(image.Rect(0, 0, width, height))
size := 20
for x := 0; x < size; x++ {
for y := 0; y < size; y++ {
i.Set(x, y, color.White)
}
for y := height - size; y < height; y++ {
i.Set(x, y, color.White)
}
}
for x := width - size; x < width; x++ {
for y := 0; y < size; y++ {
i.Set(x, y, color.White)
}
for y := height - size; y < height; y++ {
i.Set(x, y, color.White)
}
}
if font == nil {
for _, fpFont := range FontFilePath {
if _, err := os.Stat(fpFont); err != nil {
continue
}
fByte, _ := ioutil.ReadFile(fpFont)
font, _ = freetype.ParseFont(fByte)
break
}
}
fontContext := freetype.NewContext()
fontContext.SetDPI(72)
fontContext.SetFont(font)
fontContext.SetFontSize(40)
fontContext.SetClip(i.Bounds())
fontContext.SetDst(i)
fontContext.SetSrc(image.White)
//pt := freetype.Pt(width>>5, height>>1+int(fontContext.PointToFixed(40)>>8))
pt := freetype.Pt(size+10, 50)
_, _ = fontContext.DrawString(str, pt)
pt = freetype.Pt(width-300, 50)
_, _ = fontContext.DrawString("Emulator Mode", pt)
pt = freetype.Pt(size+10, height-10)
_, _ = fontContext.DrawString(time.Now().String(), pt)
return i
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。