我更喜欢使用ruby,rails,heroku甚至更新的RMagick。我已经尝试了几种解决方案,在堆栈溢出时使用Heroku上的RMagick,没有结果。这是我遇到的一个有趣的路障。
我创建了一个工作函数,只有在文本超出给定长度时才能确定要修改的正确字体大小。一切都在我的本地机器上运行得很好。但是,一旦尝试在heroku上,似乎RMagick要么没有安装,要么没有被识别。它看起来像heroku控制台引用ruby 1.9.1,这与它有什么关系吗?我正在继续研究这个问题,但如果有人对此事有任何想法,我会很感兴趣。
非常感谢
* LOCAL MACHINE RUBY 1.8.7 RAILS CONSOLE ***
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 >
* LOCAL MACHINE RUBY 1.9.2 RAILS CONSOLE ***
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 CONSOLE * ** * ** * ****
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