#!/usr/bin/ruby -w SCALES = [1, 2, 3] def usage name = File.basename(__FILE__) puts "Usage: #{name} " end def scale_retina(width, height, scale, in_file, out_dir, prefix) file_scale = scale > 1 ? "@#{scale}x" : '' filename = "#{prefix}#{file_scale}.png" out_file = File.join(out_dir, filename) puts "> convert '#{in_file}' -scale #{width * scale}x#{height * scale} '#{out_file}'" output = `gm convert '#{in_file}' -scale #{width * scale}x#{height * scale} '#{out_file}'`.strip puts output if output.length > 0 end def image_dimensions(path) `gm identify '#{path}'`.split[2].split('x').map(&:to_i) end in_file, out_dir, prefix, raw_width, raw_height = ARGV unless File.exist?(in_file) puts "Input image not found: #{in_file}" usage exit 1 end unless File.directory?(out_dir) puts "Output directory not found: #{out_dir}" usage exit 2 end unless prefix && prefix.strip.length > 0 puts "Missing output filename prefix" usage exit 3 end if raw_width && raw_height width = raw_width.to_f height = raw_height.to_f else width, height = image_dimensions(in_file).map { |x| x / 3 } end unless width > 0 && height > 0 puts "Invalid dimensions: #{raw_width.inspect} #{raw_height.inspect}" usage exit 4 end SCALES.each do |scale| scale_retina(width, height, scale, in_file, out_dir, prefix) end