Created
January 18, 2019 23:17
-
-
Save twobitlabs/76607759ea243faedd1cb3e5ffdfee09 to your computer and use it in GitHub Desktop.
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 | |
path = "/tmp/images" | |
files = Dir["#{path}/*.png"] | |
base_files = [] | |
for file in files do | |
if !file.include? "x.png" | |
base_files << file | |
end | |
end | |
output_path = "/tmp/converted-images" | |
`rm -rf #{output_path}` # DANGER | |
`mkdir -p #{output_path}/drawable-xxxhdpi` | |
`mkdir -p #{output_path}/drawable-xxhdpi` | |
`mkdir -p #{output_path}/drawable-xhdpi` | |
`mkdir -p #{output_path}/drawable-hdpi` | |
`mkdir -p #{output_path}/drawable-mdpi` | |
# Make sure we have all the variants in the directory | |
for file in base_files do | |
file_name = File.basename(file, ".*") # /tmp/images/foo.png -> foo | |
file_name_android = file_name.gsub('-', '_') | |
file1x = "#{path}/#{file_name}.png" | |
raise "Missing #{file1x}" unless File.exists?(file1x) | |
file2x = "#{path}/#{file_name}@2x.png" | |
raise "Missing #{file2x}@2x variant" unless File.exists?(file2x) | |
file3x = "#{path}/#{file_name}@3x.png" | |
raise "Missing #{file3x}@3x variant" unless File.exists?(file3x) | |
height=`sips --getProperty pixelHeight #{file1x} | sed -E "s/.*pixelHeight: ([0-9]+)/\\1/g" | tail -1` | |
width=`sips --getProperty pixelWidth #{file1x} | sed -E "s/.*pixelWidth: ([0-9]+)/\\1/g" | tail -1` | |
`cp #{file1x} #{output_path}/drawable-mdpi/#{file_name_android}.png` | |
`cp #{file2x} #{output_path}/drawable-xhdpi/#{file_name_android}.png` | |
`cp #{file3x} #{output_path}/drawable-xxhdpi/#{file_name_android}.png` | |
`sips --resampleHeightWidth #{(height.to_i * 1.5).round} #{(width.to_i * 1.5).round} #{file2x} --out #{output_path}/drawable-hdpi/#{file_name_android}.png` | |
`sips --resampleHeightWidth #{(height.to_i * 4).round} #{(width.to_i * 4).round} #{file3x} --out #{output_path}/drawable-xxxhdpi/#{file_name_android}.png` | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment