#!/usr/bin/ruby =begin Purpose: Resizes image using RMagick to fit WxH while keeping aspect and stripping all EXIF crap Author: Wejn (wejn(at)box(.)cz) License: public-domain Requires: Ruby >= 1.8, rubygems, RMagick (mine: 1.9.0) TS: 20050806220000 =end require 'rubygems' require_gem 'rmagick' if ARGV.size != 4 $stderr.puts "#$0 src dst w h" exit 1 end h,w = nil, nil begin w = Integer(ARGV[2]) h = Integer(ARGV[3]) rescue $stderr.puts "Error: w and h must be numbers!" exit 1 end unless FileTest.exist?(ARGV[0]) $stderr.puts "Error: src must exist!" exit 1 end begin # load up i = Magick::ImageList.new(ARGV[0]) puts "Image is: #{i.columns} x #{i.rows}" if $DEBUG puts "I want: #{w} x #{h}" if $DEBUG # compute new size r = [i.columns / w.to_f, i.rows / h.to_f].max nw = (i.columns / r).round nh = (i.rows / r).round puts "New size will be: #{nw} x #{nh}" if $DEBUG #scale it i2 = i.scale(nw, nh) # remove EXIF i2.profile!('*', nil) # write out i2.write(ARGV[1]) #File.open(ARGV[1], 'w').write(i2.to_blob) # also possible rescue $stderr.puts "Resize failed: #$!" exit 1 end exit 0