-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretina-scale
executable file
·53 lines (47 loc) · 1.38 KB
/
retina-scale
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
#!/usr/bin/ruby -w
SCALES = [1, 2, 3]
def usage
name = File.basename(__FILE__)
puts "Usage: #{name} <input-image> <output-directory> <output-filename-prefix> <width> <height>"
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