1

I'm newer to working in ruby, rails, heroku and even newer to RMagick. I've attempted several solutions to using RMagick on Heroku found on stack overflow with no results. Here is an interesting road block that I've come across.

I've created a working function to determine the proper font size to be modified only if the text goes beyond a given length. Everything works beautifully on my local machine. However, once attempted on heroku it seems as though the RMagick is either not install or not being recognized. It looks as thought the heroku console references ruby 1.9.1, does this have anything to do with it? I'm continuing to research the issue, but would be interested if anyone has any thoughts on the matter.

Many Thanks

*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> 

Function Code

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

0 Answers0