all repos — captcha @ cede256342ab8b650f6708edceffd8830cd531c0

Go package captcha implements generation and verification of image and audio CAPTCHAs.

Fix image.WriteTo, properly implementing interface.

Instead of writing directly via png.Encode, encode image
into a buffer first, then write the whole buffer.

~~~
This commit is brought to you by:

http://blog.oleganza.com
Must-have source of knowledge about Bitcoin.
~~~

(Sponsor my commits! https://github.com/dchest/commit-ads)
Dmitry Chestnykh dmitry@codingrobots.com
Tue, 03 Dec 2013 12:17:47 +0100
commit

cede256342ab8b650f6708edceffd8830cd531c0

parent

9ee08184699a934586df81a2c781378657d6a281

2 files changed, 12 insertions(+), 12 deletions(-)

jump to
M README.mdREADME.md

@@ -273,11 +273,3 @@

NewMemoryStore returns a new standard memory store for captchas with the given collection threshold and expiration time in seconds. The returned store must be registered with SetCustomStore to replace the default one. - - -Bugs ----- - -* While Image conforms to io.WriterTo interface, its WriteTo -method returns 0 instead of the actual bytes written because png.Encode -doesn't report this.
M image.goimage.go

@@ -5,6 +5,7 @@

package captcha import ( + "bytes" "image" "image/color" "image/png"

@@ -80,13 +81,20 @@ m.fillWithCircles(circleCount, m.dotSize)

return m } -// BUG(dchest): While Image conforms to io.WriterTo interface, its WriteTo -// method returns 0 instead of the actual bytes written because png.Encode -// doesn't report this. +// encodeToPNG encodes an image to PNG and returns +// the result as a byte slice. +func (m *Image) encodedPNG() []byte { + var buf bytes.Buffer + if err := png.Encode(&buf, m.Paletted); err != nil { + panic(err.Error()) + } + return buf.Bytes() +} // WriteTo writes captcha image in PNG format into the given writer. func (m *Image) WriteTo(w io.Writer) (int64, error) { - return 0, png.Encode(w, m.Paletted) + n, err := w.Write(m.encodedPNG()) + return int64(n), err } func (m *Image) calculateSizes(width, height, ncount int) {