Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item interface extension #115

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions interface_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package base64Captcha

import "io"

//Item is captcha item interface
// Item is captcha item interface
type Item interface {
//WriteTo writes to a writer
// WriteTo writes to a writer
WriteTo(w io.Writer) (n int64, err error)
//EncodeB64string encodes as base64 string
// EncodeBinary encodes as raw byte slice
EncodeBinary() []byte
// EncodeB64string encodes as base64 string
EncodeB64string() string
}
16 changes: 10 additions & 6 deletions item_audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"math/rand"
)

//ItemAudio captcha-audio-engine return type.
// ItemAudio captcha-audio-engine return type.
type ItemAudio struct {
answer string
body *bytes.Buffer
Expand Down Expand Up @@ -120,6 +120,14 @@ func (a *ItemAudio) makeWhiteNoise(length int, level uint8) []byte {
return noise
}

func (a *ItemAudio) EncodeBinary() []byte {
var buf bytes.Buffer
if _, err := a.WriteTo(&buf); err != nil {
panic(err)
}
return buf.Bytes()
}

// WriteTo writes captcha audio in WAVE format into the given io.Writer, and
// returns the number of bytes written and an error if any.
func (a *ItemAudio) WriteTo(w io.Writer) (n int64, err error) {
Expand Down Expand Up @@ -160,10 +168,6 @@ func (a *ItemAudio) WriteTo(w io.Writer) (n int64, err error) {

// EncodeB64string encodes a sound to base64 string
func (a *ItemAudio) EncodeB64string() string {
var buf bytes.Buffer
if _, err := a.WriteTo(&buf); err != nil {
panic(err)
}
return fmt.Sprintf("data:%s;base64,%s", MimeTypeAudio, base64.StdEncoding.EncodeToString(buf.Bytes()))
return fmt.Sprintf("data:%s;base64,%s", MimeTypeAudio, base64.StdEncoding.EncodeToString(a.EncodeBinary()))

}
27 changes: 14 additions & 13 deletions item_char.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"encoding/base64"
"errors"
"fmt"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"image"
"image/color"
"image/draw"
Expand All @@ -16,26 +13,30 @@ import (
"log"
"math"
"math/rand"

"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
)

//ItemChar captcha item of unicode characters
// ItemChar captcha item of unicode characters
type ItemChar struct {
bgColor color.Color
width int
height int
nrgba *image.NRGBA
}

//NewItemChar creates a captcha item of characters
// NewItemChar creates a captcha item of characters
func NewItemChar(w int, h int, bgColor color.RGBA) *ItemChar {
d := ItemChar{width: w, height: h}
m := image.NewNRGBA(image.Rect(0, 0, w, h))
draw.Draw(m, m.Bounds(), &image.Uniform{bgColor}, image.ZP, draw.Src)
draw.Draw(m, m.Bounds(), &image.Uniform{bgColor}, image.Point{}, draw.Src)
d.nrgba = m
return &d
}

//drawHollowLine draw strong and bold white line.
// drawHollowLine draw strong and bold white line.
func (item *ItemChar) drawHollowLine() *ItemChar {

first := item.width / 20
Expand Down Expand Up @@ -72,7 +73,7 @@ func (item *ItemChar) drawHollowLine() *ItemChar {
return item
}

//drawSineLine draw a sine line.
// drawSineLine draw a sine line.
func (item *ItemChar) drawSineLine() *ItemChar {
var py float64

Expand Down Expand Up @@ -116,7 +117,7 @@ func (item *ItemChar) drawSineLine() *ItemChar {
return item
}

//drawSlimLine draw n slim-random-color lines.
// drawSlimLine draw n slim-random-color lines.
func (item *ItemChar) drawSlimLine(num int) *ItemChar {

first := item.width / 10
Expand Down Expand Up @@ -231,8 +232,8 @@ func (item *ItemChar) drawText(text string, fonts []*truetype.Font) error {
return nil
}

//BinaryEncoding encodes an image to PNG and returns a byte slice.
func (item *ItemChar) BinaryEncoding() []byte {
// BinaryEncoding encodes an image to PNG and returns a byte slice.
func (item *ItemChar) EncodeBinary() []byte {
var buf bytes.Buffer
if err := png.Encode(&buf, item.nrgba); err != nil {
panic(err.Error())
Expand All @@ -243,13 +244,13 @@ func (item *ItemChar) BinaryEncoding() []byte {
// WriteTo writes captcha character in png format into the given io.Writer, and
// returns the number of bytes written and an error if any.
func (item *ItemChar) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write(item.BinaryEncoding())
n, err := w.Write(item.EncodeBinary())
return int64(n), err
}

// EncodeB64string encodes an image to base64 string
func (item *ItemChar) EncodeB64string() string {
return fmt.Sprintf("data:%s;base64,%s", MimeTypeImage, base64.StdEncoding.EncodeToString(item.BinaryEncoding()))
return fmt.Sprintf("data:%s;base64,%s", MimeTypeImage, base64.StdEncoding.EncodeToString(item.EncodeBinary()))
}

type point struct {
Expand Down
2 changes: 1 addition & 1 deletion item_char_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestItemChar_BinaryEncoding(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.item.BinaryEncoding(); !reflect.DeepEqual(got, tt.want) {
if got := tt.item.EncodeBinary(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ItemChar.BinaryEncoding() = %v, want %v", got, tt.want)
}
})
Expand Down