瀏覽代碼

fix: 分享图写入文字

hsun 3 年之前
父節點
當前提交
13965652a3
共有 4 個文件被更改,包括 48 次插入22 次删除
  1. 46 20
      controller/activity/activity.go
  2. 二進制
      static/img/report_share.png
  3. 二進制
      static/img/report_share_sl.png
  4. 2 2
      utils/drawtext.go

+ 46 - 20
controller/activity/activity.go

@@ -160,7 +160,7 @@ func GetActivityVoices(c *gin.Context) {
 // @Accept  json
 // @Accept  json
 // @Product json
 // @Product json
 // @Param activity_id query int true "活动ID"
 // @Param activity_id query int true "活动ID"
-// @Success 200 {object} string "获取成功"
+// @Success 200 {string} string "获取成功"
 // @failure 400 {string} string "获取失败"
 // @failure 400 {string} string "获取失败"
 // @Router /activity/getActivityShareImg [get]
 // @Router /activity/getActivityShareImg [get]
 func GetActivityShareImg(c *gin.Context)  {
 func GetActivityShareImg(c *gin.Context)  {
@@ -174,11 +174,15 @@ func GetActivityShareImg(c *gin.Context)  {
 	if err != nil {
 	if err != nil {
 		if err == utils.ErrNoRow {
 		if err == utils.ErrNoRow {
 			response.Fail("获取活动信息失败", c)
 			response.Fail("获取活动信息失败", c)
-			return
 		}
 		}
+		return
 	}
 	}
+
 	// 获取原分享图
 	// 获取原分享图
-	originShareUrl := "static/img/reportdetail_share.png"
+	originShareUrl := "static/img/report_share.png"
+	if activityInfo.FirstActivityTypeID == 3 {
+		originShareUrl = "static/img/report_share_sl.png"
+	}
 	fp, err := os.OpenFile(originShareUrl, os.O_CREATE|os.O_APPEND, 6)
 	fp, err := os.OpenFile(originShareUrl, os.O_CREATE|os.O_APPEND, 6)
 	if err != nil {
 	if err != nil {
 		response.Fail("读取封面图失败", c)
 		response.Fail("读取封面图失败", c)
@@ -190,28 +194,27 @@ func GetActivityShareImg(c *gin.Context)  {
 		response.Fail("读取封面图失败", c)
 		response.Fail("读取封面图失败", c)
 		return
 		return
 	}
 	}
-	// 时间处理
-	activityDate := activityInfo.StartTime.Format("2006-01-02")
-	activityStart := activityInfo.StartTime.Format("15:04")
-	activityEnd := activityInfo.EndTime.Format("15:04")
-	activityWeek := activityInfo.StartTime.Weekday().String()
-	week := utils.StrDateTimeToWeek(activityWeek)
-	timeFormat := activityDate + " " + activityStart + "-" + activityEnd + " " + week
+
+	// 处理文字x轴y轴点
+	drawText := activityInfo.ActivityTypeName
+	fontSize := 40
+	fontWidth := 50
+	x, y, newSize, err := calcuDrawXandY(drawText, fontSize, 500, 400)
+	if err != nil {
+		response.Fail("生成新封面图失败", c)
+		return
+	}
+
 	// 生成新分享图
 	// 生成新分享图
-	var drawText []*utils.DrawTextInfo
+	var drawInfo []*utils.DrawTextInfo
 	text := &utils.DrawTextInfo{
 	text := &utils.DrawTextInfo{
-		Text: timeFormat,
-		X: 60,
-		Y: 250,
+		Text: drawText, X: x, Y: y,
 	}
 	}
-	drawText = append(drawText, text)
+	drawInfo = append(drawInfo, text)
 	var colorRGBA = utils.FontRGBA{
 	var colorRGBA = utils.FontRGBA{
-		R: 223,
-		G: 197,
-		B: 143,
-		A: 255,
+		R: 255, G: 255, B: 255, A: 255,
 	}
 	}
-	picByte, err := utils.DrawStringOnImage(bytes, drawText, colorRGBA)
+	picByte, err := utils.DrawStringOnImage(bytes, drawInfo, colorRGBA, float64(newSize), fontWidth)
 	if err != nil {
 	if err != nil {
 		response.Fail("生成新封面图失败", c)
 		response.Fail("生成新封面图失败", c)
 		return
 		return
@@ -220,4 +223,27 @@ func GetActivityShareImg(c *gin.Context)  {
 	c.Header("Content-Type", "image/png")
 	c.Header("Content-Type", "image/png")
 	c.Header("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", "图片地址"))
 	c.Header("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", "图片地址"))
 	c.Writer.WriteString(picByte.String())
 	c.Writer.WriteString(picByte.String())
+}
+
+// calcuDrawXandY 计算写入文字的x轴和y轴
+func calcuDrawXandY(drawText string, fontSize, canvasLength, canvasWidth int) (x, y, newSize int, err error) {
+	// y轴取画布宽度的一半
+	y = canvasWidth / 2
+	// 文字所占总宽度
+	lenText := len(drawText)
+	textUnit := float64(lenText) / 3
+	textWidth := int(textUnit * float64(fontSize))
+	// x轴上的起点位置
+	newSize = fontSize
+	doubleX := canvasLength - textWidth
+	if doubleX < 50 {
+		// 文字总宽太长则调整文字大小
+		maxTextWidth := float64(canvasLength - 50)
+		newSize = int(maxTextWidth / textUnit)
+		x = 25
+	} else {
+		x = doubleX / 2
+	}
+
+	return
 }
 }

二進制
static/img/report_share.png


二進制
static/img/report_share_sl.png


+ 2 - 2
utils/drawtext.go

@@ -237,7 +237,7 @@ func DrawRectOnImageAndSave(imagePath string, imageData []byte, infos []*DrawRec
 }
 }
 
 
 //DrawStringOnImage 生成图片
 //DrawStringOnImage 生成图片
-func DrawStringOnImage(imageData []byte, infos []*DrawTextInfo, colorRGBA FontRGBA) (picBytes bytes.Buffer, err error) {
+func DrawStringOnImage(imageData []byte, infos []*DrawTextInfo, colorRGBA FontRGBA, fontSize float64, fontWidth int) (picBytes bytes.Buffer, err error) {
 	//判断图片类型
 	//判断图片类型
 	var backgroud image.Image
 	var backgroud image.Image
 	filetype := http.DetectContentType(imageData)
 	filetype := http.DetectContentType(imageData)
@@ -267,7 +267,7 @@ func DrawStringOnImage(imageData []byte, infos []*DrawTextInfo, colorRGBA FontRG
 
 
 	//新建笔刷
 	//新建笔刷
 	ttfPath := "static/ttf/songti.ttf"
 	ttfPath := "static/ttf/songti.ttf"
-	textBrush, _ := NewTextBrush(ttfPath, 25, image.Black, 50)
+	textBrush, _ := NewTextBrush(ttfPath, fontSize, image.Black, fontWidth)
 
 
 	//Px Py 绘图开始坐标 text要绘制的文字
 	//Px Py 绘图开始坐标 text要绘制的文字
 	//调整颜色
 	//调整颜色