用RMagick做了一个显示验证吗的图
在windows下显示一切正常,上传到linux服务器上后,图片显示出来了,有背景颜色,可是就是没有文字
请指点迷津,不胜感激。
引用
问题已经解决,后面说面了解决方法
class NoisyImage
require 'RMagick'
include Magick
attr_reader :code, :code_image
def initialize(length = 4)
text_size = 33
rand_height = 9
colors = ['#FF0000', '#3300CC', '#FF3300', '#b50000', '#373000', '#f000f0', '##336600']
validbglinecolors = ['#ABEFAB', '#FF99FF', '#CCCCFF', '#66FF66', '#CCFF33']
validchars = (0..9).to_a
dist = (5..30).to_a
step = 8
chars = []
text_size_space = text_size * 0.8
text_size_step = text_size * 0.6
length.times {|x|
chars << validchars[rand(validchars.size).ceil - 1].to_s
}
bglinecolor = validbglinecolors[rand(validbglinecolors.size).ceil - 1]
image = Image.new(length * text_size_step + 10, text_size * 1.2, HatchFill.new('white', bglinecolor, dist[rand(dist.size - 1)]))
text = Draw.new
#使用字体文件和直接使用字体,或者不写font, font_family两个属性都是如图所示结果,linux下无字显示
text.font = "fonts/" + ['times.ttf', 'arial.ttf', 'verdana.ttf', 'artro.ttf'].sort{rand}.pop
#text.font_family = ['times', 'sans', 'fixed', 'Verdana'].sort{rand}.pop
text.font_weight = BoldWeight
text.text(0, 0, ' ')
chars.each {|char|
text.annotate(image, 0, 0, step, text_size_space + rand(rand_height), char) {
self.rotation = dist[rand(dist.size - 1).ceil]
self.fill = colors[rand(colors.size - 1).ceil]
self.pointsize = text_size - validchars[rand(rand_height).ceil]
}
step += text_size_step - rand(5)
}
text.draw(image)
@code = chars.to_s()
@code_image = image.to_blob{ self.format="JPG" }
end
end
引用
原因是linux服务器下用的是fastcgi+apache,windows下测试用的是ruby webrick,而字体文件使用了相对路径,两者的路径是不同的,
使用绝对路径就没有这个错误了。
误: text.font = "fonts/" + ['times.ttf', 'arial.ttf', 'verdana.ttf', 'artro.ttf'].sort{rand}.pop
正: text.font = "/home/web/fonts/" + ['times.ttf', 'arial.ttf', 'verdana.ttf', 'artro.ttf'].sort{rand}.pop