Rails MiniMagic 动态生成图片

ecloud · August 09, 2017 · Last by ecloud replied at August 15, 2017 · 2194 hits

直接上代码

def generate_mini_captcha_image(opts = {})

    label = SecureRandom.hex(3)

    session[:captcha] = label

    image = MiniMagick::Image.new('mini_captcha.jpg')

    image.run_command("convert -pointsize 16 -kerning 1 +noise Laplacian -undercolor lightgrey label:#{label} mini_captcha.jpg")

    image.to_blob
end

有报错信息如下

wrong constant name Convert -pointsize 16 -kerning 1 +noise laplacian -undercolor lightgrey label:4bd6c8 mini_captcha.jpg

不懂什么意思。

应该这么写:

image.run_command *%W"convert -pointsize 16 -kerning 1 +noise Laplacian -undercolor lightgrey #{"label:#{label} "} mini_captcha.jpg"

它会根据第一个参数名去找一个 constant

给 "convert" 它就会去找 MiniMagic::Tool::Convert, 但你给了全命令它就去找 MiniMagic::Tool::Convert -pointsize ... 这显然没定义

Reply to luikore

非常感谢

ecloud closed this topic. 08 Sep 10:14
You need to Sign in before reply, if you don't have an account, please Sign up first.