module
require 'rubygems'
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('xc:#EDF7E7')
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 = 'green'
}
cur += 30
}
@code = code_array.to_s
@code_image = canvas.to_blob{
self.format="JPG"
}
end
end
controller
class CodeController < ActionController::Base
def code_image
@noisy = NoisyImage.new(4) #生成一个有4字符的图片
@code=""
@code.insert(0, @noisy.code[2])
@code.insert(1, @noisy.code[7])
@code.insert(2, @noisy.code[12])
@code.insert(3, @noisy.code[17])
session[:code] = @code
send_data @noisy.code_image, :type => 'image/jpeg', :disposition => 'inline'
end
end
view
<img src="/code/code_image" id="code_img" onclick="code_refresh()" />
...
<script>
function code_refresh(){
$("#code_img").attr("src","/code/code_image");
}
</script>
点击验证码之后,不刷新 修改 js 代码
$("#code_img").attr("src","")
img 标签改变为
但是页面中验证码依然存在 请问该如何实现点击验证码进行刷新?