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
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/favicon_maker/generator.rb', line 25
def create_versions(options={}, &block)
puts "FaviconMaker: WARNING! Your installed ImageMagick version #{IM_VERSION} is not up-to-date and might produce suboptimal output!" if IM_VERSION < RECENT_VERSION
switch_colorspace = IM_VERSION < COLORSPACE_MIN_VERSION
colorspace_conv = ["RGB", "sRGB"]
colorspace_conv.reverse! if switch_colorspace
is_windows = (RbConfig::CONFIG['host_os'].match /mswin|mingw|cygwin/)
options = {
:versions => ICON_VERSIONS.keys,
:custom_versions => {},
:root_dir => File.dirname(__FILE__),
:input_dir => "favicons",
:base_image => "favicon_base.png",
:output_dir => "favicons_output",
:copy => false
}.merge(options)
raise ArgumentError unless options[:versions].is_a? Array
base_path = File.join(options[:root_dir], options[:input_dir])
input_file = File.join(base_path, options[:base_image])
icon_versions = ICON_VERSIONS.merge(options[:custom_versions])
(options[:versions] + options[:custom_versions].keys).uniq.each do |version|
version = icon_versions[version]
sizes = version[:dimensions] || version[:sizes]
composed_path = File.join(base_path, version[:filename])
output_path = File.join(options[:root_dir], options[:output_dir])
output_file = File.join(output_path, version[:filename])
build_mode = nil
if File.exist?(composed_path) && options[:copy]
FileUtils.cp composed_path, output_file
build_mode = :copied
else
case version[:format].to_sym
when :png
image = MiniMagick::Image.open(input_file)
image.define "png:include-chunk=none,trns,gama"
image.colorspace colorspace_conv.first
image.resize sizes
image.format "png"
image.strip
image.colorspace colorspace_conv.last
image.write output_file
when :ico
ico_cmd = "convert \"#{input_file}\" -colorspace #{colorspace_conv.first} "
escapes = "\\" unless is_windows
sizes.split(',').sort_by{|s| s.split('x')[0].to_i}.each do |size|
ico_cmd << "#{escapes}( -clone 0 -colors 256 -resize #{size} #{escapes}) "
end
ico_cmd << "-delete 0 -colors 256 -colorspace #{colorspace_conv.last} \"#{File.join(output_path, version[:filename])}\""
puts `#{ico_cmd}`
end
build_mode = :generated
end
if block_given?
yield output_file, build_mode
end
end
end
|