smushkid 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/smushkid.rb +51 -34
- metadata +1 -1
data/lib/smushkid.rb
CHANGED
@@ -2,18 +2,23 @@
|
|
2
2
|
require 'find'
|
3
3
|
require 'RMagick'
|
4
4
|
require 'json'
|
5
|
+
require 'fileutils'
|
5
6
|
|
6
|
-
|
7
|
-
|
7
|
+
@quiet = ARGV.delete('-q')
|
8
|
+
@make_backup = ARGV.delete('-b')
|
9
|
+
path = ARGV[0]
|
10
|
+
@quality = ARGV[1]
|
8
11
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
if File.directory?(path)
|
13
|
+
totalsavings = 0
|
14
|
+
File.open("images_processed.json", "a+") { |f| f << "{ \"image_results\" : [ " }
|
15
|
+
@multi = true
|
16
|
+
else
|
17
|
+
end
|
18
|
+
|
19
|
+
def process_file(file)
|
15
20
|
simg = Magick::Image::read(file).first
|
16
|
-
source_image = {
|
21
|
+
@source_image = {
|
17
22
|
'format' => simg.format,
|
18
23
|
'filesize' => simg.filesize,
|
19
24
|
'geometry_cols' => simg.columns,
|
@@ -24,14 +29,13 @@ Find.find(path) do |file|
|
|
24
29
|
'colors' => simg.number_colors,
|
25
30
|
'ppi' => simg.units
|
26
31
|
}
|
27
|
-
target_file = File.dirname(file) + "/" + "smaller-" + File.basename(file)
|
28
32
|
simg.strip!
|
29
33
|
simg.quantize 32
|
30
|
-
simg.write(target_file) do
|
31
|
-
|
34
|
+
simg.write(@target_file) do
|
35
|
+
self.quality = @quality.to_i
|
32
36
|
end
|
33
|
-
timg = Magick::Image::read(target_file).first
|
34
|
-
target_image = {
|
37
|
+
timg = Magick::Image::read(@target_file).first
|
38
|
+
@target_image = {
|
35
39
|
'format' => timg.format,
|
36
40
|
'filesize' => timg.filesize,
|
37
41
|
'geometry_cols' => timg.columns,
|
@@ -42,30 +46,43 @@ Find.find(path) do |file|
|
|
42
46
|
'colors' => timg.number_colors,
|
43
47
|
'ppi' => timg.units
|
44
48
|
}
|
45
|
-
@savings = source_image['filesize'] - target_image['filesize']
|
46
|
-
image_results = { "filename" => file, "savings" => @savings, "before" => source_image, "after" => target_image }
|
47
|
-
puts "savings:"
|
48
|
-
puts @savings
|
49
|
+
@savings = @source_image['filesize'] - @target_image['filesize']
|
50
|
+
@image_results = { "filename" => file, "savings" => @savings, "before" => @source_image, "after" => @target_image }
|
51
|
+
puts "savings: " + @savings.to_s unless @quiet
|
49
52
|
if @savings < 0
|
50
53
|
@savings = 0
|
51
|
-
puts "no space savings acheived deleting #{target_file}"
|
52
|
-
File.delete(target_file)
|
54
|
+
puts "no space savings acheived deleting #{@target_file}" unless @quiet
|
55
|
+
File.delete(@target_file)
|
53
56
|
else
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
+
if @make_backup
|
58
|
+
puts "space savings! replacing with: #{file} and making backup or original: #{@backup_file}" unless @quiet
|
59
|
+
puts "move #{@target_file} to #{file}" unless @quiet
|
60
|
+
FileUtils.cp file, @backup_file
|
61
|
+
File.rename @target_file, file
|
62
|
+
File.open("images_processed.json", "a+") { |f| f << @image_results.to_json + ','}
|
63
|
+
else
|
64
|
+
puts "space savings! replacing with #{simg}" unless @quiet
|
65
|
+
puts "move #{@target_file} to #{file}" unless @quiet
|
66
|
+
File.rename @target_file, file
|
67
|
+
File.open("images_processed.json", "a+") { |f| f << @image_results.to_json + ','}
|
68
|
+
end
|
57
69
|
end
|
58
|
-
File.open("images_processed.json", "a+") { |f| f << image_results.to_json + ','}
|
59
|
-
File.open("images_processed_list.txt", "a+") { |f| f << file + "\n"}
|
60
|
-
else
|
61
|
-
puts "no match: #{file}"
|
62
|
-
end
|
63
|
-
totalsavings = totalsavings + @savings.to_i
|
64
|
-
puts "Running total savings (in bytes):"
|
65
|
-
puts totalsavings
|
66
70
|
end
|
67
|
-
File.open("images_processed.json", "a+") { |f| f << "]}" }
|
68
|
-
|
69
|
-
|
70
71
|
|
71
72
|
|
73
|
+
Find.find(path) do |file|
|
74
|
+
@target_file = File.dirname(file) + "/" + "smaller-" + File.basename(file)
|
75
|
+
@backup_file = File.dirname(file) + "/" + "original-" + File.basename(file)
|
76
|
+
if @multi
|
77
|
+
pattern = '**' '/' '*.jpg'
|
78
|
+
process_file(file) if File.fnmatch(pattern, file, File::FNM_CASEFOLD)
|
79
|
+
File.open("images_processed_list.txt", "a+") { |f| f << file + "\n"}
|
80
|
+
else
|
81
|
+
pattern = '*.jpg'
|
82
|
+
process_file(file) if File.fnmatch(pattern, file, File::FNM_CASEFOLD)
|
83
|
+
puts @image_results.to_json
|
84
|
+
end
|
85
|
+
totalsavings = totalsavings.to_i + @savings.to_i
|
86
|
+
puts "Total savings (in bytes): " + totalsavings.to_s unless @quiet
|
87
|
+
end
|
88
|
+
File.open("images_processed.json", "a+") { |f| f << "]}" } if File.directory?(path)
|