Skip to content

Instantly share code, notes, and snippets.

@jcromartie
Last active December 14, 2015 15:38
Show Gist options
  • Save jcromartie/5108840 to your computer and use it in GitHub Desktop.
Save jcromartie/5108840 to your computer and use it in GitHub Desktop.
Convert one super-sized (ideally iPad landscape @2x) image into the slew of images required to implement a non-repeating nav bar background at all sizes and resolutions for iOS.
#!/usr/bin/env ruby
# Takes one Retina iPad landscape nav bar image, and creates all of
# the requisite sizes for use on each device and orientation. Crops
# part of the image for iPhone portrait size.
#
# This is ONLY necessary for non-repeating or non-resizable (i.e. with
# cap insets) nav bar images. This program is an illustration of why
# you should discourage your designers from doing this.
#
# Requires Ruby (obviously) and ImageMagick (the "convert" tool)
#
# Usage: navbar.rb source_image
infile = ARGV[0]
raise "Source image ${infile} does not exist" unless File.exist?(infile)
dims = {
"portrait~ipad" => [768, 44],
"landscape~ipad" => [1024, 44],
"portrait~iphone" => [320, 44],
"landscape~iphone" => [480, 32],
"landscape-568h" => [568, 32]
}
dims.each do |name, dim|
scales = [2]
scales << 1 if !(name =~ /568h/)
scales.each do |s|
px_dim = dim.map { |x| x * s }
outname = if s == 2
name.sub(/(~|$)/, "@2x\\1")
else
name
end
outfile = "navbar-#{outname}.png"
dim_str = px_dim.join("x")
opts = []
if dim == [320, 44]
opts << "-shave '30%x0'"
end
opts << "-resize '#{dim_str}!'"
puts `convert "#{infile}" #{opts.join(' ')} "#{outfile}"`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment