refile-mini_magick 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10851b9908e5028f56547ef5f88631c42c8833d6
4
- data.tar.gz: 5675f42a9d038b1129c8f7c606ee464a7ee217b8
3
+ metadata.gz: 280ae54ec408e3287c8eadc03c3365fb6b7438a3
4
+ data.tar.gz: 673c76451b95246a20e57edca25c927bc54f62c3
5
5
  SHA512:
6
- metadata.gz: 9eb1c20278a5f1772aaf60e6d39f98e2940f65fb79f9d31f134fc0f45cd896f6063254b5cd21f247a863c801b6960ae0b645ec4076914eca878e4eb2f83fdeb4
7
- data.tar.gz: e926a3da9fdfa501630297b702815c560eb776c3075d37a911038786bc6522f086214865580edaccdb961d8defc40bdb0f2f89f04e0ad2ea93e069a5f4946b85
6
+ metadata.gz: 2235368076577c96821e38582c97e8c3efdcc011bf591ee2323ef977a6cd6dff6251a29f46ab01907d27e82e6b055d6ebda34a89d378f0fad8bf976ceb6dd427
7
+ data.tar.gz: 0bc6f650318f17fc6bc7e4bc6b3b1d9af552ab58bd2449e5aacebf44aebc46e70d3338b3afab354b4f6f6a36fcbdf49407196e70bba9537af40905aaa6bd3cb9
@@ -15,9 +15,10 @@ module Refile
15
15
  # @see http://www.imagemagick.org/script/command-line-options.php#format
16
16
  # @param [MiniMagick::Image] img the image to convert
17
17
  # @param [String] format the format to convert to
18
+ # @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
18
19
  # @return [void]
19
- def convert(img, format)
20
- img.format(format.to_s.downcase, nil)
20
+ def convert(img, format, &block)
21
+ img.format(format.to_s.downcase, nil, &block)
21
22
  end
22
23
 
23
24
  # Resize the image to fit within the specified dimensions while retaining
@@ -29,9 +30,13 @@ module Refile
29
30
  # @param [MiniMagick::Image] img the image to convert
30
31
  # @param [#to_s] width the maximum width
31
32
  # @param [#to_s] height the maximum height
33
+ # @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
32
34
  # @return [void]
33
35
  def limit(img, width, height)
34
- img.resize "#{width}x#{height}>"
36
+ img.combine_options do |cmd|
37
+ yield cmd if block_given?
38
+ cmd.resize "#{width}x#{height}>"
39
+ end
35
40
  end
36
41
 
37
42
  # Resize the image to fit within the specified dimensions while retaining
@@ -42,9 +47,13 @@ module Refile
42
47
  # @param [MiniMagick::Image] img the image to convert
43
48
  # @param [#to_s] width the width to fit into
44
49
  # @param [#to_s] height the height to fit into
50
+ # @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
45
51
  # @return [void]
46
52
  def fit(img, width, height)
47
- img.resize "#{width}x#{height}"
53
+ img.combine_options do |cmd|
54
+ yield cmd if block_given?
55
+ cmd.resize "#{width}x#{height}"
56
+ end
48
57
  end
49
58
 
50
59
  # Resize the image so that it is at least as large in both dimensions as
@@ -60,13 +69,17 @@ module Refile
60
69
  # @param [#to_s] width the width to fill out
61
70
  # @param [#to_s] height the height to fill out
62
71
  # @param [String] gravity which part of the image to focus on
72
+ # @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
63
73
  # @return [void]
64
74
  # @see http://www.imagemagick.org/script/command-line-options.php#gravity
65
75
  def fill(img, width, height, gravity = "Center")
66
- img.combine_options do |cmd|
76
+ # We use `convert` to work around GraphicsMagick's absence of "gravity"
77
+ ::MiniMagick::Tool::Convert.new do |cmd|
78
+ yield cmd if block_given?
67
79
  cmd.resize "#{width}x#{height}^"
68
80
  cmd.gravity gravity
69
81
  cmd.extent "#{width}x#{height}"
82
+ cmd.merge! [img.path, img.path]
70
83
  end
71
84
  end
72
85
 
@@ -87,11 +100,14 @@ module Refile
87
100
  # @param [#to_s] height the height to fill out
88
101
  # @param [string] background the color to use as a background
89
102
  # @param [string] gravity which part of the image to focus on
103
+ # @yield [MiniMagick::Tool::Mogrify, MiniMagick::Tool::Convert]
90
104
  # @return [void]
91
105
  # @see http://www.imagemagick.org/script/color.php
92
106
  # @see http://www.imagemagick.org/script/command-line-options.php#gravity
93
107
  def pad(img, width, height, background = "transparent", gravity = "Center")
94
- img.combine_options do |cmd|
108
+ # We use `convert` to work around GraphicsMagick's absence of "gravity"
109
+ ::MiniMagick::Tool::Convert.new do |cmd|
110
+ yield cmd if block_given?
95
111
  cmd.thumbnail "#{width}x#{height}>"
96
112
  if background == "transparent"
97
113
  cmd.background "rgba(255, 255, 255, 0.0)"
@@ -100,6 +116,7 @@ module Refile
100
116
  end
101
117
  cmd.gravity gravity
102
118
  cmd.extent "#{width}x#{height}"
119
+ cmd.merge! [img.path, img.path]
103
120
  end
104
121
  end
105
122
 
@@ -112,10 +129,10 @@ module Refile
112
129
  # @param [Tempfile] file the file to manipulate
113
130
  # @param [String] format the file format to convert to
114
131
  # @return [File] the processed file
115
- def call(file, *args, format: nil)
132
+ def call(file, *args, format: nil, &block)
116
133
  img = ::MiniMagick::Image.new(file.path)
117
134
  img.format(format.to_s.downcase, nil) if format
118
- send(@method, img, *args)
135
+ send(@method, img, *args, &block)
119
136
 
120
137
  ::File.open(img.path, "rb")
121
138
  end
@@ -1,5 +1,5 @@
1
1
  module Refile
2
2
  class MiniMagick
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -2,124 +2,157 @@ require "pry"
2
2
  require "refile/mini_magick"
3
3
  require "phashion"
4
4
 
5
- describe Refile::MiniMagick do
6
- let(:portrait) { Tempfile.new(["portrait", ".jpg"]) }
7
- let(:landscape) { Tempfile.new(["landscape", ".jpg"]) }
8
-
9
- matcher :be_similar_to do |expected|
10
- match do |actual|
11
- a = Phashion::Image.new(expected)
12
- b = Phashion::Image.new(actual)
13
- @distance = a.distance_from(b).abs
14
- @distance < allowed_distance
15
- end
16
-
17
- failure_message do
18
- "perceptual hash distance between images should be < #{allowed_distance} but was #{@distance}"
19
- end
20
-
21
- def allowed_distance
22
- 2
23
- end
24
- end
25
-
26
- def fixture_path(name)
27
- File.expand_path("./fixtures/#{name}", File.dirname(__FILE__))
28
- end
29
-
30
- before do
31
- FileUtils.cp(fixture_path("portrait.jpg"), portrait.path)
32
- FileUtils.cp(fixture_path("landscape.jpg"), landscape.path)
33
- end
34
-
35
- describe "#convert" do
36
- it "changes the image format" do
37
- file = Refile::MiniMagick.new(:convert).call(portrait, "png")
38
- expect(::MiniMagick::Image.new(file.path).identify).to match(/PNG/)
39
- end
40
- end
41
-
42
- describe "#limit" do
43
- it "resizes the image up to a given limit" do
44
- file = Refile::MiniMagick.new(:limit).call(portrait, "400", "400")
45
- result = ::MiniMagick::Image.new(file.path)
46
- expect(result.width).to eq(300)
47
- expect(result.height).to eq(400)
48
- end
49
-
50
- it "does not resize the image if it is smaller than the limit" do
51
- file = Refile::MiniMagick.new(:limit).call(portrait, "1000", "1000")
52
- result = ::MiniMagick::Image.new(file.path)
53
- expect(result.width).to eq(600)
54
- expect(result.height).to eq(800)
55
- end
56
-
57
- it "produces correct image" do
58
- file = Refile::MiniMagick.new(:limit).call(portrait, "400", "400")
59
- expect(file.path).to be_similar_to(fixture_path("limit.jpg"))
60
- end
61
- end
62
-
63
- describe "#fit" do
64
- it "resizes the image to fit given dimensions" do
65
- file = Refile::MiniMagick.new(:fit).call(portrait, "400", "400")
66
- result = ::MiniMagick::Image.new(file.path)
67
- expect(result.width).to eq(300)
68
- expect(result.height).to eq(400)
69
- end
70
-
71
- it "enlarges image if it is smaller than given dimensions" do
72
- file = Refile::MiniMagick.new(:fit).call(portrait, "1000", "1000")
73
- result = ::MiniMagick::Image.new(file.path)
74
- expect(result.width).to eq(750)
75
- expect(result.height).to eq(1000)
76
- end
77
-
78
- it "produces correct image" do
79
- file = Refile::MiniMagick.new(:fit).call(portrait, "400", "400")
80
- expect(file.path).to be_similar_to(fixture_path("fit.jpg"))
81
- end
82
- end
83
-
84
- describe "#fill" do
85
- it "resizes and crops the image to fill out the given dimensions" do
86
- file = Refile::MiniMagick.new(:fill).call(portrait, "400", "400")
87
- result = ::MiniMagick::Image.new(file.path)
88
- expect(result.width).to eq(400)
89
- expect(result.height).to eq(400)
90
- end
91
-
92
- it "enlarges image and crops it if it is smaller than given dimensions" do
93
- file = Refile::MiniMagick.new(:fill).call(portrait, "1000", "1000")
94
- result = ::MiniMagick::Image.new(file.path)
95
- expect(result.width).to eq(1000)
96
- expect(result.height).to eq(1000)
97
- end
98
-
99
- it "produces correct image" do
100
- file = Refile::MiniMagick.new(:fill).call(portrait, "400", "400")
101
- expect(file.path).to be_similar_to(fixture_path("fill.jpg"))
102
- end
103
- end
104
-
105
- describe "#fill" do
106
- it "resizes and fills out the remaining space to fill out the given dimensions" do
107
- file = Refile::MiniMagick.new(:pad).call(portrait, "400", "400", "red")
108
- result = ::MiniMagick::Image.new(file.path)
109
- expect(result.width).to eq(400)
110
- expect(result.height).to eq(400)
111
- end
112
-
113
- it "enlarges image and fills out the remaining space to fill out the given dimensions" do
114
- file = Refile::MiniMagick.new(:pad).call(portrait, "1000", "1000", "red")
115
- result = ::MiniMagick::Image.new(file.path)
116
- expect(result.width).to eq(1000)
117
- expect(result.height).to eq(1000)
118
- end
119
-
120
- it "produces correct image" do
121
- file = Refile::MiniMagick.new(:pad).call(portrait, "400", "400")
122
- expect(file.path).to be_similar_to(fixture_path("pad.jpg"))
5
+ ["ImageMagick", "GraphicsMagick"].each do |cli|
6
+ RSpec.context "With #{cli}", cli: cli.downcase.to_sym do
7
+ describe Refile::MiniMagick do
8
+ let(:portrait) { Tempfile.new(["portrait", ".jpg"]) }
9
+ let(:landscape) { Tempfile.new(["landscape", ".jpg"]) }
10
+
11
+ matcher :be_similar_to do |expected|
12
+ match do |actual|
13
+ a = Phashion::Image.new(expected)
14
+ b = Phashion::Image.new(actual)
15
+ @distance = a.distance_from(b).abs
16
+ @distance < allowed_distance
17
+ end
18
+
19
+ failure_message do
20
+ "perceptual hash distance between images should be < #{allowed_distance} but was #{@distance}"
21
+ end
22
+
23
+ def allowed_distance
24
+ 2
25
+ end
26
+ end
27
+
28
+ def fixture_path(name)
29
+ File.expand_path("./fixtures/#{name}", File.dirname(__FILE__))
30
+ end
31
+
32
+ around do |example|
33
+ MiniMagick.with_cli(example.metadata[:cli]) { example.run }
34
+ end
35
+
36
+ before do
37
+ FileUtils.cp(fixture_path("portrait.jpg"), portrait.path)
38
+ FileUtils.cp(fixture_path("landscape.jpg"), landscape.path)
39
+ end
40
+
41
+ describe "#convert" do
42
+ it "changes the image format" do
43
+ file = Refile::MiniMagick.new(:convert).call(portrait, "png")
44
+ expect(::MiniMagick::Image.new(file.path).identify).to match(/PNG/)
45
+ end
46
+
47
+ it "yields the command object" do
48
+ expect { |b| Refile::MiniMagick.new(:convert).call(portrait, "png", &b) }
49
+ .to yield_with_args(MiniMagick::Tool)
50
+ end
51
+ end
52
+
53
+ describe "#limit" do
54
+ it "resizes the image up to a given limit" do
55
+ file = Refile::MiniMagick.new(:limit).call(portrait, "400", "400")
56
+ result = ::MiniMagick::Image.new(file.path)
57
+ expect(result.width).to eq(300)
58
+ expect(result.height).to eq(400)
59
+ end
60
+
61
+ it "does not resize the image if it is smaller than the limit" do
62
+ file = Refile::MiniMagick.new(:limit).call(portrait, "1000", "1000")
63
+ result = ::MiniMagick::Image.new(file.path)
64
+ expect(result.width).to eq(600)
65
+ expect(result.height).to eq(800)
66
+ end
67
+
68
+ it "produces correct image" do
69
+ file = Refile::MiniMagick.new(:limit).call(portrait, "400", "400")
70
+ expect(file.path).to be_similar_to(fixture_path("limit.jpg"))
71
+ end
72
+
73
+ it "yields the command object" do
74
+ expect { |b| Refile::MiniMagick.new(:limit).call(portrait, "400", "400", &b) }
75
+ .to yield_with_args(MiniMagick::Tool)
76
+ end
77
+ end
78
+
79
+ describe "#fit" do
80
+ it "resizes the image to fit given dimensions" do
81
+ file = Refile::MiniMagick.new(:fit).call(portrait, "400", "400")
82
+ result = ::MiniMagick::Image.new(file.path)
83
+ expect(result.width).to eq(300)
84
+ expect(result.height).to eq(400)
85
+ end
86
+
87
+ it "enlarges image if it is smaller than given dimensions" do
88
+ file = Refile::MiniMagick.new(:fit).call(portrait, "1000", "1000")
89
+ result = ::MiniMagick::Image.new(file.path)
90
+ expect(result.width).to eq(750)
91
+ expect(result.height).to eq(1000)
92
+ end
93
+
94
+ it "produces correct image" do
95
+ file = Refile::MiniMagick.new(:fit).call(portrait, "400", "400")
96
+ expect(file.path).to be_similar_to(fixture_path("fit.jpg"))
97
+ end
98
+
99
+ it "yields the command object" do
100
+ expect { |b| Refile::MiniMagick.new(:fit).call(portrait, "400", "400", &b) }
101
+ .to yield_with_args(MiniMagick::Tool)
102
+ end
103
+ end
104
+
105
+ describe "#fill" do
106
+ it "resizes and crops the image to fill out the given dimensions" do
107
+ file = Refile::MiniMagick.new(:fill).call(portrait, "400", "400")
108
+ result = ::MiniMagick::Image.new(file.path)
109
+ expect(result.width).to eq(400)
110
+ expect(result.height).to eq(400)
111
+ end
112
+
113
+ it "enlarges image and crops it if it is smaller than given dimensions" do
114
+ file = Refile::MiniMagick.new(:fill).call(portrait, "1000", "1000")
115
+ result = ::MiniMagick::Image.new(file.path)
116
+ expect(result.width).to eq(1000)
117
+ expect(result.height).to eq(1000)
118
+ end
119
+
120
+ it "produces correct image" do
121
+ file = Refile::MiniMagick.new(:fill).call(portrait, "400", "400")
122
+ expect(file.path).to be_similar_to(fixture_path("fill.jpg"))
123
+ end
124
+
125
+ it "yields the command object" do
126
+ expect { |b| Refile::MiniMagick.new(:fill).call(portrait, "400", "400", &b) }
127
+ .to yield_with_args(MiniMagick::Tool)
128
+ end
129
+ end
130
+
131
+ describe "#pad" do
132
+ it "resizes and fills out the remaining space to fill out the given dimensions" do
133
+ file = Refile::MiniMagick.new(:pad).call(portrait, "400", "400", "red")
134
+ result = ::MiniMagick::Image.new(file.path)
135
+ expect(result.width).to eq(400)
136
+ expect(result.height).to eq(400)
137
+ end
138
+
139
+ it "enlarges image and fills out the remaining space to fill out the given dimensions" do
140
+ file = Refile::MiniMagick.new(:pad).call(portrait, "1000", "1000", "red")
141
+ result = ::MiniMagick::Image.new(file.path)
142
+ expect(result.width).to eq(1000)
143
+ expect(result.height).to eq(1000)
144
+ end
145
+
146
+ it "produces correct image" do
147
+ file = Refile::MiniMagick.new(:pad).call(portrait, "400", "400")
148
+ expect(file.path).to be_similar_to(fixture_path("pad.jpg"))
149
+ end
150
+
151
+ it "yields the command object" do
152
+ expect { |b| Refile::MiniMagick.new(:pad).call(portrait, "400", "400", &b) }
153
+ .to yield_with_args(MiniMagick::Tool)
154
+ end
155
+ end
123
156
  end
124
157
  end
125
158
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: refile-mini_magick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Nicklas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-20 00:00:00.000000000 Z
11
+ date: 2015-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: refile
@@ -80,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
80
  version: '0'
81
81
  requirements: []
82
82
  rubyforge_project:
83
- rubygems_version: 2.4.5
83
+ rubygems_version: 2.4.5.1
84
84
  signing_key:
85
85
  specification_version: 4
86
86
  summary: Image processing via MiniMagick for Refile
@@ -92,3 +92,4 @@ test_files:
92
92
  - spec/refile/fixtures/pad.jpg
93
93
  - spec/refile/fixtures/portrait.jpg
94
94
  - spec/refile/mini_magick_spec.rb
95
+ has_rdoc: