image.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package captcha
  2. import (
  3. "image"
  4. "image/png"
  5. "io"
  6. "os"
  7. "rand"
  8. "time"
  9. )
  10. const (
  11. // Standard width and height for captcha image
  12. StdWidth = 300
  13. StdHeight = 80
  14. maxSkew = 2
  15. )
  16. type CaptchaImage struct {
  17. *image.NRGBA
  18. primaryColor image.NRGBAColor
  19. numWidth int
  20. numHeight int
  21. dotSize int
  22. }
  23. func init() {
  24. rand.Seed(time.Seconds())
  25. }
  26. // NewImage returns a new captcha image of the given width and height with the
  27. // given slice of numbers, where each number must be in range 0-9.
  28. func NewImage(numbers []byte, width, height int) *CaptchaImage {
  29. img := new(CaptchaImage)
  30. img.NRGBA = image.NewNRGBA(width, height)
  31. img.primaryColor = image.NRGBAColor{
  32. uint8(rand.Intn(129)),
  33. uint8(rand.Intn(129)),
  34. uint8(rand.Intn(129)),
  35. 0xFF,
  36. }
  37. // Calculate sizes
  38. var border int
  39. if width > height {
  40. border = height / 5
  41. } else {
  42. border = width / 5
  43. }
  44. bwidth := width - border*2
  45. bheight := height - border*2
  46. img.calculateSizes(bwidth, bheight, len(numbers))
  47. // Draw background (10 random circles of random brightness)
  48. img.fillWithCircles(10, img.dotSize)
  49. // Randomly position captcha within image
  50. maxx := width - (img.numWidth+img.dotSize)*len(numbers) - img.dotSize
  51. maxy := height - img.numHeight - img.dotSize*2
  52. x := rnd(img.dotSize*2, maxx)
  53. y := rnd(img.dotSize*2, maxy)
  54. // Draw numbers
  55. for _, n := range numbers {
  56. img.drawNumber(font[n], x, y)
  57. x += img.numWidth + img.dotSize
  58. }
  59. // Draw strike-through line
  60. img.strikeThrough()
  61. return img
  62. }
  63. // NewRandomImage generates a sequence of random numbers with the given length,
  64. // and returns a new captcha image of the given width and height with generated
  65. // numbers printed on it, and the sequence of numbers itself.
  66. func NewRandomImage(length, width, height int) (img *CaptchaImage, numbers []byte) {
  67. numbers = randomNumbers(length)
  68. img = NewImage(numbers, width, height)
  69. return
  70. }
  71. // PNGEncode writes captcha image in PNG format into the given writer.
  72. func (img *CaptchaImage) PNGEncode(w io.Writer) os.Error {
  73. return png.Encode(w, img)
  74. }
  75. func (img *CaptchaImage) calculateSizes(width, height, ncount int) {
  76. // Goal: fit all numbers inside the image.
  77. // Convert everything to floats for calculations.
  78. w := float64(width)
  79. h := float64(height)
  80. // fw takes into account 1-dot spacing between numbers
  81. fw := float64(fontWidth) + 1
  82. fh := float64(fontHeight)
  83. nc := float64(ncount)
  84. // Calculate the width of a single number taking into account only the
  85. // width of the image
  86. nw := w / nc
  87. // Calculate the height of a number from this width
  88. nh := nw * fh / fw
  89. // Number height too large?
  90. if nh > h {
  91. // Fit numbers based on height
  92. nh = h
  93. nw = fw / fh * nh
  94. }
  95. // Calculate dot size
  96. img.dotSize = int(nh / fh)
  97. // Save everything, making actual width smaller by 1 dot, to account
  98. // for spacing between numbers
  99. img.numWidth = int(nw)
  100. img.numHeight = int(nh) - img.dotSize
  101. }
  102. func (img *CaptchaImage) drawHorizLine(color image.Color, fromX, toX, y int) {
  103. for x := fromX; x <= toX; x++ {
  104. img.Set(x, y, color)
  105. }
  106. }
  107. func (img *CaptchaImage) drawCircle(color image.Color, x, y, radius int) {
  108. f := 1 - radius
  109. dfx := 1
  110. dfy := -2 * radius
  111. xx := 0
  112. yy := radius
  113. img.Set(x, y+radius, color)
  114. img.Set(x, y-radius, color)
  115. img.drawHorizLine(color, x-radius, x+radius, y)
  116. for xx < yy {
  117. if f >= 0 {
  118. yy--
  119. dfy += 2
  120. f += dfy
  121. }
  122. xx++
  123. dfx += 2
  124. f += dfx
  125. img.drawHorizLine(color, x-xx, x+xx, y+yy)
  126. img.drawHorizLine(color, x-xx, x+xx, y-yy)
  127. img.drawHorizLine(color, x-yy, x+yy, y+xx)
  128. img.drawHorizLine(color, x-yy, x+yy, y-xx)
  129. }
  130. }
  131. func (img *CaptchaImage) fillWithCircles(n, maxradius int) {
  132. color := img.primaryColor
  133. maxx := img.Bounds().Max.X
  134. maxy := img.Bounds().Max.Y
  135. for i := 0; i < n; i++ {
  136. setRandomBrightness(&color, 255)
  137. r := rnd(1, maxradius)
  138. img.drawCircle(color, rnd(r, maxx-r), rnd(r, maxy-r), r)
  139. }
  140. }
  141. func (img *CaptchaImage) strikeThrough() {
  142. r := 0
  143. maxx := img.Bounds().Max.X
  144. maxy := img.Bounds().Max.Y
  145. y := rnd(maxy/3, maxy-maxy/3)
  146. for x := 0; x < maxx; x += r {
  147. r = rnd(1, img.dotSize/2-1)
  148. y += rnd(-img.dotSize/2, img.dotSize/2)
  149. if y <= 0 || y >= maxy {
  150. y = rnd(maxy/3, maxy-maxy/3)
  151. }
  152. img.drawCircle(img.primaryColor, x, y, r)
  153. }
  154. }
  155. func (img *CaptchaImage) drawNumber(number []byte, x, y int) {
  156. skf := rand.Float64() * float64(rnd(-maxSkew, maxSkew))
  157. xs := float64(x)
  158. minr := img.dotSize / 2 // minumum radius
  159. maxr := img.dotSize/2 + img.dotSize/4 // maximum radius
  160. y += rnd(-minr, minr)
  161. for yy := 0; yy < fontHeight; yy++ {
  162. for xx := 0; xx < fontWidth; xx++ {
  163. if number[yy*fontWidth+xx] != blackChar {
  164. continue
  165. }
  166. // introduce random variations
  167. or := rnd(minr, maxr)
  168. ox := x + (xx * img.dotSize) + rnd(0, or/2)
  169. oy := y + (yy * img.dotSize) + rnd(0, or/2)
  170. img.drawCircle(img.primaryColor, ox, oy, or)
  171. }
  172. xs += skf
  173. x = int(xs)
  174. }
  175. }
  176. func setRandomBrightness(c *image.NRGBAColor, max uint8) {
  177. minc := min3(c.R, c.G, c.B)
  178. maxc := max3(c.R, c.G, c.B)
  179. if maxc > max {
  180. return
  181. }
  182. n := rand.Intn(int(max-maxc)) - int(minc)
  183. c.R = uint8(int(c.R) + n)
  184. c.G = uint8(int(c.G) + n)
  185. c.B = uint8(int(c.B) + n)
  186. }
  187. func min3(x, y, z uint8) (o uint8) {
  188. o = x
  189. if y < o {
  190. o = y
  191. }
  192. if z < o {
  193. o = z
  194. }
  195. return
  196. }
  197. func max3(x, y, z uint8) (o uint8) {
  198. o = x
  199. if y > o {
  200. o = y
  201. }
  202. if z > o {
  203. o = z
  204. }
  205. return
  206. }
  207. // rnd returns a random number in range [from, to].
  208. func rnd(from, to int) int {
  209. return rand.Intn(to+1-from) + from
  210. }