Re: [問題] 驗證圖片的產生

看板Ruby作者 (台中喵小咩)時間18年前 (2007/09/08 12:13), 編輯推噓3(300)
留言3則, 2人參與, 最新討論串3/3 (看更多)
最後還是自己寫了一個...因為那些套件感覺都不好用 我是用 GD2 去畫..所以要先安裝 gem gd2 def captcha # 圖寬 width = 180 # 圖高 height = 60 # 背景的雜訊數量 noise = 50 # 驗證碼 code = 'ABCD' # 建立新畫布 img = GD2::Image.new(width, height) # 塗滿背景色(白色) img.draw { |canvas| canvas.color = GD2::Color[255, 255, 255] canvas.rectangle(0, 0, img.width - 1, img.height - 1, true) } # 畫星星雜訊 noise.times { |i| img.draw { |canvas| canvas.move_to(rand(width-1), rand(height-1)) canvas.color = GD2::Color[150+rand(105), 150+rand(105), 150+rand(105)] canvas.font = GD2::Font::TrueType.new('cour.ttf', 20) canvas.text('* *', rand(10)) } } # 畫驗證碼(亂數顏色,亂數位置,亂數角度) clen = code.length clen.times do |i| img.draw { |canvas| fw = (width - 20) / clen canvas.move_to(10+(i*fw+rand(7)), rand((height-30)/2)+30) canvas.color = GD2::Color[50+rand(150), 50+rand(150), 50+rand(150)] canvas.font = GD2::Font::TrueType.new('CENTURY.TTF', 30) canvas.text("#{code[i,1]}", (rand(10).to_f/10.0)-0.5) } end # 輸出成 JPEG send_data img.jpeg(40), :type => 'image/jpeg', :disposition => 'inline' end -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.225.163.112 ※ 編輯: shelary 來自: 61.225.163.112 (09/08 12:15)

09/08 13:10, , 1F
XD
09/08 13:10, 1F

09/08 16:34, , 2F
差個題外話...有人知道怎麼讀取圖片的長跟寬嗎?
09/08 16:34, 2F

09/08 16:40, , 3F
查手冊吧,我知道 RMagick 可以
09/08 16:40, 3F
文章代碼(AID): #16uY6779 (Ruby)
文章代碼(AID): #16uY6779 (Ruby)