thumbkit 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/README.md CHANGED
@@ -1,14 +1,14 @@
1
1
  # Thumbkit
2
2
 
3
+ Thumbkit makes thumbnails from a variety of media types.
4
+ Thumbkit is designed to work with carrierwave but does not require it.
5
+
3
6
  > it's like quicklook for carrierwave :)
4
7
  >
5
8
  > -- <cite>[Emmanuel Gomez][1]</cite>
6
9
 
7
10
  [1]:https://github.com/emmanuel
8
11
 
9
- Thumbkit makes thumbnails from a variety of media types.
10
- Thumbkit is designed to work with carrierwave but does not require it.
11
-
12
12
  ## Synopsis
13
13
 
14
14
  ```ruby
@@ -23,14 +23,18 @@ See [Usage](#usage) below for more examples
23
23
 
24
24
  Add this line to your application's Gemfile:
25
25
 
26
- gem 'thumbkit'
27
- gem 'mini_magick' # For text or image thumbnails
28
- gem 'waveform' # For audio thumbnails
29
- gem 'oily_png' # Optional, for presumably faster audio thumbnails
26
+ ```ruby
27
+ gem 'thumbkit'
28
+ gem 'mini_magick' # For text or image thumbnails
29
+ gem 'waveform' # For audio thumbnails
30
+ gem 'oily_png' # Optional, for presumably faster audio thumbnails
31
+ ```
30
32
 
31
33
  And then execute:
32
34
 
33
- $ bundle
35
+ ```shell
36
+ $ bundle
37
+ ```
34
38
 
35
39
  Please see [Requirements](#requirements) for more information about each
36
40
  thumbnail type.
@@ -72,8 +76,11 @@ other than .wav files.
72
76
 
73
77
  See https://github.com/benalavi/waveform for more on requirements.
74
78
 
75
- NOTE: As of 0.0.3 waveform fails on mono files (benalavi/waveform#4,
76
- benalavi/waveform#5. I've forked and fixed (see benalavi/waveform#6). Until my
79
+ NOTE: As of 0.0.3 waveform fails on mono files
80
+ ([benalavi/waveform#4](https://github.com/benalavi/waveform/issues/4),
81
+ [benalavi/waveform#5](https://github.com/benalavi/waveform/issues/5)).
82
+ I've forked and fixed the issue (see
83
+ [benalavi/waveform#6](https://github.com/benalavi/waveform/pull/6)). Until my
77
84
  fix gets merged in you can use https://github.com/amiel/waveform/tree/thumbkit.
78
85
  Like so:
79
86
 
@@ -89,49 +96,13 @@ Thumbkit takes a path to a file, and saves a thumbnail for that file regardless
89
96
  of type. Certain types require different gems, but none are dependencies so
90
97
  you'll have to install them yourself.
91
98
 
92
- All settings can be set globally. These are the defaults:
93
-
94
- ### Configuration
95
-
96
- ```ruby
97
- Thumbkit.defaults = {
98
- width: 200, height: 200,
99
- gravity: 'Center',
100
- colors: { foreground: '#888888', background: '#eeeeee' },
101
- font: {
102
- family: 'Arial-Regular',
103
- pointsize: '18',
104
- direction: :auto,
105
- },
106
- }
107
- ```
108
-
109
- Setting `Thumbkit.defaults=` will deep merge. So setting one option is possible
110
- with:
111
-
112
- ```ruby
113
- Thumbkit.defaults = { colors: { foreground: '#FF69B4' } } # HOT PINK
114
- ```
115
-
116
- #### Font options
117
-
118
- The list of fonts available to imagemagick can be found with
119
- `identify -list Font`
120
-
121
- #### Gravity Options
122
-
123
- A list of gravity options can be found with `identify -list Gravity`
124
-
125
- See http://www.imagemagick.org/script/command-line-options.php#gravity for more
126
- information.
127
-
128
99
  ### Image thumbnails
129
100
 
130
101
  ```ruby
131
102
  Thumbkit.new('path/to/image.jpg').write_thumbnail # => 'path/to/image.jpg'
132
103
  ```
133
104
 
134
- Will write a 60x60 cropped image to `path/to/image.jpg`.
105
+ Will write a 200x200 cropped image to `path/to/image.jpg`.
135
106
 
136
107
  The format of the output file will depend on the extension of the output path
137
108
  and defaults to the same as the input file.
@@ -142,13 +113,13 @@ and defaults to the same as the input file.
142
113
  text = Thumbkit.new('path/to/text_file.txt')
143
114
 
144
115
  text.write_thumbnail(nil, {
145
- width: 200, height: 200,
116
+ width: 160, height: 160,
146
117
  colors: { foreground: '#663854' },
147
118
  font: { pointsize: '18' },
148
119
  }) # => 'path/to/text_file.png'
149
120
  ```
150
121
 
151
- Will write a 200x200 cropped image to `path/to/text_file.png`.
122
+ Will write a 160x160 cropped image to `path/to/text_file.png`.
152
123
 
153
124
  The format of output will depend on the extension of the output path provided
154
125
  but defaults to .png.
@@ -176,7 +147,7 @@ but defaults to .png.
176
147
  }) # => 'path/to/output.png'
177
148
  ```
178
149
 
179
- Will write a 60x60 cropped image to `path/to/output.png`.
150
+ Will write a 200x200 cropped image to `path/to/output.png`.
180
151
 
181
152
  Note that while imagemagick supports most color specification formats, waveform
182
153
  only takes 6 digit hex values. However, there is one special case for the symbol
@@ -196,22 +167,93 @@ NOT YET IMPLEMENTED
196
167
 
197
168
  ### CarrierWave usage
198
169
 
199
- NOT YET IMPLEMENTED
200
-
201
170
  ```ruby
202
171
  class MyUploader < CarrierWave::Uploader::Base
203
- include CarrierWave::Thumbkit
172
+ include Thumbkit::Adapters::CarrierWave
204
173
 
205
174
  version :thumbnail do
206
- process :thumbkit => [200, 200, { colors: { foreground: '#cccccc' } }]
175
+ # See Configuration below for more about options.
176
+ process thumbkit: [200, 200, { colors: { foreground: '#cccccc' } }]
177
+
178
+ # This tells CarrierWave where the version file can be found since
179
+ # thumbkit can write a to a file with a different extension than the
180
+ # original.
181
+ #
182
+ # See https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Customize-your-version-file-names
183
+ # for more about
184
+ def full_filename(for_file = model.file.file)
185
+ [version_name, thumbkit_filename(for_file)].join('_')
186
+ end
207
187
  end
208
188
  end
209
189
  ```
210
190
 
191
+ ## Configuration
192
+
193
+ All settings can be set globally. These are the defaults:
194
+
195
+ ```ruby
196
+ Thumbkit.defaults = {
197
+ width: 200, height: 200,
198
+ gravity: 'Center',
199
+ colors: { foreground: '#888888', background: '#eeeeee' },
200
+ font: {
201
+ family: 'Arial-Regular',
202
+ pointsize: '18',
203
+ direction: :auto,
204
+ },
205
+ }
206
+ ```
207
+
208
+ Setting `Thumbkit.defaults=` will deep merge. So setting one option is possible
209
+ with:
210
+
211
+ ```ruby
212
+ Thumbkit.defaults = { colors: { foreground: '#FF69B4' } } # HOT PINK
213
+ ```
214
+
215
+ #### Font options
216
+
217
+ The list of fonts available to imagemagick can be found with
218
+ `identify -list Font`
219
+
220
+ #### Gravity Options
221
+
222
+ A list of gravity options can be found with `identify -list Gravity`
223
+
224
+ See http://www.imagemagick.org/script/command-line-options.php#gravity for more
225
+ information.
226
+
227
+ #### Processors
228
+
229
+ Built-in processors can be found in `lib/thumbkit/processor`.
230
+
231
+ Adding a processor mapping:
232
+
233
+ ```ruby
234
+ Thumbkit.processors['jpeg'] = 'Image'
235
+ ```
236
+
237
+ Adding a processor:
238
+
239
+ ```ruby
240
+ class Thumbkit::Processors::Doc
241
+ def write
242
+ # use `path` to generate `outfile`
243
+
244
+ # always return the generated filename
245
+ outfile
246
+ end
247
+ end
248
+
249
+ Thumbkit.processors['doc'] = 'Doc'
250
+ ```
251
+
211
252
  ## Other plans
212
253
 
213
- * Accept a StringIO instead of a pathname
214
- * Maybe use filemagic if available
254
+ * Optionally accept a StringIO instead of a pathname
255
+ * Maybe use filemagic/mime-type if available
256
+ * Paperclip processor
215
257
  * Processors:
216
258
  * Composite
217
259
  * HTML
@@ -0,0 +1,36 @@
1
+ # Example usage:
2
+ #
3
+ # class MyUploader < CarrierWave::Uploader::Base
4
+ # include Thumbkit::Adapters::CarrierWave
5
+ #
6
+ # version :thumbnail do
7
+ # # See [Configuration](#configuration) below for more about options.
8
+ # process thumbkit: [200, 200, { colors: { foreground: '#cccccc' } }]
9
+ #
10
+ # # This tells CarrierWave where the version file can be found since
11
+ # # thumbkit can write a to a file with a different extension than the
12
+ # # original.
13
+ # #
14
+ # # See https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Customize-your-version-file-names
15
+ # # for more about
16
+ # def full_filename(for_file = model.file.file)
17
+ # [version_name, thumbkit_filename(for_file)].join('_')
18
+ # end
19
+ # end
20
+ # end
21
+ #
22
+ module Thumbkit::Adapters::CarrierWave
23
+ def thumbkit(width, height, options = {})
24
+ options.merge!(width: width, height: height)
25
+ cache_stored_file! if !cached?
26
+ thumbkit = Thumbkit.new(current_path)
27
+ result = thumbkit.write_thumbnail(nil, options)
28
+ @file = CarrierWave::SanitizedFile.new(result)
29
+ end
30
+
31
+ # Given a filename, return the filename that Thumbkit would generate.
32
+ # This should not do any processing.
33
+ def thumbkit_filename(for_file)
34
+ File.basename(Thumbkit.new(for_file).processor_instance.outfile)
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Thumbkit::Adapters
2
+ autoload :CarrierWave, 'thumbkit/adapters/carrierwave'
3
+ end
@@ -34,7 +34,7 @@ class Thumbkit::Processor::Image < Thumbkit::Processor
34
34
  rows = (scale * (rows + 0.5)).round
35
35
  cmd.resize "#{ cols }x#{ rows }"
36
36
  end
37
- cmd.gravity options[:gravity].to_s
37
+ cmd.gravity options[:gravity].to_s if options[:gravity]
38
38
  cmd.background options[:colors][:background].to_s
39
39
 
40
40
  if cols != options[:width] || rows != options[:height]
@@ -59,7 +59,8 @@ class Thumbkit::Processor::Text < Thumbkit::Processor
59
59
  mogrify.antialias
60
60
  mogrify.font options[:font][:family]
61
61
  mogrify.direction direction if direction
62
- mogrify.gravity options[:gravity].to_s
62
+ mogrify.gravity options[:gravity].to_s if options[:gravity]
63
+
63
64
  # While we convert to png, imagemagick will still output the format given
64
65
  # by the extension. This allows users to costumize the output with the
65
66
  # outfile extension.
@@ -70,6 +71,8 @@ class Thumbkit::Processor::Text < Thumbkit::Processor
70
71
  mogrify.crop "#{ options[:width] }x#{ options[:height] }+0+0"
71
72
  mogrify.extent "#{ options[:width] }x#{ options[:height] }"
72
73
  mogrify << '+repage'
74
+ # This extra gravity call prevents left-to-right text from rendering centered
75
+ mogrify.gravity 'NorthWest'
73
76
  mogrify.write outfile
74
77
  mogrify << "label:@#{path}"
75
78
  end
@@ -7,6 +7,7 @@ class Thumbkit::Processor
7
7
  @processors ||= {
8
8
  'png' => 'Image',
9
9
  'jpg' => 'Image',
10
+ 'gif' => 'Image',
10
11
  'txt' => 'Text',
11
12
  'md' => 'Text',
12
13
  'mp3' => 'Audio',
@@ -1,3 +1,3 @@
1
1
  class Thumbkit
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/thumbkit.rb CHANGED
@@ -3,6 +3,7 @@ require "thumbkit/version"
3
3
  class Thumbkit
4
4
  autoload :Processor, 'thumbkit/processor'
5
5
  autoload :Options, 'thumbkit/options'
6
+ autoload :Adapters, 'thumbkit/adapters'
6
7
 
7
8
  attr_accessor :path, :filename, :type
8
9
 
@@ -10,7 +11,7 @@ class Thumbkit
10
11
  @defaults ||= Thumbkit::Options.new({
11
12
  width: 200,
12
13
  height: 200,
13
- # Try `identify -list Gravity` for a list of available options
14
+ # Run `identify -list Gravity` for a list of available options
14
15
  gravity: 'Center',
15
16
  colors: {
16
17
  # Colors must be 6-digit hex
@@ -18,7 +19,7 @@ class Thumbkit
18
19
  foreground: '#888888',
19
20
  },
20
21
  font: {
21
- # Try `identify -list Font` for available font options
22
+ # Run `identify -list Font` for available font options
22
23
  family: 'Arial-Regular',
23
24
  size: '18', # In points
24
25
  direction: :auto, # nil, :auto, 'right-to-left', or 'left-to-right'
@@ -26,17 +27,25 @@ class Thumbkit
26
27
  })
27
28
  end
28
29
 
30
+ def self.processors
31
+ Thumbkit::Processor.processors
32
+ end
33
+
29
34
  def initialize(path)
30
35
  @path = File.expand_path(path)
31
36
  @filename = File.basename(@path)
32
- @type = File.extname(filename)[1..-1]
37
+ @type = File.extname(@filename)[1..-1]
33
38
  end
34
39
 
35
40
  def processor
36
41
  @processor ||= Thumbkit::Processor.processor_for(type)
37
42
  end
38
43
 
44
+ def processor_instance(outfile = nil, options = {})
45
+ processor.new(path, outfile, options)
46
+ end
47
+
39
48
  def write_thumbnail(outfile = nil, options = {})
40
- processor.new(path, outfile, options).write
49
+ processor_instance(outfile, options).write
41
50
  end
42
51
  end
@@ -68,10 +68,11 @@ describe Thumbkit::Processor::Text do
68
68
 
69
69
  context 'with some greek letters' do
70
70
  let(:fixture) { 'greek.txt' }
71
+ let(:options) { { width: 400, height: 400 } }
71
72
  let(:outfile) { path_for_output('greek.png').to_s }
72
73
  it('writes a file') { File.should exist(subject) }
73
74
  it('autodetects left-to-right') { processor.__send__(:direction).should == 'left-to-right' }
74
- its_size_should_be('200x200')
75
+ its_size_should_be('400x400')
75
76
  its_mimetype_should_be('image/png')
76
77
  # Manually check the file to verify unicode stuff worked
77
78
  end
data/thumbkit.gemspec CHANGED
@@ -5,13 +5,13 @@ Gem::Specification.new do |gem|
5
5
  gem.authors = ["Amiel Martin"]
6
6
  gem.email = ["amiel@carnesmedia.com"]
7
7
  gem.description = %q{Thumbkit makes thumbnails!}
8
- gem.summary = %q{Thumbkit makes thumbnails from a variety of media types. Planned: Audio, Text, Image, and Video.}
8
+ gem.summary = %q{Thumbkit makes thumbnails from a variety of media types.}
9
9
  gem.homepage = "http://github.com/carnesmedia/thumbkit"
10
10
 
11
11
  # TODO: Remove dependency on git
12
12
  gem.files = `git ls-files`.split($\)
13
13
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.test_files = gem.files.grep(%r{^(spec)/})
15
15
  gem.name = "thumbkit"
16
16
  gem.require_paths = ["lib"]
17
17
  gem.version = Thumbkit::VERSION
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thumbkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
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: 2012-05-01 00:00:00.000000000 Z
12
+ date: 2012-05-02 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Thumbkit makes thumbnails!
15
15
  email:
@@ -25,6 +25,8 @@ files:
25
25
  - README.md
26
26
  - Rakefile
27
27
  - lib/thumbkit.rb
28
+ - lib/thumbkit/adapters.rb
29
+ - lib/thumbkit/adapters/carrierwave.rb
28
30
  - lib/thumbkit/options.rb
29
31
  - lib/thumbkit/processor.rb
30
32
  - lib/thumbkit/processor/audio.rb
@@ -69,8 +71,7 @@ rubyforge_project:
69
71
  rubygems_version: 1.8.15
70
72
  signing_key:
71
73
  specification_version: 3
72
- summary: ! 'Thumbkit makes thumbnails from a variety of media types. Planned: Audio,
73
- Text, Image, and Video.'
74
+ summary: Thumbkit makes thumbnails from a variety of media types.
74
75
  test_files:
75
76
  - spec/fixtures/16Hz-20kHz-Exp-1f-5sec.mp3
76
77
  - spec/fixtures/arabic.txt