Esempio n. 1
0
// Show is the handler for showing a stored QR code.
func Show(w http.ResponseWriter, req *http.Request) {
	ctxt := fs.NewContext(req)
	tag := req.URL.Path[len("/qr/show/"):]
	png := strings.HasSuffix(tag, ".png")
	if png {
		tag = tag[:len(tag)-len(".png")]
	}
	if !isTagName(tag) {
		fmt.Fprintf(w, "Sorry, QR code not found\n")
		return
	}
	if req.FormValue("flag") == "1" {
		flag(w, req, tag, ctxt)
		return
	}
	data, _, err := ctxt.Read("qrsave/" + tag)
	if err != nil {
		fmt.Fprintf(w, "Sorry, QR code not found.\n")
		return
	}

	var m Image
	if err := json.Unmarshal(data, &m); err != nil {
		panic(err)
	}
	m.Tag = tag

	switch req.FormValue("size") {
	case "big":
		m.Scale *= 2
	case "small":
		m.Scale /= 2
	}

	if png {
		if err := m.Encode(req); err != nil {
			panic(err)
			return
		}
		w.Header().Set("Cache-Control", "public, max-age=3600")
		w.Write(m.Code.PNG())
		return
	}

	w.Header().Set("Cache-Control", "public, max-age=300")
	runTemplate(ctxt, w, "qr/permalink.html", &m)
}
Esempio n. 2
0
func upload(w http.ResponseWriter, req *http.Request, link string) {
	// Upload of a new image.
	// Copied from Moustachio demo.
	f, _, err := req.FormFile("image")
	if err != nil {
		fmt.Fprintf(w, "You need to select an image to upload.\n")
		return
	}
	defer f.Close()

	i, _, err := image.Decode(f)
	if err != nil {
		panic(err)
	}

	// Convert image to 128x128 gray+alpha.
	b := i.Bounds()
	const max = 128
	// If it's gigantic, it's more efficient to downsample first
	// and then resize; resizing will smooth out the roughness.
	var i1 *image.RGBA
	if b.Dx() > 4*max || b.Dy() > 4*max {
		w, h := 2*max, 2*max
		if b.Dx() > b.Dy() {
			h = b.Dy() * h / b.Dx()
		} else {
			w = b.Dx() * w / b.Dy()
		}
		i1 = resize.Resample(i, b, w, h)
	} else {
		// "Resample" to same size, just to convert to RGBA.
		i1 = resize.Resample(i, b, b.Dx(), b.Dy())
	}
	b = i1.Bounds()

	// Encode to PNG.
	dx, dy := 128, 128
	if b.Dx() > b.Dy() {
		dy = b.Dy() * dx / b.Dx()
	} else {
		dx = b.Dx() * dy / b.Dy()
	}
	i128 := resize.ResizeRGBA(i1, i1.Bounds(), dx, dy)

	var buf bytes.Buffer
	if err := png.Encode(&buf, i128); err != nil {
		panic(err)
	}

	h := md5.New()
	h.Write(buf.Bytes())
	tag := fmt.Sprintf("%x", h.Sum(nil))[:32]

	ctxt := fs.NewContext(req)
	if err := ctxt.Write("qr/upload/"+tag+".png", buf.Bytes()); err != nil {
		panic(err)
	}

	// Redirect with new image tag.
	// Redirect to draw with new image tag.
	http.Redirect(w, req, req.URL.Path+"?"+url.Values{"i": {tag}, "url": {link}}.Encode(), 302)
}
Esempio n. 3
0
// Draw is the handler for drawing a QR code.
func Draw(w http.ResponseWriter, req *http.Request) {
	ctxt := fs.NewContext(req)

	url := req.FormValue("url")
	if url == "" {
		url = "http://swtch.com/qr"
	}
	if req.FormValue("upload") == "1" {
		upload(w, req, url)
		return
	}

	t0 := time.Now()
	img := req.FormValue("i")
	if !isImgName(img) {
		img = "pjw"
	}
	if req.FormValue("show") == "png" {
		i := loadSize(ctxt, img, 48)
		var buf bytes.Buffer
		png.Encode(&buf, i)
		w.Write(buf.Bytes())
		return
	}
	if req.FormValue("flag") == "1" {
		flag(w, req, img, ctxt)
		return
	}
	if req.FormValue("x") == "" {
		var data = struct {
			Name string
			URL  string
		}{
			Name: img,
			URL:  url,
		}
		runTemplate(ctxt, w, "qr/main.html", &data)
		return
	}

	arg := func(s string) int { x, _ := strconv.Atoi(req.FormValue(s)); return x }
	targ := makeTarg(ctxt, img, 17+4*arg("v")+arg("z"))

	m := &Image{
		Name:         img,
		Dx:           arg("x"),
		Dy:           arg("y"),
		URL:          req.FormValue("u"),
		Version:      arg("v"),
		Mask:         arg("m"),
		RandControl:  arg("r") > 0,
		Dither:       arg("i") > 0,
		OnlyDataBits: arg("d") > 0,
		SaveControl:  arg("c") > 0,
		Scale:        arg("scale"),
		Target:       targ,
		Seed:         int64(arg("s")),
		Rotation:     arg("o"),
		Size:         arg("z"),
	}
	if m.Version > 8 {
		m.Version = 8
	}

	if m.Scale == 0 {
		if arg("l") > 1 {
			m.Scale = 8
		} else {
			m.Scale = 4
		}
	}
	if m.Version >= 12 && m.Scale >= 4 {
		m.Scale /= 2
	}

	if arg("l") == 1 {
		data, err := json.Marshal(m)
		if err != nil {
			panic(err)
		}
		h := md5.New()
		h.Write(data)
		tag := fmt.Sprintf("%x", h.Sum(nil))[:16]
		if err := ctxt.Write("qrsave/"+tag, data); err != nil {
			panic(err)
		}
		http.Redirect(w, req, "/qr/show/"+tag, http.StatusTemporaryRedirect)
		return
	}

	if err := m.Encode(req); err != nil {
		fmt.Fprintf(w, "%s\n", err)
		return
	}

	var dat []byte
	switch {
	case m.SaveControl:
		dat = m.Control
	default:
		dat = m.Code.PNG()
	}

	if arg("l") > 0 {
		w.Header().Set("Content-Type", "image/png")
		w.Write(dat)
		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	fmt.Fprint(w, "<center><img src=\"data:image/png;base64,")
	io.WriteString(w, base64.StdEncoding.EncodeToString(dat))
	fmt.Fprint(w, "\" /><br>")
	fmt.Fprintf(w, "<form method=\"POST\" action=\"%s&l=1\"><input type=\"submit\" value=\"Save this QR code\"></form>\n", m.Link())
	fmt.Fprintf(w, "</center>\n")
	fmt.Fprintf(w, "<br><center><font size=-1>%v</font></center>\n", time.Now().Sub(t0))
}
Esempio n. 4
-1
func makeImage(req *http.Request, caption, font string, pt, size, border, scale int, f func(x, y int) uint32) *image.RGBA {
	d := (size + 2*border) * scale
	csize := 0
	if caption != "" {
		if pt == 0 {
			pt = 11
		}
		csize = pt * 2
	}
	c := image.NewRGBA(image.Rect(0, 0, d, d+csize))

	// white
	u := &image.Uniform{C: color.White}
	draw.Draw(c, c.Bounds(), u, image.ZP, draw.Src)

	for y := 0; y < size; y++ {
		for x := 0; x < size; x++ {
			r := image.Rect((x+border)*scale, (y+border)*scale, (x+border+1)*scale, (y+border+1)*scale)
			rgba := f(x, y)
			u.C = color.RGBA{byte(rgba >> 24), byte(rgba >> 16), byte(rgba >> 8), byte(rgba)}
			draw.Draw(c, r, u, image.ZP, draw.Src)
		}
	}

	if csize != 0 {
		if font == "" {
			font = "data/luxisr.ttf"
		}
		ctxt := fs.NewContext(req)
		dat, _, err := ctxt.Read(font)
		if err != nil {
			panic(err)
		}
		tfont, err := freetype.ParseFont(dat)
		if err != nil {
			panic(err)
		}
		ft := freetype.NewContext()
		ft.SetDst(c)
		ft.SetDPI(100)
		ft.SetFont(tfont)
		ft.SetFontSize(float64(pt))
		ft.SetSrc(image.NewUniform(color.Black))
		ft.SetClip(image.Rect(0, 0, 0, 0))
		wid, err := ft.DrawString(caption, freetype.Pt(0, 0))
		if err != nil {
			panic(err)
		}
		p := freetype.Pt(d, d+3*pt/2)
		p.X -= wid.X
		p.X /= 2
		ft.SetClip(c.Bounds())
		ft.DrawString(caption, p)
	}

	return c
}