camouflage 0.0.1 → 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/README.md CHANGED
@@ -1,4 +1,9 @@
1
1
  What is Camouflage
2
2
  ==================
3
3
 
4
- A work in progress to find the best way to disguise an image to be used as a page background for a track public page.
4
+ A work in progress to find the best way to disguise an image to be used as a page background for a track public page.
5
+
6
+ How to use
7
+ ==========
8
+
9
+ Install as a gem, then type `camouflage [image_path]`
data/bin/camouflage ADDED
@@ -0,0 +1,91 @@
1
+ #!/usr/bin/env ruby
2
+ begin
3
+ require 'camouflage'
4
+ rescue LoadError
5
+ require 'rubygems'
6
+ require 'camouflage'
7
+ end
8
+
9
+ require 'optparse'
10
+ class CamouflageParser
11
+ def self.parse(args)
12
+ options = {}
13
+ options[:output] = :html
14
+
15
+ opt_parser = OptionParser.new do |opts|
16
+ opts.banner = "Usage: #{File.basename($0)} [image_path]"
17
+
18
+ opts.separator ''
19
+ opts.separator 'Specific options:'
20
+
21
+ opts.on('-r', '--blur_radius N', Integer, 'Blur radius') do |n|
22
+ options[:blur_radius] = n
23
+ end
24
+
25
+ opts.on('-s', '--blur_sigma N', Integer, 'Blur sigma') do |n|
26
+ options[:blur_sigma] = n
27
+ end
28
+
29
+ opts.on('-w', '--width N', Integer, 'Width to fit into') do |n|
30
+ options[:width] = n
31
+ end
32
+
33
+ opts.on('-h', '--height N', Integer, 'Height to fit into') do |n|
34
+ options[:height] = n
35
+ end
36
+
37
+ opts.on("--output [OUTPUT]", [:image, :html], 'What to show') do |o|
38
+ options[:output] = o
39
+ end
40
+
41
+ opts.separator ''
42
+ opts.separator 'Common options:'
43
+
44
+ opts.on_tail('-h', '--help', 'Show this message') do
45
+ puts opts
46
+ exit
47
+ end
48
+
49
+ # Another typical switch to print the version.
50
+ opts.on_tail('--version', 'Show version') do
51
+ puts Camouflage::VERSION
52
+ exit
53
+ end
54
+ end
55
+
56
+ opt_parser.parse!(args)
57
+ options
58
+ end
59
+ end
60
+
61
+ if ARGV.empty?
62
+ abort "Please specify an image, e.g. `#{File.basename($0)} example.jpg'"
63
+ end
64
+ options = CamouflageParser.parse(ARGV)
65
+ output = options.delete(:output)
66
+ puts "Creating camouflage version of #{ARGV[0]} (options: #{options}):"
67
+ disguised_image_path = Camouflage.disguise ARGV[0], options
68
+ puts "Image created!"
69
+
70
+ case output
71
+ when :image
72
+ `open #{disguised_image_path}`
73
+ when :html
74
+ File.open '/tmp/camouflage.html', 'w' do |f|
75
+ f.write <<-HTML
76
+ <!DOCTYPE html>
77
+ <html>
78
+ <head>
79
+ <meta charset="UTF-8" />
80
+ <title>Camouflage output</title>
81
+ </head>
82
+ <body style="
83
+ background-image: url('#{disguised_image_path}');
84
+ background-size: 100%;
85
+ background-repeat: no-repeat;">
86
+ </body>
87
+ </html>
88
+ HTML
89
+ end
90
+ `open /tmp/camouflage.html`
91
+ end
data/lib/camouflage.rb ADDED
@@ -0,0 +1,75 @@
1
+ require 'carrierwave'
2
+ require 'carrierwave/processing/mini_magick'
3
+
4
+ # Provides a methods to disguise a local image
5
+ module Camouflage
6
+
7
+ # Returns the path to a blurred version of the image located at +image_path+.
8
+ #
9
+ # @param [String] image_path Local path to an image file
10
+ # @param [Hash] options the options to disguise an image
11
+ # @option options [Integer] :blur_radius Radius of the blur effect to apply
12
+ # @option options [Integer] :blur_sigma Sigma of the blur effect to apply
13
+ # @option options [Integer] :width Width to resize the image to fit
14
+ # @option options [Integer] :height Height to resize the image to fit
15
+ #
16
+ # @note More info about Blur effect parameter on
17
+ # {http://www.imagemagick.org/Usage/blur/#blur_args Imagemagick docs}
18
+ #
19
+ # @example Disguise a local JPEG file
20
+ # Camouflage.disguise("example.jpg")
21
+ #
22
+ # # => /uploads/disguised_example.jpg
23
+ def self.disguise(image_path, options = {})
24
+ camouflage_uploader = CamouflageUploader.new(options)
25
+ camouflage_uploader.store! File.open(image_path)
26
+ camouflage_uploader.path
27
+ end
28
+
29
+ # Returns the width and height of the image located at +image_path+.
30
+ #
31
+ # @param [String] image_path Local path to an image file
32
+ # @return [Array] Local path to the disguised image file
33
+ #
34
+ # @example Size a local JPEG file
35
+ # Camouflage.size_of("example.jpg") # => [448, 604]
36
+ def self.size_of(image_path)
37
+ image = MiniMagick::Image.open image_path
38
+ [image['width'], image['height']]
39
+ end
40
+
41
+ class CamouflageUploader < CarrierWave::Uploader::Base
42
+ storage :file
43
+ attr_accessor :blur_radius, :blur_sigma, :width, :height
44
+
45
+ def initialize(options = {})
46
+ @blur_radius = options.fetch(:blur_radius, 0)
47
+ @blur_sigma = options.fetch(:blur_sigma, 30)
48
+ @width = options.fetch(:width, 300)
49
+ @height = options.fetch(:height, 300)
50
+ super(nil, nil)
51
+ end
52
+
53
+ def store_dir
54
+ '/tmp'
55
+ end
56
+
57
+ include CarrierWave::MiniMagick
58
+ process :resize
59
+ process :blur
60
+
61
+ private
62
+
63
+ def blur
64
+ effect! {|img| img.blur "#{@blur_radius}x#{@blur_sigma}"}
65
+ end
66
+
67
+ def resize
68
+ effect! {|img| img.resize "#{@width}x#{@height}"}
69
+ end
70
+
71
+ def effect!
72
+ manipulate! {|img| yield(img) && img}
73
+ end
74
+ end
75
+ end
@@ -1,3 +1,3 @@
1
1
  module Camouflage
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.3'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: camouflage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-18 00:00:00.000000000 Z
12
+ date: 2013-06-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: carrierwave
@@ -126,11 +126,14 @@ dependencies:
126
126
  description: Disguise an image by applying graphical effects
127
127
  email:
128
128
  - claudio@topspinmedia.com
129
- executables: []
129
+ executables:
130
+ - camouflage
130
131
  extensions: []
131
132
  extra_rdoc_files: []
132
133
  files:
134
+ - bin/camouflage
133
135
  - lib/camouflage/version.rb
136
+ - lib/camouflage.rb
134
137
  - MIT-LICENSE
135
138
  - README.md
136
139
  homepage: https://github.com/topspin/camouflage
@@ -146,12 +149,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
146
149
  - - ! '>='
147
150
  - !ruby/object:Gem::Version
148
151
  version: '0'
152
+ segments:
153
+ - 0
154
+ hash: 1459006739667432163
149
155
  required_rubygems_version: !ruby/object:Gem::Requirement
150
156
  none: false
151
157
  requirements:
152
158
  - - ! '>='
153
159
  - !ruby/object:Gem::Version
154
160
  version: '0'
161
+ segments:
162
+ - 0
163
+ hash: 1459006739667432163
155
164
  requirements: []
156
165
  rubyforge_project:
157
166
  rubygems_version: 1.8.23