package handler import ( "encoding/json" "github.com/fogleman/gg" "github.com/golang/freetype" "github.com/golang/freetype/truetype" "github.com/gorilla/mux" "golang.org/x/image/font" "golang.org/x/image/font/gofont/goregular" "golang.org/x/image/math/fixed" "html/template" "image" "image/color" "image/draw" "strconv" //"image/png" "net/http" ) func (h *Handler) showUserView(w http.ResponseWriter, r *http.Request) { logged, _ := h.sess.Get(r) var page int64 = 0 if val, ok := r.URL.Query()["page"]; ok && len(val[0]) == 1 { page, _ = strconv.ParseInt(val[0], 10, 64) } username := mux.Vars(r)["user"] user, err := h.storage.UserByName(username) if err != nil { notFound(w) return } statuses, showMore, err := h.storage.StatusByUsername(mux.Vars(r)["user"], 20, page) if err != nil { serverError(w, err) return } h.renderLayout(w, "user", map[string]interface{}{ "user": username, "statuses": statuses, "homepage": user.Homepage, "about": template.HTML(user.About), "picture": user.Picture, "style": template.CSS(user.Style), "showMore": showMore, "page": page, "next_page": page + 1, "prev_page": page - 1, }, logged) } type statusjson struct { Author string `json:"author"` Content string `json:"content"` TimeAgo string `json:"timeAgo"` } func (h *Handler) showUserStatusView(w http.ResponseWriter, r *http.Request) { user := mux.Vars(r)["user"] if !h.storage.UserExists(user) { notFound(w) return } statuses, _, err := h.storage.StatusByUsername(mux.Vars(r)["user"], 1, 0) if err != nil { serverError(w, err) return } w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Content-Type", "application/json") var res statusjson if len(statuses) > 0 { res.Author = statuses[0].User res.Content = statuses[0].Content res.TimeAgo = statuses[0].TimeAgo() } json.NewEncoder(w).Encode(res) } func drawText(canvas *image.RGBA, text string) error { var ( fgColor image.Image fontFace *truetype.Font err error fontSize = 12.0 ) fgColor = image.White fontFace, err = freetype.ParseFont(goregular.TTF) fontDrawer := &font.Drawer{ Dst: canvas, Src: fgColor, Face: truetype.NewFace(fontFace, &truetype.Options{ Size: fontSize, Hinting: font.HintingFull, }), } //textBounds, _ := fontDrawer.BoundString(text) xPosition := fixed.I(20) //textHeight := textBounds.Max.Y - textBounds.Min.Y yPosition := fixed.I(20) fontDrawer.Dot = fixed.Point26_6{ X: xPosition, Y: yPosition, } fontDrawer.DrawString(text) return err } func createAvatar(text string) *image.RGBA { bgColor := color.RGBA{ R: 255, G: 20, B: 100, A: 255, } background := image.NewRGBA(image.Rect(0, 0, 200, 100)) draw.Draw(background, background.Bounds(), &image.Uniform{C: bgColor}, image.Point{}, draw.Src) drawText(background, text) return background } func (h *Handler) showUserStatusImageView(w http.ResponseWriter, r *http.Request) { user := mux.Vars(r)["user"] if !h.storage.UserExists(user) { notFound(w) return } statuses, _, err := h.storage.StatusByUsername(mux.Vars(r)["user"], 1, 0) if err != nil { serverError(w, err) return } //w.Header().Set("Access-Control-Allow-Origin", "*") //w.Header().Set("Content-Type", "application/json") var text string if len(statuses) > 0 { text = statuses[0].Content } background := "#ffffff" foreground := "#000000" if c := r.URL.Query().Get("background"); c != "" { background = "#" + c } if c := r.URL.Query().Get("foreground"); c != "" { foreground = "#" + c } width := 350 dc := gg.NewContext(width, 84) dc.SetHexColor(background) dc.Clear() dc.SetHexColor(foreground) dc.DrawStringWrapped(text, 8, 4, 0, 0, float64(width-(2*16)), 1.5, gg.AlignLeft) dc.EncodePNG(w) //dc.SavePNG("out.png") //avatar := createAvatar(text) //png.Encode(w, avatar) }