smushkid 0.0.2 → 0.0.3
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 +43 -7
- metadata +1 -1
data/lib/smushkid.rb
CHANGED
@@ -6,16 +6,23 @@ require 'fileutils'
|
|
6
6
|
|
7
7
|
@quiet = ARGV.delete('-q')
|
8
8
|
@make_backup = ARGV.delete('-b')
|
9
|
+
@exif_tag = ARGV.delete('-e')
|
9
10
|
path = ARGV[0]
|
10
11
|
@quality = ARGV[1]
|
11
12
|
|
12
|
-
if File.directory?(path)
|
13
|
+
if File.directory?(path)
|
13
14
|
totalsavings = 0
|
14
15
|
File.open("images_processed.json", "a+") { |f| f << "{ \"image_results\" : [ " }
|
15
16
|
@multi = true
|
16
17
|
else
|
17
18
|
end
|
18
19
|
|
20
|
+
def already_done?(file)
|
21
|
+
simg = Magick::Image::read(file).first
|
22
|
+
puts "checking for EXIF value"
|
23
|
+
simg.properties.has_value?("smushkid")
|
24
|
+
end
|
25
|
+
|
19
26
|
def process_file(file)
|
20
27
|
simg = Magick::Image::read(file).first
|
21
28
|
@source_image = {
|
@@ -52,16 +59,23 @@ def process_file(file)
|
|
52
59
|
if @savings < 0
|
53
60
|
@savings = 0
|
54
61
|
puts "no space savings acheived deleting #{@target_file}" unless @quiet
|
62
|
+
puts "tagging original file" unless @quiet
|
63
|
+
@quoted_filename = "\"#{file}\""
|
64
|
+
%x(/usr/local/bin/jhead -cl \"smushkid\" #{@quoted_filename}) if @exif_tag
|
55
65
|
File.delete(@target_file)
|
56
66
|
else
|
57
67
|
if @make_backup
|
58
|
-
puts "space savings! replacing with: #{
|
68
|
+
puts "space savings! replacing with: #{simg} and making backup or original: #{@backup_file}" unless @quiet
|
69
|
+
puts "tagging target file" unless @quiet
|
70
|
+
%x(/usr/local/bin/jhead -cl \"smushkid\" #{@quoted_filename}) if @exif_tag
|
59
71
|
puts "move #{@target_file} to #{file}" unless @quiet
|
60
72
|
FileUtils.cp file, @backup_file
|
61
73
|
File.rename @target_file, file
|
62
74
|
File.open("images_processed.json", "a+") { |f| f << @image_results.to_json + ','}
|
63
75
|
else
|
64
76
|
puts "space savings! replacing with #{simg}" unless @quiet
|
77
|
+
puts "tagging target file" unless @quiet
|
78
|
+
%x(/usr/local/bin/jhead -cl \"smushkid\" #{@quoted_filename}) if @exif_tag
|
65
79
|
puts "move #{@target_file} to #{file}" unless @quiet
|
66
80
|
File.rename @target_file, file
|
67
81
|
File.open("images_processed.json", "a+") { |f| f << @image_results.to_json + ','}
|
@@ -72,15 +86,37 @@ end
|
|
72
86
|
|
73
87
|
Find.find(path) do |file|
|
74
88
|
@target_file = File.dirname(file) + "/" + "smaller-" + File.basename(file)
|
89
|
+
@quoted_filename = "\"#{@target_file}\""
|
75
90
|
@backup_file = File.dirname(file) + "/" + "original-" + File.basename(file)
|
91
|
+
puts "processing #{file}" unless @quiet
|
76
92
|
if @multi
|
77
|
-
|
78
|
-
|
79
|
-
File.
|
93
|
+
pattern1 = "**" "/" "*.jpg"
|
94
|
+
pattern2 = "**" "/" "*.jpeg"
|
95
|
+
if File.fnmatch(pattern1, file, File::FNM_CASEFOLD) || File.fnmatch(pattern2, file, File::FNM_CASEFOLD)
|
96
|
+
if already_done?(file)
|
97
|
+
puts "EXIF signature detected, skipping." unless @quiet
|
98
|
+
else
|
99
|
+
puts "EXIF data not found attempting compression..." unless @quiet
|
100
|
+
process_file(file)
|
101
|
+
end
|
102
|
+
File.open("images_processed_list.txt", "a+") { |f| f << file + "\n"}
|
103
|
+
else
|
104
|
+
puts "no file match" unless @quiet
|
105
|
+
end
|
80
106
|
else
|
81
|
-
|
82
|
-
|
107
|
+
pattern1 = '*.jpg'
|
108
|
+
pattern2 = '*.jpeg'
|
109
|
+
if File.fnmatch(pattern1, file, File::FNM_CASEFOLD) || File.fnmatch(pattern2, file, File::FNM_CASEFOLD)
|
110
|
+
if already_done?(file)
|
111
|
+
puts "EXIF signature detected, skipping." unless @quiet
|
112
|
+
else
|
113
|
+
puts "EXIF data not found attempting compression..." unless @quiet
|
114
|
+
process_file(file)
|
115
|
+
end
|
83
116
|
puts @image_results.to_json
|
117
|
+
else
|
118
|
+
puts "no file match" unless @quiet
|
119
|
+
end
|
84
120
|
end
|
85
121
|
totalsavings = totalsavings.to_i + @savings.to_i
|
86
122
|
puts "Total savings (in bytes): " + totalsavings.to_s unless @quiet
|