-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRetinizer.rb
80 lines (56 loc) · 2.87 KB
/
Retinizer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#
# Created by Hector Zarate (hecktorzr.at.gmail.com) on 29/11/12
#
# Retinizer.rb
# Script to prepare image resources for Retina and standard resolutions under iPhone projects.
#
#Some Configuration
require 'rubygems'
require 'RMagick'
require 'fileutils'
retinaFilenameSuffix = "@2x" # String that will be added to original filenames (if needed)
imageFilenameExtensions = ["png", "jpg", "jpeg"] # Files with this extensions will be affected by script
SCALE_BY = 0.5 # Scale for standard (non-Retina) images 50%
def resize_retina_image(image_filename)
image_name = File.basename(image_filename)
image = Magick::Image.read(image_filename).first # Read the image
new_image = image.scale(SCALE_BY)
if new_image.write(image_filename) # Overwrite image file
puts "Resizing Image (#{SCALE_BY}): #{image_name}"
else
puts "Error: Couldn't resize image #{image_name}"
end
end
targetDirectory = ARGV[0] ? ARGV[0] : "./" # If no argument is supplied we use the current directory (.)
unless targetDirectory.end_with?("/")
targetDirectory += "/"
end
abort ("Error: #{targetDirectory} is not a directory") unless File.directory?(targetDirectory)
targetFilenames = Array.new
# For each supported extension, add found files to targetFilenames
imageFilenameExtensions.each do |extension|
targetFilenames += Dir["#{targetDirectory}*.#{extension}"]
end
puts "Images found in directory: #{targetFilenames.count}"
# Rename all image files to Basename + @2x
targetFilenames.each do |originalImageFilename|
imageBasename = File.basename(originalImageFilename, ".*")
if imageBasename.end_with?(retinaFilenameSuffix)
retinaImageFilename = originalImageFilename # image file already includes @2x suffix
else
retinaImageFilename = "#{targetDirectory}#{imageBasename}#{retinaFilenameSuffix}#{File.extname(originalImageFilename)}"
File.rename(originalImageFilename, retinaImageFilename)
puts "Renaming: #{File.basename(originalImageFilename)} => #{File.basename(retinaImageFilename)}"
end
retinaImageBasename = File.basename(retinaImageFilename, ".*")
if retinaImageBasename.length > retinaFilenameSuffix.length # avoiding a filename without name. :S
standardImageBaseName = retinaImageBasename[0...(retinaImageBasename.length - retinaFilenameSuffix.length)]
standardImageFilename = "#{targetDirectory}#{standardImageBaseName}#{File.extname(retinaImageFilename)}"
FileUtils.cp_r(retinaImageFilename, standardImageFilename, :remove_destination => true)
puts "Coping #{File.basename(retinaImageFilename)} => #{File.basename(standardImageFilename)}"
resize_retina_image(standardImageFilename)
else
puts "Warning: {#originalImageFilename} file name is too short. Coulnd't create standard resolution image."
end
standardImageFilename = retinaImageFilename
end