Skip to content
This repository
rmagick
file 64 lines (47 sloc) 1.883 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
require 'RMagick'
include Magick

puts <<END_INFO

This example demonstrates how to make a thumbnail of an image.
An image is resized to the target size (retaining its original
aspect ratio, of course) and then "mounted" on a square background
with raised edges.

Usage:

ruby thumbnail.rb <filename <size>>

where `filename' is the name of an image file and `size' is the
size of the thumbnail in pixels. The default size is 120 pixels.
If you don't specify any arguments this script uses a default
image.

END_INFO

DEFAULT_SIZE = 120

case ARGV.length
    when 2
        size = ARGV[1].to_i
        image = ARGV[0]
    when 1
        image = ARGV[0]
        size = DEFAULT_SIZE
    else
        size = DEFAULT_SIZE
        image = "../doc/ex/images/Flower_Hat.jpg"
end

geom = "#{size}x#{size}"

# Read the image and resize it. The `change_geometry' method
# computes the new image geometry and yields to a block. The
# return value of the block is the return value of the method.

img = Image.read(image)[0]
img.change_geometry!(geom) { |cols, rows| img.thumbnail! cols, rows }

# We need a background to display the thumbnail.
# Create a square, neutral gray background with raised edges.
# Make this background slightly larger than the image to allow
# for the raised border. A 3-pixel raised edge means that the
# background needs to be 6 pixels larger in each dimension.

bg = Image.new(size+6, size+6) { self.background_color = "gray75" }
bg = bg.raise(3,3)

# Just for the purposes of this example, display the thumbnail background on
# a larger white background.

white_bg = Image.new(size+50, size+50) {self.background_color = "white"}
white_bg = white_bg.composite(bg, CenterGravity, OverCompositeOp)

# Finally, center the thumbnail on the gray background.
thumbnail = white_bg.composite(img, CenterGravity, OverCompositeOp)

thumbnail.write("thumbnail.gif")
exit
Something went wrong with that request. Please try again.