Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 40 lines (36 sloc) 1.512 kb
c003dff @igrigorik heroku app
authored
1 require 'rubygems'
2 require 'RMagick'
5101389 @igrigorik sinatra app
authored
3 require 'rvg/rvg'
4
5 include Magick
6
7 module RMagickTextUtil
8 def render_cropped_text(caption_text, width_constraint, height_constraint, &block)
9 image = render_text(caption_text, width_constraint, &block)
10 if height_constraint < image.rows
11 percent = height_constraint.to_f / image.rows.to_f
12 end_index = (caption_text.size * percent).to_i # takes a leap into cropping
13 image = render_text(caption_text[0..end_index] + "...", width_constraint, &block)
14 while height_constraint < image.rows && end_index > 0 # reduce in big chunks until within range
15 end_index -= 80
16 image = render_text(caption_text[0..end_index] + "...", width_constraint, &block)
17 end
18 while height_constraint > image.rows # lengthen in smaller steps until exceed
19 end_index += 10
20 image = render_text(caption_text[0..end_index] + "...", width_constraint, &block)
21 end
22 while height_constraint < image.rows && end_index > 0 # reduce in baby steps until fit
23 end_index -= 1
24 image = render_text(caption_text[0..end_index] + "...", width_constraint, &block)
25 end
26 end
27 image
28 end
29
30 def render_text(caption_text, width_constraint, &block)
31 Magick::Image.read("caption:#{caption_text.to_s}") {
32 # this wraps the text to fixed width
33 self.size = width_constraint
34 # other optional settings
35 block.call(self) if block_given?
36 }.first
37 end
38 end
39
40 include RMagickTextUtil
Something went wrong with that request. Please try again.