Skip to content

Instantly share code, notes, and snippets.

@sikachu
Created December 22, 2009 04:55
Show Gist options
  • Save sikachu/261508 to your computer and use it in GitHub Desktop.
Save sikachu/261508 to your computer and use it in GitHub Desktop.
Generate 4 characters captcha based on given string
require 'rubygems'
require 'active_support'
numbers = "%04d" % rand(9999)
available_colors = %w(#FF0080 #0080FF #408000 #FF8000 #FF6666 #6666FF #FF6FCF #800080 #FFCC66 #0000FF #FF0000)
colors = available_colors.shuffle[0,5]
y_points = [rand(10) + 20, rand(20), rand(20) + 30, rand(10) + 25, rand(20) + 15, rand(20) + 20]
`convert -size 135x50 xc:white \
-fill "#{colors[0]}" -stroke "#{colors[0]}" -strokewidth 1 -family TimesNewRoman -pointsize #{rand(25) + 25} \
-draw "translate -45,0 rotate #{40 - rand(80)} skewX #{10 - rand(20)} gravity center text 0,0 '#{numbers[0,1]}'" \
-fill "#{colors[1]}" -stroke "#{colors[1]}" -pointsize #{rand(25) + 25} \
-draw "translate -15,0 rotate #{40 - rand(80)} skewX #{10 - rand(20)} gravity center text 0,0 '#{numbers[1,1]}'" \
-fill "#{colors[2]}" -stroke "#{colors[2]}" -pointsize #{rand(25) + 25} \
-draw "translate 15,0 rotate #{40 - rand(80)} skewX #{10 - rand(20)} gravity center text 0,0 '#{numbers[2,1]}'" \
-fill "#{colors[3]}" -stroke "#{colors[3]}" -pointsize #{rand(25) + 25} \
-draw "translate 45,0 rotate #{40 - rand(80)} skewX #{10 - rand(20)} gravity center text 0,0 '#{numbers[3,1]}'" \
-fill none -strokewidth 2 -stroke "#{colors[4]}" \
-draw "bezier 8,#{y_points[0]} 40,#{y_points[1]} 70,#{y_points[2]} 90,#{y_points[3]}" \
-draw "polyline 90,#{y_points[3]} 110,#{y_points[4]} 130,#{y_points[5]}" \
"test.png"
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment