image.go 5.3 KB

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