Skip to content

Instantly share code, notes, and snippets.

@tieleman
Created June 8, 2012 16:38
Show Gist options
  • Save tieleman/2896735 to your computer and use it in GitHub Desktop.
Save tieleman/2896735 to your computer and use it in GitHub Desktop.
A hacky retina file finder/reporter. Use it for all your weird retina weirdness stuff.
class RetinaFinder
PATH_TO_NORMAL = "app/assets/images/"
PATH_TO_RETINA = "app/assets/images/2x/"
IMAGE_FORMATS = %w(png jpg gif)
MULTIPLIER = 4 # 2 for regular retina, 4 for retina + 2x zoom, etc.
def run
build_file_lists
match_files
report_missing_or_orphaned
end
private
def build_file_lists
@normal_files = files_for_directory(PATH_TO_NORMAL).reject { |f| f.include? PATH_TO_RETINA }
@retina_files = files_for_directory(PATH_TO_RETINA)
@normal_files.map! { |f| f.gsub PATH_TO_NORMAL, '' }.sort!
@retina_files.map! { |f| f.gsub PATH_TO_RETINA, '' }.sort!
end
def files_for_directory(path)
Dir.glob(path + "**/*.{#{IMAGE_FORMATS.join(',')}}")
end
def match_files
puts "\033[1mPossible incorrect retina images:\033[22m"
(@normal_files & @retina_files).each do |f|
d_org, d_ret = `identify #{PATH_TO_NORMAL + f} | cut -d' ' -f3`.strip.split('x'), `identify #{PATH_TO_RETINA + f} | cut -d' ' -f3`.strip.split('x')
d_org.map! { |n| n.to_i }
d_ret.map! { |n| n.to_i }
if (d_org[0] * MULTIPLIER != d_ret[0]) || (d_org[1] * MULTIPLIER != d_ret[1])
puts "\t#{f} : Dimensions for retina incorrect.\n\t\toriginal: #{d_org.join('x')}\n\t\tretina: #{d_ret.join('x')}"
end
end
end
def report_missing_or_orphaned
left = @normal_files - @retina_files
right = @retina_files - @normal_files
if left.any?
puts "\n\033[1mThere are/is #{left.size} file(s) that don't have retina counterpart:\033[22m"
left.each { |f| puts "\t#{f}"}
end
if right.any?
puts "\n\033[1mThere are/is #{right.size} retina file(s) that don't have normal counterpart:\033[22m"
right.each { |f| puts "\t#{f}"}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment