|
@@ -160,7 +160,7 @@ func GetActivityVoices(c *gin.Context) {
|
|
|
// @Accept json
|
|
|
// @Product json
|
|
|
// @Param activity_id query int true "活动ID"
|
|
|
-// @Success 200 {object} string "获取成功"
|
|
|
+// @Success 200 {string} string "获取成功"
|
|
|
// @failure 400 {string} string "获取失败"
|
|
|
// @Router /activity/getActivityShareImg [get]
|
|
|
func GetActivityShareImg(c *gin.Context) {
|
|
@@ -174,11 +174,15 @@ func GetActivityShareImg(c *gin.Context) {
|
|
|
if err != nil {
|
|
|
if err == utils.ErrNoRow {
|
|
|
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)
|
|
|
if err != nil {
|
|
|
response.Fail("读取封面图失败", c)
|
|
@@ -190,28 +194,27 @@ func GetActivityShareImg(c *gin.Context) {
|
|
|
response.Fail("读取封面图失败", c)
|
|
|
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: timeFormat,
|
|
|
- X: 60,
|
|
|
- Y: 250,
|
|
|
+ Text: drawText, X: x, Y: y,
|
|
|
}
|
|
|
- drawText = append(drawText, text)
|
|
|
+ drawInfo = append(drawInfo, text)
|
|
|
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 {
|
|
|
response.Fail("生成新封面图失败", c)
|
|
|
return
|
|
@@ -220,4 +223,27 @@ func GetActivityShareImg(c *gin.Context) {
|
|
|
c.Header("Content-Type", "image/png")
|
|
|
c.Header("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", "图片地址"))
|
|
|
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
|
|
|
}
|