瀏覽代碼

fix: 分享图片文字宽度问题

hsun 3 年之前
父節點
當前提交
f3db3864e6
共有 1 個文件被更改,包括 12 次插入2 次删除
  1. 12 2
      controller/activity/activity.go

+ 12 - 2
controller/activity/activity.go

@@ -11,6 +11,7 @@ import (
 	"io/ioutil"
 	"os"
 	"strconv"
+	"strings"
 	"time"
 )
 
@@ -255,8 +256,17 @@ func GetActivityShareImg(c *gin.Context)  {
 func calcuDrawXandY(drawText string, fontSize, canvasLength, canvasWidth int) (x, y, newSize int, err error) {
 	// y轴取画布宽度的一半
 	y = canvasWidth / 2
-	// 文字所占总宽度
-	lenText := len(drawText)
+	// 拆分文字计算总宽度
+	lenText := 0
+	strArr := strings.Split(drawText, "")
+	for _, str := range strArr {
+		if len(str) == 1 {
+			// 将字母符号算作2个长度
+			lenText += 2
+		} else {
+			lenText += 3
+		}
+	}
 	textUnit := float64(lenText) / 3
 	textWidth := int(textUnit * float64(fontSize))
 	// x轴上的起点位置