Created
June 21, 2021 10:07
-
-
Save MatyasKriz/8db771f4870f95dbd11a9e9a5e15d19a to your computer and use it in GitHub Desktop.
Simple ruby script that generates all necessary iOS icons.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'fileutils' | |
require 'json' | |
# MARK: Classes | |
class Size | |
attr_reader :base_size | |
attr_reader :scale | |
def initialize(base_size, scale) | |
@base_size = base_size | |
@scale = scale | |
end | |
def filename | |
"Icon-App-#{@base_size}x#{@base_size}@#{@scale}x.png" | |
end | |
end | |
class IconType | |
attr_reader :idiom | |
attr_reader :sizes | |
def initialize(idiom, sizes) | |
@idiom = idiom | |
@sizes = sizes | |
end | |
end | |
# MARK: Program | |
unless ARGV.length >= 1 | |
puts "Usage: #{__FILE__} source [destination]" | |
puts "\tsource - raster (largest available) or vector representation of the source icon" | |
puts "\tdestination - directory relative to current directory (#{Dir.pwd}) where to store the resultant icons, default is current directory" | |
exit 1 | |
end | |
source = ARGV[0] | |
destination = ARGV[1] || '.' | |
FileUtils.mkpath(destination) unless File.exists?(destination) | |
icon_types = [ | |
IconType.new('iphone', [ | |
Size.new(20, 2), | |
Size.new(20, 3), | |
Size.new(29, 1), | |
Size.new(29, 2), | |
Size.new(29, 3), | |
Size.new(40, 2), | |
Size.new(40, 3), | |
Size.new(60, 2), | |
Size.new(60, 3), | |
]), | |
IconType.new('ipad', [ | |
Size.new(20, 1), | |
Size.new(20, 2), | |
Size.new(29, 1), | |
Size.new(29, 2), | |
Size.new(40, 1), | |
Size.new(40, 2), | |
Size.new(76, 1), | |
Size.new(76, 2), | |
Size.new(83.5, 2), | |
]), | |
IconType.new('ios-marketing', [Size.new(1024, 1)]) | |
] | |
sizes = icon_types.flat_map { |icon_type| icon_type.sizes }.uniq { |size| [size.base_size, size.scale] } | |
sizes.each do |size| | |
real_size = size.base_size * size.scale | |
exit 1 unless system("convert \"#{source}\" -resize #{real_size}x#{real_size} -unsharp 1x4 #{destination}/#{size.filename}") | |
end | |
contents = { | |
:images => icon_types.flat_map do |icon_type| | |
icon_type.sizes.map do |icon_size| | |
{ | |
:size => "#{icon_size.base_size}x#{icon_size.base_size}", | |
:idiom => icon_type.idiom, | |
:filename => icon_size.filename, | |
:scale => "#{icon_size.scale}x", | |
} | |
end | |
end, | |
:info => { | |
:version => 1, | |
:author => 'xcode' | |
} | |
} | |
IO.write("#{destination}/Contents.json", JSON.pretty_generate(contents)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment