Sha256: 8a77775b376b43457ed4e62da1f94d553eeb898d7a6495665eae828ff2ee0589

Contents?: true

Size: 1.82 KB

Versions: 18

Compression:

Stored size: 1.82 KB

Contents

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.display
exit

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
rmagick-1.10.0 examples/thumbnail.rb
rmagick-1.10.1 examples/thumbnail.rb
rmagick-1.11.0 examples/thumbnail.rb
rmagick-1.11.1 examples/thumbnail.rb
rmagick-1.12.0 examples/thumbnail.rb
rmagick-1.13.0 examples/thumbnail.rb
rmagick-1.7.1 examples/thumbnail.rb
rmagick-1.7.2 examples/thumbnail.rb
rmagick-1.7.3 examples/thumbnail.rb
rmagick-1.7.4 examples/thumbnail.rb
rmagick-1.8.0 examples/thumbnail.rb
rmagick-1.8.1 examples/thumbnail.rb
rmagick-1.8.2 examples/thumbnail.rb
rmagick-1.8.3 examples/thumbnail.rb
rmagick-1.9.0 examples/thumbnail.rb
rmagick-1.9.1 examples/thumbnail.rb
rmagick-1.9.2 examples/thumbnail.rb
rmagick-1.9.3 examples/thumbnail.rb