-
-
Save yaraki/6398025 to your computer and use it in GitHub Desktop.
A simple Ruby script for preparing resized drawable images for multiple DPIs on Android.
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 | |
# -*- coding: utf-8 -*- | |
# | |
# Copyright (C) 2013 Yuichi Araki | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
# | |
# $ andr path/to/drawable-xxxhdpi/FILE | |
# -> drawable-xxhdpi/FILE drawable-xhdpi/FILE, drawable-hdpi/FILE, drawable-mdpi/FILE, drawable-ldpi/FILE | |
# | |
# $ andr path/to/drawable-xxhdpi/FILE | |
# -> drawable-xhdpi/FILE, drawable-hdpi/FILE, drawable-mdpi/FILE, drawable-ldpi/FILE | |
class Size | |
attr_accessor :width, :height | |
def initialize(width, height) | |
@width = width | |
@height = height | |
end | |
def to_s | |
"#{@width} x #{@height}" | |
end | |
end | |
class Strategy | |
RATIOS = { | |
:ldpi => 120, | |
:mdpi => 160, | |
:tvdpi => 213, | |
:hdpi => 240, | |
:xhdpi => 320, | |
:xxhdpi => 480, | |
:xxxhdpi => 640 | |
} | |
def initialize(filename) | |
# The original file | |
@original_file = File.expand_path(filename) | |
@filename = File.basename(filename) | |
# The orignal DPI | |
segments = @original_file.split(File::SEPARATOR) | |
parent_dir = segments[-2] | |
@original_dpi = parse_dpi(parent_dir) | |
@original_dir = File.dirname(@original_file) | |
@parent_dir = File.expand_path(File.join(@original_dir, '..')) | |
# Target DPIs | |
collect_target_dpis | |
# Original image size | |
@original_size = detect_image_size(filename) | |
end | |
def collect_target_dpis | |
@target_dpis = [] | |
return if @original_dpi == :ldpi | |
@target_dpis.push :ldpi if File.directory?(File.join(@original_dir, '..', 'drawable-ldpi')) | |
return if @original_dpi == :mdpi | |
@target_dpis.push :mdpi if File.directory?(File.join(@original_dir, '..', 'drawable-mdpi')) | |
return if @original_dpi == :tvdpi | |
@target_dpis.push :tvdpi if File.directory?(File.join(@original_dir, '..', 'drawable-tvdpi')) | |
return if @original_dpi == :hdpi | |
@target_dpis.push :hdpi if File.directory?(File.join(@original_dir, '..', 'drawable-hdpi')) | |
return if @original_dpi == :xhdpi | |
@target_dpis.push :xhdpi if File.directory?(File.join(@original_dir, '..', 'drawable-xhdpi')) | |
return if @original_dpi == :xxhdpi | |
@target_dpis.push :xxhdpi if File.directory?(File.join(@original_dir, '..', 'drawable-xxhdpi')) | |
return if @original_dpi == :xxxhdpi | |
@target_dpis.push :xxxhdpi if File.directory?(File.join(@original_dir, '..', 'drawable-xxxhdpi')) | |
end | |
def command(to) | |
w = @original_size.width * RATIOS[to] / RATIOS[@original_dpi] | |
h = @original_size.height * RATIOS[to] / RATIOS[@original_dpi] | |
f = File.join(@parent_dir, "drawable-#{to}", @filename) | |
"sips -z #{h} #{w} #{@original_file} -o #{f}" | |
end | |
def commands | |
@target_dpis.map {|dpi| command(dpi) } | |
end | |
def carry_out! | |
puts self.to_s | |
commands.each do |c| | |
puts c | |
break unless system c | |
end | |
end | |
def to_s | |
"#{@original_dpi}: #{@original_size}: #{@target_dpis.join(', ')}" | |
end | |
def parse_dpi(dir) | |
case dir | |
when 'drawable-xxxhdpi' | |
:xxxhdpi | |
when 'drawable-xxhdpi' | |
:xxhdpi | |
when 'drawable-xhdpi' | |
:xhdpi | |
when 'drawable-hdpi' | |
:hdpi | |
when 'drawable-mdpi' | |
:mdpi | |
when 'drawable-ldpi' | |
:ldpi | |
else | |
raise 'Invalid directory' | |
end | |
end | |
def detect_image_size(filename) | |
result = `file #{filename}` | |
md = result.match(/([1-9][0-9]*) x ([1-9][0-9]*)/) | |
Size.new(md[1].to_i, md[2].to_i) | |
end | |
end | |
strategy = Strategy.new(ARGV[0]) | |
strategy.carry_out! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment