示例#1
0
func serveImage(w http.ResponseWriter, r *http.Request) {

	params := mux.Vars(r)
	r.ParseForm()
	text := r.FormValue("text")
	width, _ := strconv.ParseInt(params["width"], 10, 32)
	height, _ := strconv.ParseInt(params["height"], 10, 32)
	// FIXME err handling

	if width > 4000 || height > 4000 {
		w.Header().Set("Content-Type", "text/html")
		w.Write([]byte("image too large"))
		return
	}

	img, err := goplaceholder.Placeholder(
		text,
		"/usr/share/fonts/TTF/DejaVuSans-Bold.ttf",
		color.RGBA{150, 150, 150, 255},
		color.RGBA{204, 204, 204, 255},
		int(width), int(height))

	if err != nil {
		log.Println(err)
		return
	}

	w.Header().Set("Content-Type", "image/png")
	png.Encode(w, img)
	log.Printf("served image: w: %d h: %d\n", width, height)
}
示例#2
0
func main() {

	width := flag.Int("width", 0, "image width")
	height := flag.Int("height", 0, "image height")
	text := flag.String("text", "", "optional text")

	flag.Parse()

	font := "/usr/share/fonts/TTF/DejaVuSans-Bold.ttf"
	fg := color.RGBA{150, 150, 150, 255}
	bg := color.RGBA{204, 204, 204, 255}

	img, err := goplaceholder.Placeholder(*text, font, fg, bg, *width, *height)

	if err != nil {
		log.Fatal(err)
	}

	writeImageToPng(&img, "./test.png")
}