smushkid 0.0.1

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.
Files changed (3) hide show
  1. data/bin/smushkid +2 -0
  2. data/lib/smushkid.rb +71 -0
  3. metadata +48 -0
data/bin/smushkid ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ruby
2
+ require 'smushkid'
data/lib/smushkid.rb ADDED
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+ require 'find'
3
+ require 'RMagick'
4
+ require 'json'
5
+
6
+ path = ARGV[0]
7
+ quality = ARGV[1]
8
+
9
+ totalsavings = 0
10
+ File.open("images_processed.json", "a+") { |f| f << "{ \"image_results\" : [ " }
11
+ Find.find(path) do |file|
12
+ pattern = '**' '/' '*.jpg'
13
+ if File.fnmatch(pattern, file, File::FNM_CASEFOLD)
14
+ puts "matched: #{file}"
15
+ simg = Magick::Image::read(file).first
16
+ source_image = {
17
+ 'format' => simg.format,
18
+ 'filesize' => simg.filesize,
19
+ 'geometry_cols' => simg.columns,
20
+ 'geometry_rows' => simg.rows,
21
+ 'resolution_x' => simg.x_resolution.to_i,
22
+ 'resolution_y' => simg.y_resolution.to_i,
23
+ 'depth' => simg.depth,
24
+ 'colors' => simg.number_colors,
25
+ 'ppi' => simg.units
26
+ }
27
+ target_file = File.dirname(file) + "/" + "smaller-" + File.basename(file)
28
+ simg.strip!
29
+ simg.quantize 32
30
+ simg.write(target_file) do
31
+ self.quality = quality.to_i
32
+ end
33
+ timg = Magick::Image::read(target_file).first
34
+ target_image = {
35
+ 'format' => timg.format,
36
+ 'filesize' => timg.filesize,
37
+ 'geometry_cols' => timg.columns,
38
+ 'geometry_rows' => timg.rows,
39
+ 'resolution_x' => timg.x_resolution.to_i,
40
+ 'resolution_y' => timg.y_resolution.to_i,
41
+ 'depth' => timg.depth,
42
+ 'colors' => timg.number_colors,
43
+ 'ppi' => timg.units
44
+ }
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
+ if @savings < 0
50
+ @savings = 0
51
+ puts "no space savings acheived deleting #{target_file}"
52
+ File.delete(target_file)
53
+ else
54
+ puts "space savings! replacing with #{simg}"
55
+ puts "move #{target_file} to #{file}"
56
+ File.rename target_file, file
57
+ 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
+ end
67
+ File.open("images_processed.json", "a+") { |f| f << "]}" }
68
+
69
+
70
+
71
+
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smushkid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Fraley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-21 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Compress and optimize JPG images with RMagick
15
+ email:
16
+ - whiskeytangoeng@gmail.com
17
+ executables:
18
+ - smushkid
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/smushkid.rb
23
+ - bin/smushkid
24
+ homepage: http://github.com/zippykid/smushkid
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.24
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Compress and Optimize JPG images
48
+ test_files: []