Browse Source

Merge branch 'master' into chart2.0

hsun 3 years ago
parent
commit
2fd9df592f
2 changed files with 87 additions and 6 deletions
  1. 23 3
      controller/activity/activity.go
  2. 64 3
      utils/drawtext.go

+ 23 - 3
controller/activity/activity.go

@@ -184,6 +184,23 @@ func GetActivityShareImg(c *gin.Context)  {
 		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()
+	var WeekDayMap = map[string]string{
+		"Monday":    "周一",
+		"Tuesday":   "周二",
+		"Wednesday": "周三",
+		"Thursday":  "周四",
+		"Friday":    "周五",
+		"Saturday":  "周六",
+		"Sunday":    "周日",
+	}
+	weekStr := WeekDayMap[activityWeek]
+	timeStr := activityDate + " " + activityStart + "-" + activityEnd + " " + weekStr
+
 	// 获取原分享图
 	originShareUrl := "static/img/report_share.png"
 	if activityInfo.FirstActivityTypeID == 3 {
@@ -214,13 +231,16 @@ func GetActivityShareImg(c *gin.Context)  {
 	// 生成新分享图
 	var drawInfo []*utils.DrawTextInfo
 	text := &utils.DrawTextInfo{
-		Text: drawText, X: x, Y: y,
+		Text: drawText, X: x, Y: y - 20, FontSize: fontSize,
+	}
+	textTime := &utils.DrawTextInfo{
+		Text: timeStr, X: 35, Y: y + 30, FontSize: 30,
 	}
-	drawInfo = append(drawInfo, text)
+	drawInfo = append(drawInfo, text, textTime)
 	var colorRGBA = utils.FontRGBA{
 		R: 255, G: 255, B: 255, A: 255,
 	}
-	picByte, err := utils.DrawStringOnImage(bytes, drawInfo, colorRGBA, float64(newSize), fontWidth)
+	picByte, err := utils.DrawStringRowsOnImage(bytes, drawInfo, colorRGBA, float64(newSize), fontWidth)
 	if err != nil {
 		response.Fail("生成新封面图失败", c)
 		return

+ 64 - 3
utils/drawtext.go

@@ -20,9 +20,10 @@ import (
 
 //DrawTextInfo 图片绘字信息
 type DrawTextInfo struct {
-	Text string
-	X    int
-	Y    int
+	Text 		string
+	X    		int
+	Y    		int
+	FontSize	int
 }
 
 //DrawRectInfo 图片画框信息
@@ -294,3 +295,63 @@ func DrawStringOnImage(imageData []byte, infos []*DrawTextInfo, colorRGBA FontRG
 
 	return
 }
+
+
+// DrawStringRowsOnImage 生成图片
+func DrawStringRowsOnImage(imageData []byte, infos []*DrawTextInfo, colorRGBA FontRGBA, fontSize float64, fontWidth int) (picBytes bytes.Buffer, err error) {
+	//判断图片类型
+	var backgroud image.Image
+	filetype := http.DetectContentType(imageData)
+	switch filetype {
+	case "image/jpeg", "image/jpg":
+		backgroud, err = jpeg.Decode(bytes.NewReader(imageData))
+		if err != nil {
+			fmt.Println("jpeg error")
+			return
+		}
+
+	case "image/gif":
+		backgroud, err = gif.Decode(bytes.NewReader(imageData))
+		if err != nil {
+			return
+		}
+
+	case "image/png":
+		backgroud, err = png.Decode(bytes.NewReader(imageData))
+		if err != nil {
+			return
+		}
+	default:
+		return
+	}
+	des := Image2RGBA(backgroud)
+
+	//新建笔刷
+	ttfPath := "static/ttf/songti.ttf"
+	textBrush, _ := NewTextBrush(ttfPath, fontSize, image.Black, fontWidth)
+
+	//Px Py 绘图开始坐标 text要绘制的文字
+	//调整颜色
+
+	for _, info := range infos {
+		c := freetype.NewContext()
+		c.SetDPI(72)
+		c.SetFont(textBrush.FontType)
+		c.SetHinting(font.HintingFull)
+		c.SetFontSize(float64(info.FontSize))
+		c.SetClip(des.Bounds())
+		c.SetDst(des)
+		textBrush.FontColor = image.NewUniform(color.RGBA{
+			R: colorRGBA.R,
+			G: colorRGBA.G,
+			B: colorRGBA.B,
+			A: colorRGBA.A,
+		})
+		c.SetSrc(textBrush.FontColor)
+		c.DrawString(info.Text, freetype.Pt(info.X, info.Y))
+	}
+
+	err = jpeg.Encode(&picBytes, des, nil)
+
+	return
+}