mojo_magick 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ fixtures/tmp
2
+ *~
data/README.md ADDED
@@ -0,0 +1,146 @@
1
+ MojoMagick
2
+ ==========
3
+
4
+ MojoMagick is a "dog simple, do very little" image library. It is basically a couple of stateless
5
+ module methods that make it somewhat more convenient than calling ImageScience by hand.
6
+
7
+ The main reason to use MojoMagick is that you should consolidate your ImageScience calls into
8
+ one place, so why not do it here? If you improve on this tool, send me the patch.
9
+
10
+ This tool came about because I wanted a fast, simple, lightweight, nothing-goes-wrong-with-it-
11
+ because-it's-too-simple-to-break image tool.
12
+
13
+ Examples
14
+ ========
15
+
16
+ ## Image Resizing
17
+
18
+ ### Obtain the size of an image (assuming image is "120wx222h")
19
+
20
+ dimensions = MojoMagick::get_image_size(test_image)
21
+ # ==> dimensions now holds a hash: {:height => 120, :width => 222}
22
+
23
+ ### Resize an image so that it fits within a 100w x 200h bounding box
24
+ (Note: this will scale an image either up or down to fit these dimensions which may not be what you want.)
25
+ In this example, we overwrite our image, but if you pass in a different file for the second file name, a new file will be created with the resized dimensions
26
+
27
+ MojoMagick::resize('/img/test.jpg', '/img/test.jpg', {:width=>100, :height=>200})
28
+
29
+ ### Resize an image so that it fills a 100 x 100 bounding box
30
+
31
+ After this transformation, your image's short side will be 100px. This will preserve the aspect ratio.
32
+
33
+ MojoMagick::resize('/img/infile.jpg', '/img/outfile.jpg', {:width=>100, :height=>100, :fill => true})
34
+
35
+ ### Resize an image so that it fills and is cropped to a 100 x 100 bounding box
36
+
37
+ After this transformation, your image will be 100x100. But it does not distort the images. It crops out of the Center.
38
+
39
+ MojoMagick::resize('/img/infile.jpg', '/img/outfile.jpg', {:width=>100, :height=>100, :fill => true, :crop => true})
40
+
41
+ ### Code sample of how to shrink all jpg's in a folder
42
+
43
+ require 'mojo_magick'
44
+
45
+ image_folder = '/tmp/img'
46
+ Dir::glob(File::join(image_folder, '*.jpg')).each do |image|
47
+ begin
48
+ # shrink all the images *in place* to no bigger than 60pix x 60pix
49
+ MojoMagick::shrink(image, image, {:width => 60, :height => 60})
50
+ puts "Shrunk: #{image}"
51
+ rescue MojoMagick::MojoFailed => e
52
+ STDERR.puts "Unable to shrink image '#{image}' - probably an invalid image\n#{e.message}"
53
+ rescue MojoMagick::MojoMagickException => e
54
+ STDERR.puts "Unknown exception on image '#{image}'\n#{e.message}"
55
+ end
56
+ end
57
+
58
+ ## Setting Memory/Resource limits for ImageMagick
59
+ Be sure you're upgraded to the current release of ImageMagick.
60
+
61
+ set limits on disk, area, map and ram usage
62
+ obtain/print a hash of default limits:
63
+
64
+ puts MojoMagick::get_default_limits.inspect
65
+
66
+ current\_limits shows same values:
67
+
68
+ puts MojoMagick::get_current_limits.inspect
69
+
70
+ MojoMagick::set_limits(:area => '32mb', :disk => '0', :memory => '64mb', :map => '32mb')
71
+ puts MojoMagick::get_current_limits.inspect
72
+
73
+ As of ImageMagick 6.6, you also have the ability to set `:threads` and `:time`. Read [ImageMagick docs on limits](http://www.imagemagick.org/script/command-line-options.php#limit) for more info.
74
+
75
+ ## For more complex operations (thanks to Elliot Nelson for adding this code to the system)
76
+
77
+ Two command-line builders, #convert and #mogrify, have been added to simplify
78
+ complex commands.
79
+
80
+ ### using #convert
81
+
82
+ MojoMagick::convert('source.jpg', 'dest.jpg') do |c|
83
+ c.crop '250x250+0+0'
84
+ c.repage!
85
+ c.strip
86
+ c.set 'comment', 'my favorite file'
87
+ end
88
+
89
+ # Equivalent to:
90
+ MojoMagick::raw_command('convert', 'source.jpg -crop 250x250+0+0 +repage -strip -set comment "my favorite file" dest.jpg')
91
+
92
+
93
+ ### using #mogrify
94
+
95
+ MojoMagick::mogrify('image.jpg') {|i| i.shave '10x10'}
96
+
97
+ # Equivalent to:
98
+
99
+ MojoMagick::raw_command('mogrify', '-shave 10x10 image.jpg')
100
+
101
+ # Example showing some additional options:
102
+ # assuming binary data that is rgb, 8bit depth and 10x20 pixels, :format => :rgb, :depth => 8, :size => '10x20'OAu
103
+
104
+ MojoMagick::convert do |c|
105
+ c.file 'source.jpg'
106
+ c.blob my_binary_data, :format => :rgb, :depth => 8, :size => '10x20'
107
+ c.append
108
+ c.crop '256x256+0+0'
109
+ c.repage!
110
+ c.file 'output.jpg'
111
+ end
112
+
113
+ # Use .file to specify file names, .blob to create and include a tempfile. The
114
+ # bang (!) can be appended to command names to use the '+' versions
115
+ # instead of '-' versions.
116
+
117
+ ### Create a brand new image from data
118
+
119
+ binary_data = '1111222233334444'
120
+ MojoMagick::convert do |c|
121
+ c.rgba8 binary_data, :format => :rgba, :depth => 8, :size => '2x2'
122
+ c.file 'output.jpg'
123
+ end
124
+
125
+ Availablility
126
+ =============
127
+
128
+ * [SVN Repo access](http://trac.misuse.org/science/wiki/MojoMagick)
129
+ * Concact the author or discuss MojoMagick : [misuse.org](http://www.misuse.org/science/2008/01/30/mojomagick-ruby-image-library-for-imagemagick/)
130
+
131
+
132
+ #### Updates by Jon Rogers Aug 2011 (http://github.com/bunnymatic)
133
+ * added gemspec for building gem with bundler
134
+ * updated tests for ImageMagick 6.6
135
+ * added ability to do fill + crop resizing
136
+ * bumped version to 0.3.0
137
+ * [new github repo](https://github.com/bunnymatic/mojo_magick)
138
+
139
+
140
+ References
141
+ ==========
142
+
143
+ * [ImageMagick](http://www.imagemagick.org/)
144
+
145
+ Copyright (c) 2008 Steve Midgley, released under the MIT license
146
+ Credit to Elliot Nelson for significant code contributions. Thanks Elliot!
@@ -0,0 +1,3 @@
1
+ module MojoMagick
2
+ VERSION = '0.4.0'
3
+ end
data/lib/mojo_magick.rb CHANGED
@@ -52,8 +52,6 @@ require 'tempfile'
52
52
  #
53
53
  module MojoMagick
54
54
 
55
- VERSION = "0.2.0"
56
-
57
55
  class MojoMagickException < StandardError; end
58
56
  class MojoError < MojoMagickException; end
59
57
  class MojoFailed < MojoMagickException; end
@@ -118,8 +116,8 @@ module MojoMagick
118
116
  raise MojoMagickError, "Unknown options for method resize: #{options.inspect}"
119
117
  end
120
118
  if !options[:fill].nil? && !options[:crop].nil?
119
+ extras << "-gravity Center"
121
120
  extras << "-extent #{geometry}"
122
- extras << "-gravity center"
123
121
  end
124
122
  retval = raw_command("convert", "\"#{source_file}\" -resize #{geometry}#{scale_options} #{extras.join(' ')} \"#{dest_file}\"")
125
123
  dest_file
@@ -154,6 +152,23 @@ module MojoMagick
154
152
  raw_command('mogrify', opts.to_s)
155
153
  end
156
154
 
155
+
156
+ def MojoMagick::tempfile(*opts)
157
+ begin
158
+ data = opts[0]
159
+ rest = opts[1]
160
+ ext = rest && rest[:format]
161
+ file = Tempfile.new(["mojo", ext ? '.' + ext.to_s : ''])
162
+ file.binmode
163
+ file.write(data)
164
+ file.path
165
+ rescue Exception => ex
166
+ raise
167
+ end
168
+ ensure
169
+ file.close
170
+ end
171
+
157
172
  # Option builder used in #convert and #mogrify helpers.
158
173
  class OptBuilder
159
174
  def initialize
@@ -180,8 +195,13 @@ module MojoMagick
180
195
  alias files file
181
196
 
182
197
  # Create a temporary file for the given image and add to command line
183
- def blob(arg)
184
- file MojoMagick::tempfile(arg)
198
+ def blob(*args)
199
+ data = args[0]
200
+ opts = args[1] || {}
201
+ [:format, :depth, :size].each do |opt|
202
+ self.send(opt, opts[opt].to_s) if opts[opt]
203
+ end
204
+ file MojoMagick::tempfile(data, opts)
185
205
  end
186
206
 
187
207
  # Generic commands. Arguments will be formatted if necessary
@@ -211,15 +231,6 @@ module MojoMagick
211
231
  end
212
232
  end
213
233
  end
214
-
215
- def MojoMagick::tempfile(data)
216
- file = Tempfile.new("mojo")
217
- file.binmode
218
- file.write(data)
219
- file.path
220
- ensure
221
- file.close
222
- end
223
234
 
224
235
  end # MojoMagick
225
236
 
@@ -0,0 +1,21 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "mojo_magick/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "mojo_magick"
6
+ s.version = MojoMagick::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Steve Midgley", "Elliot Nelson", "Jon Rogers"]
9
+ s.email = ["public@misuse.org", "j@2rye.com"]
10
+ s.homepage = "http://github.com/bunnymatic/mojo_magick"
11
+ s.summary = "mojo_magick-#{MojoMagick::VERSION}"
12
+ s.description = %q{Simple Ruby stateless module interface to imagemagick.}
13
+
14
+ s.rubyforge_project = "mojo_magick"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ s.add_development_dependency('rake')
21
+ end
@@ -78,7 +78,6 @@ class MojoMagickTest < Test::Unit::TestCase
78
78
  assert_equal 1000, new_dimensions[:height]
79
79
  assert_equal 666, new_dimensions[:width]
80
80
 
81
-
82
81
  # test bad images
83
82
  bad_image = File::join(@working_path, 'not_an_image.jpg')
84
83
  zero_image = File::join(@working_path, 'zero_byte_image.jpg')
@@ -87,6 +86,26 @@ class MojoMagickTest < Test::Unit::TestCase
87
86
  assert_raise(MojoMagick::MojoFailed) {MojoMagick::get_image_size('/file_does_not_exist_here_ok.jpg')}
88
87
  end
89
88
 
89
+ def test_resize_with_fill
90
+ reset_images
91
+ test_image = File::join(@working_path, '5742.jpg')
92
+ orig_image_size = File::size(test_image)
93
+ MojoMagick::resize(test_image, test_image, {:fill => true, :width => 100, :height => 100})
94
+ dim = MojoMagick::get_image_size(test_image)
95
+ assert_equal 100, dim[:width]
96
+ assert_equal 150, dim[:height]
97
+ end
98
+
99
+ def test_resize_with_fill_and_crop
100
+ reset_images
101
+ test_image = File::join(@working_path, '5742.jpg')
102
+ orig_image_size = File::size(test_image)
103
+ MojoMagick::resize(test_image, test_image, {:fill => true, :crop => true, :width => 150, :height => 120})
104
+ dim = MojoMagick::get_image_size(test_image)
105
+ assert_equal 150, dim[:width]
106
+ assert_equal 120, dim[:height]
107
+ end
108
+
90
109
  def test_resource_limits
91
110
  orig_limits = MojoMagick::get_default_limits
92
111
  assert_equal 7, orig_limits.size
@@ -202,6 +221,7 @@ class MojoMagickTest < Test::Unit::TestCase
202
221
  File.open(filename, 'rb') do |f|
203
222
  assert_equal f.read, 'binary data'
204
223
  end
224
+
205
225
  end
206
226
 
207
227
  def test_command_helpers
@@ -244,5 +264,27 @@ class MojoMagickTest < Test::Unit::TestCase
244
264
  retval = MojoMagick::get_image_size(out_image)
245
265
  assert_equal 50, retval[:width]
246
266
  assert_equal 50, retval[:height]
267
+
268
+ # RGB8 test
269
+ bdata = 'aaaaaabbbbbbccc'
270
+ out = 'out.png'
271
+ MojoMagick::convert do |c|
272
+ c.blob bdata, :format => :rgb, :depth => 8, :size => '5x1'
273
+ c.file out
274
+ end
275
+ r = MojoMagick::get_image_size(out)
276
+ assert r[:height] == 1
277
+ assert r[:width] == 5
278
+
279
+ bdata = '1111222233334444'
280
+ out = 'out.png'
281
+ MojoMagick::convert do |c|
282
+ c.blob bdata, :format => :rgba, :depth => 8, :size => '4x1'
283
+ c.file out
284
+ end
285
+ r = MojoMagick::get_image_size(out)
286
+ assert r[:height] == 1
287
+ assert r[:width] == 4
288
+
247
289
  end
248
290
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mojo_magick
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.0
5
+ version: 0.4.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Steve Midgley
@@ -12,9 +12,19 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2011-08-28 00:00:00 Z
16
- dependencies: []
17
-
15
+ date: 2012-03-18 00:00:00 Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: rake
19
+ prerelease: false
20
+ requirement: &id001 !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: "0"
26
+ type: :development
27
+ version_requirements: *id001
18
28
  description: Simple Ruby stateless module interface to imagemagick.
19
29
  email:
20
30
  - public@misuse.org
@@ -26,15 +36,15 @@ extensions: []
26
36
  extra_rdoc_files: []
27
37
 
28
38
  files:
29
- - README
39
+ - .gitignore
40
+ - README.md
30
41
  - init.rb
31
42
  - lib/image_resources.rb
32
43
  - lib/mojo_magick.rb
44
+ - lib/mojo_magick/version.rb
45
+ - mojo_magick.gemspec
33
46
  - test/fixtures/5742.jpg
34
47
  - test/fixtures/not_an_image.jpg
35
- - test/fixtures/tmp/5742.jpg
36
- - test/fixtures/tmp/not_an_image.jpg
37
- - test/fixtures/tmp/zero_byte_image.jpg
38
48
  - test/fixtures/zero_byte_image.jpg
39
49
  - test/mojo_magick_test.rb
40
50
  homepage: http://github.com/bunnymatic/mojo_magick
@@ -60,15 +70,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
70
  requirements: []
61
71
 
62
72
  rubyforge_project: mojo_magick
63
- rubygems_version: 1.8.5
73
+ rubygems_version: 1.8.15
64
74
  signing_key:
65
75
  specification_version: 3
66
- summary: mojo_magick-0.3.0
76
+ summary: mojo_magick-0.4.0
67
77
  test_files:
68
78
  - test/fixtures/5742.jpg
69
79
  - test/fixtures/not_an_image.jpg
70
- - test/fixtures/tmp/5742.jpg
71
- - test/fixtures/tmp/not_an_image.jpg
72
- - test/fixtures/tmp/zero_byte_image.jpg
73
80
  - test/fixtures/zero_byte_image.jpg
74
81
  - test/mojo_magick_test.rb
data/README DELETED
@@ -1,112 +0,0 @@
1
- MojoMagick
2
- ==========
3
-
4
- MojoMagick is a "dog simple, do very little" image library. It is basically a couple of stateless
5
- module methods that make it somewhat more convenient than calling ImageScience by hand.
6
-
7
- The main reason to use MojoMagick is that you should consolidate your ImageScience calls into
8
- one place, so why not do it here? If you improve on this tool, send me the patch.
9
-
10
- This tool came about because I wanted a fast, simple, lightweight, nothing-goes-wrong-with-it-
11
- because-it's-too-simple-to-break image tool.
12
-
13
- Examples
14
- ========
15
-
16
- # Obtain the size of an image (assuming image is "120wx222h")
17
- dimensions = MojoMagick::get_image_size(test_image)
18
- # ==> dimensions now holds a hash: {:height => 120, :width => 222}
19
-
20
- # Resize an image so that it fits within a 100w x 200h bounding box
21
- # (Note: this will scale an image either up or down to fit these dimensions
22
- # which may not be what you want.)
23
- # In this example, we overwrite our image, but if you pass in a different file for the
24
- # second file name, a new file will be created with the resized dimensions
25
- MojoMagick::resize('/img/test.jpg', '/img/test.jpg', {:width=>100, :height=>200})
26
-
27
- ## Code sample of how to shrink all jpg's in a folder
28
- {{{
29
- require 'mojo_magick'
30
-
31
- image_folder = '/tmp/img'
32
- Dir::glob(File::join(image_folder, '*.jpg')).each do |image|
33
- begin
34
- # shrink all the images *in place* to no bigger than 60pix x 60pix
35
- MojoMagick::shrink(image, image, {:width => 60, :height => 60})
36
- puts "Shrunk: #{image}"
37
- rescue MojoMagick::MojoFailed => e
38
- STDERR.puts "Unable to shrink image '#{image}' - probably an invalid image\n#{e.message}"
39
- rescue MojoMagick::MojoMagickException => e
40
- STDERR.puts "Unknown exception on image '#{image}'\n#{e.message}"
41
- end
42
- end
43
- }}}
44
-
45
- ## More Code on setting memory limits for imagemagick
46
- # (Be sure you�re upgraded to the current release of ImageMagick.)
47
-
48
- # set limits on disk, area, map and ram usage
49
- # obtain/print a hash of default limits:
50
- puts MojoMagick::get_default_limits.inspect
51
- # current_limits shows same values:
52
- puts MojoMagick::get_current_limits.inspect
53
-
54
- MojoMagick::set_limits(:area => '32mb', :disk => '0', :memory => '64mb', :map => '32mb')
55
- puts MojoMagick::get_current_limits.inspect
56
-
57
- ### More sample code (thanks to Elliot Nelson for adding this code to the system)
58
- # Two command-line builders, #convert and #mogrify, have been added to simplify
59
- # complex commands. Examples included below.
60
-
61
- # Example #convert usage:
62
-
63
- MojoMagick::convert('source.jpg', 'dest.jpg') do |c|
64
- c.crop '250x250+0+0'
65
- c.repage!
66
- c.strip
67
- c.set 'comment', 'my favorite file'
68
- end
69
-
70
- # Equivalent to:
71
-
72
- MojoMagick::raw_command('convert', 'source.jpg -crop 250x250+0+0 +repage -strip -set comment "my favorite file" dest.jpg')
73
-
74
- # Example #mogrify usage:
75
-
76
- MojoMagick::mogrify('image.jpg') {|i| i.shave '10x10'}
77
-
78
- # Equivalent to:
79
-
80
- MojoMagick::raw_command('mogrify', '-shave 10x10 image.jpg')
81
-
82
- # Example showing some additional options:
83
-
84
- MojoMagick::convert do |c|
85
- c.file 'source.jpg'
86
- c.blob my_binary_data
87
- c.append
88
- c.crop '256x256+0+0'
89
- c.repage!
90
- c.file 'output.jpg'
91
- end
92
-
93
- # Use .file to specify file names, .blob to create and include a tempfile. The
94
- # bang (!) can be appended to command names to use the '+' versions
95
- # instead of '-' versions.
96
-
97
- Availablility
98
- =============
99
- SVN Repo access from here: http://trac.misuse.org/science/wiki/MojoMagick
100
- Contact author or discuss MojoMagick here: http://www.misuse.org/science/2008/01/30/mojomagick-ruby-image-library-for-imagemagick/
101
-
102
-
103
- Updates by Jon Rogers (jon@2rye.com)
104
- ------------------------------------
105
- # added gemspec for building gem with bundler
106
- # updated tests for ImageMagick 6.6
107
- # added ability to do fill + crop resizing
108
- # bumped version to 0.3.0
109
-
110
-
111
- Copyright (c) 2008 Steve Midgley, released under the MIT license
112
- Credit to Elliot Nelson for significant code contributions. Thanks Elliot!
Binary file
Binary file
File without changes