我对 ruby、rails、heroku 甚至 RMagick 的工作都比较陌生。我尝试了几种在堆栈溢出时发现的在 Heroku 上使用 RMagick 的解决方案,但没有任何结果。这是我遇到的一个有趣的路障。
我创建了一个工作函数来确定仅在文本超出给定长度时要修改的正确字体大小。一切都在我的本地机器上运行良好。但是,一旦在 heroku 上尝试,似乎 RMagick 要么未安装,要么未被识别。看起来 Heroku 控制台引用了 ruby 1.9.1,这与它有什么关系吗?我正在继续研究这个问题,但如果有人对此事有任何想法,我会很感兴趣。
非常感谢
*本地机器 Ruby 1.8.7 导轨控制台***
1.8.7 :001 > def find_width(string, typeface, font_size)
1.8.7 :002?> max_width = 1087
1.8.7 :003?> type_size = font_size.to_i
1.8.7 :004?> label = Magick::Draw.new
1.8.7 :005?> label.font = typeface
1.8.7 :006?> label.pointsize = type_size
1.8.7 :007?> label.text_antialias(true)
1.8.7 :008?> label.text(0,0,string)
1.8.7 :009?> metrics = label.get_type_metrics(string)
1.8.7 :010?> width = metrics.width
1.8.7 :011?> if width > max_width
1.8.7 :012?> adjusted_size = type_size * max_width / width
1.8.7 :013?> return adjusted_size.to_i
1.8.7 :014?> else
1.8.7 :015 > return font_size
1.8.7 :016?> end
1.8.7 :017?> end
=> nil
1.8.7 :018 > find_width("HI", "blackjar-webfont", "263pt")
=> "263pt"
1.8.7 :019 > find_width("This is a test!", "blackjar-webfont", "263pt")
=> 185
1.8.7 :020 >
*本地机器 Ruby 1.9.2 导轨控制台***
Loading development environment (Rails 3.0.7)
1.9.2p290 :001 > def find_width(string, typeface, font_size)
1.9.2p290 :002?> max_width = 1087
1.9.2p290 :003?> type_size = font_size.to_i
1.9.2p290 :004?> label = Magick::Draw.new
1.9.2p290 :005?> label.font = typeface
1.9.2p290 :006?> label.pointsize = type_size
1.9.2p290 :007?> label.text_antialias(true)
1.9.2p290 :008?> label.text(0,0,string)
1.9.2p290 :009?> metrics = label.get_type_metrics(string)
1.9.2p290 :010?> width = metrics.width
1.9.2p290 :011?> if width > max_width
1.9.2p290 :012?> adjusted_size = type_size * max_width / width
1.9.2p290 :013?> return adjusted_size.to_i
1.9.2p290 :014?> else
1.9.2p290 :015 > return font_size
1.9.2p290 :016?> end
1.9.2p290 :017?> end
=> nil
1.9.2p290 :018 > find_width("HI", "blackjar-webfont", "263pt")
=> "263pt"
1.9.2p290 :019 > find_width("This is a test!", "blackjar-webfont", "263pt")
=> 185
1.9.2p290 :020 >
** * ** * ** * HEROKU 控制台* ** * ** * ****
Loading production environment (Rails 3.0.7)
irb(main):001:0> def find_width(string, typeface, font_size)
irb(main):002:1> max_width = 1087
irb(main):003:1> type_size = font_size.to_i
irb(main):004:1> label = Magick::Draw.new
irb(main):005:1> label.font = typeface
irb(main):006:1> label.pointsize = type_size
irb(main):007:1> label.text_antialias(true)
irb(main):008:1> label.text(0,0,string)
irb(main):009:1> metrics = label.get_type_metrics(string)
irb(main):010:1> width = metrics.width
irb(main):011:1> if width > max_width
irb(main):012:2> adjusted_size = type_size * max_width / width
irb(main):013:2> return adjusted_size.to_i
irb(main):014:2> else
irb(main):015:2* return font_size
irb(main):016:2> end
irb(main):017:1> end
=> nil
irb(main):018:0> find_width("HI", "blackjar-webfont", "263pt")
NameError: uninitialized constant Magick from /app/vendor/bundle/ruby/1.9.1/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206:in `const_missing_from_s3_library'
from (irb):4:in `find_width'
from (irb):18
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>'from script/rails:6:in `require'from script/rails:6:in `<main>'
irb(main):019:0>
功能码
def find_width(string, typeface, font_size)
max_width = 1087
type_size = font_size.to_i
label = Magick::Draw.new
label.font = typeface
label.pointsize = type_size
label.text_antialias(true)
label.text(0,0,string)
metrics = label.get_type_metrics(string)
width = metrics.width
if width > max_width
adjusted_size = type_size * max_width / width
return adjusted_size.to_i
else
return font_size
end
end