Commit 6f8f1df6 authored by santiaago's avatar santiaago
Browse files

add Width and Height params to /themes/:theme endpoint.

parent de9774f7
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@ import (
// the theme is defined by keyword :theme
// url: "/themes/:theme"
func Theme(w http.ResponseWriter, r *http.Request) {

	var err error
	var th string
	if th, _ = route.Context.Get(r, "theme"); err != nil {
@@ -32,14 +31,16 @@ func Theme(w http.ResponseWriter, r *http.Request) {
		theme = colorMap["base"]
	}

	width := extract.WidthOrDefault(r, 20*len(theme))
	height := extract.HeightOrDefault(r, 34)

	if f := extract.Format(r); f == format.JPEG {
		m := image.NewRGBA(image.Rect(0, 0, 20*len(theme), 34))
		m := image.NewRGBA(image.Rect(0, 0, width, height))
		squares.Palette(m, theme)
		var img image.Image = m
		write.ImageJPEG(w, &img)
	} else if f == format.SVG {
		write.ImageSVG(w)
		squares.PaletteSVG(w, theme, 20*len(theme), 34)
		squares.PaletteSVG(w, theme, width, height)
	}

}
+28 −0
Original line number Diff line number Diff line
@@ -159,6 +159,20 @@ func Width(r *http.Request) int {
	return 720
}

// WidthOrDefault returns the value of the 'w' parameter in the http.Request.
func WidthOrDefault(r *http.Request, v int) int {
	strW := r.FormValue("w")
	if len(strW) > 0 {
		if w, errW := strconv.ParseInt(strW, 0, 64); errW == nil {
			iw := int(w)
			if iw > 0 {
				return iw
			}
		}
	}
	return v
}

// Height returns the value of the 'h' parameter in the http.Request.
func Height(r *http.Request) int {
	strH := r.FormValue("h")
@@ -173,6 +187,20 @@ func Height(r *http.Request) int {
	return 300
}

// HeightOrDefault returns the value of the 'h' parameter in the http.Request.
func HeightOrDefault(r *http.Request, v int) int {
	strH := r.FormValue("h")
	if len(strH) > 0 {
		if h, errH := strconv.ParseInt(strH, 0, 64); errH == nil {
			ih := int(h)
			if ih > 0 {
				return ih
			}
		}
	}
	return v
}

// XSquares returns the value of the 'xs' parameter in the http.Request.
// Used to defined the number of squares that are wanted in the x axis of an image
func XSquares(r *http.Request) int {