Breaking News
Home / Webmaster / Build Website / How to create nosiy captcha using RMagick?

How to create nosiy captcha using RMagick?


 require 'Rmagick'
 
 class NoisyImage
   include Magick      
   
   attr_reader :code, :code_image
   Jiggle = 15
   Wobble = 15
   
   def initialize(len)
     chars = ('a'..'z').to_a - ['a','e','i','o','u']
     code_array=[]
     1.upto(len) {code_array << chars[rand(chars.length)]}
     granite = Magick::ImageList.new('granite:')
     canvas = Magick::ImageList.new
     canvas.new_image(32*len, 50, Magick::TextureFill.new(granite))
     text = Magick::Draw.new
     text.font_family = 'times'
     text.pointsize = 40
     cur = 10
     
     code_array.each{|c|
       rand(10) > 5 ? rot=rand(Wobble):rot= -rand(Wobble)
       rand(10) > 5 ? weight = NormalWeight : weight = BoldWeight
       text.annotate(canvas,0,0,cur,30+rand(Jiggle),c){
         self.rotation=rot
         self.font_weight = weight
         self.fill = 'darkred'
       }
       cur += 30
     }
     @code = code_array.to_s
     @code_image = canvas.to_blob{
       self.format="JPG"
     }
   end
   
 end
 

[pb_builder]

Check Also

What is the best day to blog by Social Shares?

Every since Social Websites like Twitter, Facebook have come mainstream for social sharing, it imperative …

Advertisment ad adsense adlogger