Last active
August 29, 2015 14:00
-
-
Save chocomelonchan/11359947 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
require 'fileutils' | |
require 'rmagick' | |
# file name | |
img_file_path = ARGV[0] | |
if (/\// =~ img_file_path) then | |
/^.*\/([^\/]+).png$/ =~ img_file_path | |
else | |
/^(.+).png$/ =~ img_file_path | |
end | |
img_file_name = $1 + '.png' | |
# directory | |
root_dir = 'res' | |
if ($DEBUG) then | |
root_dir = $1 | |
end | |
# get xxhdpi image data | |
img_data = Magick::ImageList.new(img_file_path) | |
xxhdpi_width = img_data.columns | |
xxhdpi_height = img_data.rows | |
RESIZE_IMAGES = [ | |
{ | |
'dir' => 'mdpi', | |
'scale' => 1.0 | |
}, | |
{ | |
'dir' => 'hdpi', | |
'scale' => 1.5 | |
}, | |
{ | |
'dir' => 'xhdpi', | |
'scale' => 2.0 | |
}, | |
{ | |
'dir' => 'xxhdpi', | |
'scale' => 3.0 | |
} | |
] | |
# resize image | |
mdpi_width = xxhdpi_width / 3 | |
mdpi_height = xxhdpi_height / 3 | |
img = Magick::Image.read(img_file_path).first | |
RESIZE_IMAGES.each do |resize_images| | |
dir = root_dir + '/drawable-' + resize_images['dir'] | |
FileUtils.mkdir_p dir, :mode => 0755 | |
width = mdpi_width * resize_images['scale'] | |
height = mdpi_height * resize_images['scale'] | |
img.resize(width, height).write(dir + '/' + img_file_name) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment