Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 60 lines (42 sloc) 1.575 kb
3151aab New example
rmagick authored
1 require 'RMagick'
2 include Magick
3
4 puts <<END_INFO
5
6 This example demonstrates how to make a thumbnail of an image.
7 Usage:
8
9 ruby thumbnail.rb <filename <size>>
10
11 where `filename' is the name of an image file and `size' is the
12 size of the thumbnail in pixels. The default size is 120 pixels.
13 If you don't specify any arguments this script uses a default
14 image.
15
16 END_INFO
17
18 DEFAULT_SIZE = 120
19
20 case ARGV.length
21 when 2
22 size = ARGV[1].to_i
23 image = ARGV[0]
24 when 1
25 image = ARGV[0]
26 size = DEFAULT_SIZE
27 else
28 size = DEFAULT_SIZE
29 image = "../doc/ex/images/Blonde_with_dog.jpg"
30 end
31
32 geom = "#{size}x#{size}"
33
34 # Read the image and resize it.
35
36 img = Image.read(image)[0]
37 img = img.change_geometry(geom) { |cols, rows| img.thumbnail(cols, rows) }
38
39 # We need a background to display the thumbnail.
40 # Create a square, neutral gray background with raised edges.
41 # Make this background slightly larger than the image to allow
42 # for the raised border. A 3-pixel raised edge means that the
43 # background needs to be 6 pixels larger in each dimension.
44
45 bg = Image.new(size+6, size+6) { self.background_color = "gray75" }
46 bg = bg.raise(3,3)
47
48 # Just for the purposes of this example, display the thumbnail background on
49 # a larger white background.
50
51 white_bg = Image.new(size+50, size+50) {self.background_color = "white"}
52 white_bg = white_bg.composite(bg, CenterGravity, OverCompositeOp)
53
54 # Finally, center the thumbnail on the gray background.
55 thumbnail = white_bg.composite(img, CenterGravity, OverCompositeOp)
56
57 thumbnail.display
58 exit
59
Something went wrong with that request. Please try again.