micro_magick 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 +23 -2
- data/Rakefile +3 -1
- data/lib/micro_magick.rb +1 -0
- data/lib/micro_magick/convert.rb +62 -23
- data/lib/micro_magick/geometry.rb +22 -0
- data/lib/micro_magick/version.rb +1 -1
- data/test/{640.jpg → 480x270.jpg} +0 -0
- data/test/geometry_test.rb +9 -0
- data/test/micro_gmagick_test.rb +11 -0
- data/test/micro_imagick_test.rb +11 -0
- data/test/micro_magick_test.rb +33 -0
- data/test/micro_magick_test_base.rb +66 -0
- metadata +17 -8
- data/test/test_micro_magick.rb +0 -65
data/README.md
CHANGED
@@ -24,15 +24,32 @@ source, performs operations on it, and saves the result to a different file.
|
|
24
24
|
|
25
25
|
```ruby
|
26
26
|
img = MicroMagick::Convert.new("/path/to/image.jpg")
|
27
|
+
img.width
|
28
|
+
# => 3264
|
29
|
+
img.height
|
30
|
+
# => 2448
|
27
31
|
img.strip
|
28
32
|
img.quality(85)
|
29
33
|
img.resize("640x480>")
|
30
|
-
img.write("/
|
34
|
+
img.write("/new/path/image-640x480.jpg")
|
31
35
|
```
|
32
36
|
|
33
37
|
This results in the following system call:
|
34
38
|
|
35
|
-
```gm convert -size 640x480 /path/to/image.jpg
|
39
|
+
```gm convert -size 640x480 /path/to/image.jpg +profile \* -quality 85 -resize "640x480>" /new/path/image-640x480.jpg```
|
40
|
+
|
41
|
+
### "Plus" options
|
42
|
+
|
43
|
+
To add an output option that has a "plus" prefix, like, ```+matte```, use ```.add_output_option("+matte")```.
|
44
|
+
|
45
|
+
### Goodies
|
46
|
+
|
47
|
+
There are a couple additional methods that have been added to address common image tasks:
|
48
|
+
|
49
|
+
* ```img.strip``` removes all comments and EXIF headers
|
50
|
+
* ```img.square_crop``` crops the image to a square (so a 640x480 image would crop down to a 480x480 image, cropped in the middle).
|
51
|
+
|
52
|
+
|
36
53
|
|
37
54
|
Note that all of ```convert```'s options are supported, but ```micro_magick``` does no validations.
|
38
55
|
A ```MicroMagick::ArgumentError``` will be raised on ```.write``` if
|
@@ -75,3 +92,7 @@ and run ```bundle```.
|
|
75
92
|
### 0.0.1
|
76
93
|
|
77
94
|
Let's get this party started.
|
95
|
+
|
96
|
+
### 0.0.3
|
97
|
+
|
98
|
+
Added square_crop, image dimensions, and support for ```+option```s.
|
data/Rakefile
CHANGED
data/lib/micro_magick.rb
CHANGED
data/lib/micro_magick/convert.rb
CHANGED
@@ -3,41 +3,80 @@ require 'shellwords'
|
|
3
3
|
module MicroMagick
|
4
4
|
class Convert
|
5
5
|
def initialize(input_file)
|
6
|
-
@
|
6
|
+
@input_file = input_file
|
7
|
+
@input_options = []
|
8
|
+
@output_options = []
|
7
9
|
end
|
8
10
|
|
11
|
+
def width
|
12
|
+
geometry.width
|
13
|
+
end
|
14
|
+
|
15
|
+
def height
|
16
|
+
geometry.height
|
17
|
+
end
|
18
|
+
|
19
|
+
def geometry
|
20
|
+
@geometry ||= MicroMagick::Geometry.new(@input_file)
|
21
|
+
end
|
22
|
+
|
23
|
+
# strip the image of any profiles or comments
|
24
|
+
# (ImageMagick has the -strip command, but GraphicsMagick doesn't.
|
25
|
+
# It turns out that ```+profile *``` does the same thing.)
|
26
|
+
def strip
|
27
|
+
add_output_option("+profile", "*")
|
28
|
+
end
|
29
|
+
|
30
|
+
# Crop to a square, using the specified gravity.
|
31
|
+
def square_crop(gravity = "Center")
|
32
|
+
gravity(gravity) unless gravity.nil?
|
33
|
+
d = [width, height].min
|
34
|
+
crop("#{d}x#{d}+0+0!")
|
35
|
+
end
|
36
|
+
|
37
|
+
# For normal options, like -resize or -flip, you can call .resize("32x32") or .flip().
|
38
|
+
# If you need to add an output option that starts with a '+', you can use this method.
|
39
|
+
def add_output_option(option_name, *args)
|
40
|
+
(@output_options ||= []) << option_name
|
41
|
+
@output_options += args.collect { |ea| Shellwords.escape(ea.to_s) }
|
42
|
+
|
43
|
+
# if we're a resize call, let's give the -size render hint to gm, but only when it's safe:
|
44
|
+
# * we don't have input options yet,
|
45
|
+
# * we're not cropping (because the -size will prevent the crop from working),
|
46
|
+
# * and we have dimensions in the form of NNNxNNN
|
47
|
+
if %w{-geometry -resize -sample -scale}.include?(option_name) &&
|
48
|
+
@input_options.empty? &&
|
49
|
+
!@output_options.include?("-crop") &&
|
50
|
+
(dimensions = args.first) &&
|
51
|
+
(simple_dim = dimensions.scan(/(\d+x\d+)/).first)
|
52
|
+
@input_options << "-size #{simple_dim}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
# Executes `convert`, writing to output_file, and clearing all previously set input and output options.
|
58
|
+
# @return the command given to system()
|
9
59
|
def write(output_file)
|
10
|
-
@
|
11
|
-
cmd =
|
60
|
+
@output_file = output_file
|
61
|
+
cmd = command()
|
12
62
|
MicroMagick.exec(cmd)
|
13
|
-
@
|
14
|
-
@
|
63
|
+
@input_options.clear
|
64
|
+
@output_options.clear
|
15
65
|
cmd
|
16
66
|
end
|
17
67
|
|
18
68
|
protected
|
19
69
|
|
20
70
|
def method_missing(method, *args, &block)
|
21
|
-
|
22
|
-
dimensions = args.first
|
23
|
-
# let's give the -size render hint to gm, but only if we're a resize call, and we have simple dimensions:
|
24
|
-
if dimensions && (simple_dim = dimensions.scan(/(\d+x\d+)/).first)
|
25
|
-
@pre_input = "-size #{simple_dim}"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
if method == :strip
|
30
|
-
# ImageMagick knows that ```-strip```, means ```+profile "*"```
|
31
|
-
@args << '+profile'
|
32
|
-
@args << Shellwords.escape('*')
|
33
|
-
else
|
34
|
-
@args << "-#{method.to_s}"
|
35
|
-
end
|
36
|
-
@args += args.compact.collect { |ea| Shellwords.escape(ea.to_s) }
|
71
|
+
add_output_option("-#{method.to_s}", *args)
|
37
72
|
end
|
38
73
|
|
39
|
-
def
|
40
|
-
([MicroMagick.cmd_prefix, "convert"
|
74
|
+
def command
|
75
|
+
([MicroMagick.cmd_prefix, "convert"] +
|
76
|
+
@input_options +
|
77
|
+
[Shellwords.escape(@input_file)] +
|
78
|
+
@output_options +
|
79
|
+
[Shellwords.escape(@output_file)]).compact.join(" ")
|
41
80
|
end
|
42
81
|
end
|
43
82
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'shellwords'
|
2
|
+
|
3
|
+
module MicroMagick
|
4
|
+
class Geometry
|
5
|
+
attr_reader :width, :height
|
6
|
+
|
7
|
+
def initialize(input_file)
|
8
|
+
cmd = [MicroMagick.cmd_prefix,
|
9
|
+
"identify",
|
10
|
+
"-format",
|
11
|
+
"%w:%h",
|
12
|
+
Shellwords.escape(input_file)
|
13
|
+
]
|
14
|
+
geometry = MicroMagick.exec(cmd.join(" "))
|
15
|
+
@width, @height = geometry.split(':').collect { |ea| ea.to_i }
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
"#{width} x #{height}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/micro_magick/version.rb
CHANGED
File without changes
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
|
3
|
+
class MicroMagickTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_use_nil
|
6
|
+
# This shouldn't throw:
|
7
|
+
MicroMagick.use(nil)
|
8
|
+
|
9
|
+
# eh, ok, now we should stub out system
|
10
|
+
# and verify that the next call to cmd_prefix
|
11
|
+
# calls `hash gm`?
|
12
|
+
|
13
|
+
# Meh, let's just assume that the test running machine has gm in their PATH.
|
14
|
+
assert_equal MicroMagick.cmd_prefix, "gm"
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_use_invalid
|
18
|
+
assert_raise MicroMagick::InvalidArgument do
|
19
|
+
MicroMagick.use(:boink)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_use_gm
|
24
|
+
MicroMagick.use(:graphicsmagick)
|
25
|
+
assert_equal "gm", MicroMagick.cmd_prefix
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_use_im
|
29
|
+
MicroMagick.use(:imagemagick)
|
30
|
+
assert_equal nil, MicroMagick.cmd_prefix
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "shellwords"
|
3
|
+
require "micro_magick"
|
4
|
+
|
5
|
+
class MicroMagickTestBase < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@img = MicroMagick::Convert.new("test/480x270.jpg")
|
9
|
+
tmp = Tempfile.new('out.jpg')
|
10
|
+
@outfile = tmp.path
|
11
|
+
tmp.close
|
12
|
+
tmp.delete
|
13
|
+
assert !File.exist?(@outfile)
|
14
|
+
end
|
15
|
+
|
16
|
+
def cmd_prefix
|
17
|
+
s = MicroMagick.cmd_prefix
|
18
|
+
s.nil? ? "" : s + " "
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_resize
|
22
|
+
@img.resize("64x64")
|
23
|
+
command = @img.write(@outfile)
|
24
|
+
assert_equal "#{cmd_prefix}convert -size 64x64 test/480x270.jpg -resize 64x64 " + Shellwords.escape(@outfile), command
|
25
|
+
assert File.exist?(@outfile)
|
26
|
+
g = MicroMagick::Geometry.new(@outfile)
|
27
|
+
assert_equal (64 * (270.0/480.0)).to_i, g.width
|
28
|
+
assert_equal 64, g.height
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_strip
|
32
|
+
@img.strip
|
33
|
+
command = @img.write(@outfile)
|
34
|
+
assert_equal "#{cmd_prefix}convert test/480x270.jpg +profile \\* " + Shellwords.escape(@outfile), command
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_convert_with_crop
|
38
|
+
@img.quality(85)
|
39
|
+
@img.square_crop("North")
|
40
|
+
@img.resize("128x128")
|
41
|
+
command = @img.write(@outfile)
|
42
|
+
assert_equal "#{cmd_prefix}convert test/480x270.jpg " +
|
43
|
+
"-quality 85 -gravity North -crop 270x270\\+0\\+0\\! -resize 128x128 " +
|
44
|
+
Shellwords.escape(@outfile),
|
45
|
+
command
|
46
|
+
assert File.exist?(@outfile)
|
47
|
+
g = MicroMagick::Geometry.new(@outfile)
|
48
|
+
assert_equal 128, g.width
|
49
|
+
assert_equal 128, g.height
|
50
|
+
|
51
|
+
# make sure calling previous arguments don't leak into new calls:
|
52
|
+
@img.resize("64x64")
|
53
|
+
command = @img.write(@outfile)
|
54
|
+
assert_equal "#{cmd_prefix}convert -size 64x64 test/480x270.jpg -resize 64x64 " +
|
55
|
+
Shellwords.escape(@outfile),
|
56
|
+
command
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_bad_args
|
60
|
+
@img.boing
|
61
|
+
assert_raise MicroMagick::InvalidArgument do
|
62
|
+
@img.write(@outfile)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: micro_magick
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Matthew McEachen
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-03-
|
18
|
+
date: 2012-03-04 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
prerelease: false
|
@@ -47,10 +47,15 @@ files:
|
|
47
47
|
- Rakefile
|
48
48
|
- lib/micro_magick.rb
|
49
49
|
- lib/micro_magick/convert.rb
|
50
|
+
- lib/micro_magick/geometry.rb
|
50
51
|
- lib/micro_magick/version.rb
|
51
52
|
- micro_magick.gemspec
|
52
|
-
- test/
|
53
|
-
- test/
|
53
|
+
- test/480x270.jpg
|
54
|
+
- test/geometry_test.rb
|
55
|
+
- test/micro_gmagick_test.rb
|
56
|
+
- test/micro_imagick_test.rb
|
57
|
+
- test/micro_magick_test.rb
|
58
|
+
- test/micro_magick_test_base.rb
|
54
59
|
homepage: ""
|
55
60
|
licenses: []
|
56
61
|
|
@@ -85,5 +90,9 @@ signing_key:
|
|
85
90
|
specification_version: 3
|
86
91
|
summary: Simplest ImageMagick/GraphicsMagick ruby wrapper EVAR
|
87
92
|
test_files:
|
88
|
-
- test/
|
89
|
-
- test/
|
93
|
+
- test/480x270.jpg
|
94
|
+
- test/geometry_test.rb
|
95
|
+
- test/micro_gmagick_test.rb
|
96
|
+
- test/micro_imagick_test.rb
|
97
|
+
- test/micro_magick_test.rb
|
98
|
+
- test/micro_magick_test_base.rb
|
data/test/test_micro_magick.rb
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
require "test/unit"
|
2
|
-
require "shellwords"
|
3
|
-
require "micro_magick"
|
4
|
-
|
5
|
-
class TestMicroMagick < Test::Unit::TestCase
|
6
|
-
|
7
|
-
def test_use
|
8
|
-
MicroMagick.use(:graphicsmagick)
|
9
|
-
assert_equal MicroMagick.cmd_prefix, "gm"
|
10
|
-
MicroMagick.use(:imagemagick)
|
11
|
-
assert_equal MicroMagick.cmd_prefix, nil
|
12
|
-
|
13
|
-
# This shouldn't throw:
|
14
|
-
MicroMagick.use(nil)
|
15
|
-
# eh, ok, now we should stub out system
|
16
|
-
# and verify that the next call to cmd_prefix
|
17
|
-
# calls `hash gm`?
|
18
|
-
|
19
|
-
# Meh, let's just assume that the test running machine has gm in their PATH.
|
20
|
-
assert_equal MicroMagick.cmd_prefix, "gm"
|
21
|
-
|
22
|
-
assert_raise MicroMagick::InvalidArgument do
|
23
|
-
MicroMagick.use(:boink)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_convert
|
28
|
-
i = MicroMagick::Convert.new("test/640.jpg")
|
29
|
-
i.strip
|
30
|
-
i.quality(85)
|
31
|
-
i.gravity("Center")
|
32
|
-
i.crop("250x250")
|
33
|
-
i.resize("128x128")
|
34
|
-
tmp = mktmpfile
|
35
|
-
assert !File.exist?(tmp)
|
36
|
-
command = i.write(tmp)
|
37
|
-
assert_equal command, "gm convert -size 128x128 test/640.jpg +profile \\* " +
|
38
|
-
"-quality 85 -gravity Center -crop 250x250 -resize 128x128 " +
|
39
|
-
Shellwords.escape(tmp)
|
40
|
-
assert File.exist?(tmp)
|
41
|
-
|
42
|
-
# make sure calling previous arguments don't leak into new calls:
|
43
|
-
i.resize("64x64")
|
44
|
-
command = i.write(tmp)
|
45
|
-
assert_equal command, "gm convert -size 64x64 test/640.jpg -resize 64x64 " +
|
46
|
-
Shellwords.escape(tmp)
|
47
|
-
end
|
48
|
-
|
49
|
-
def mktmpfile
|
50
|
-
tmp = Tempfile.new('out.jpg')
|
51
|
-
outfile = tmp.path
|
52
|
-
tmp.close
|
53
|
-
tmp.delete
|
54
|
-
outfile
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_bad_args
|
58
|
-
i = MicroMagick::Convert.new("test/640.jpg")
|
59
|
-
i.boing
|
60
|
-
assert_raise MicroMagick::InvalidArgument do
|
61
|
-
i.write(mktmpfile)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|