refile-mini_magick 0.1.0 → 0.2.0
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.
- checksums.yaml +4 -4
- data/lib/refile/mini_magick.rb +25 -8
- data/lib/refile/mini_magick/version.rb +1 -1
- data/spec/refile/mini_magick_spec.rb +151 -118
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 280ae54ec408e3287c8eadc03c3365fb6b7438a3
|
4
|
+
data.tar.gz: 673c76451b95246a20e57edca25c927bc54f62c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2235368076577c96821e38582c97e8c3efdcc011bf591ee2323ef977a6cd6dff6251a29f46ab01907d27e82e6b055d6ebda34a89d378f0fad8bf976ceb6dd427
|
7
|
+
data.tar.gz: 0bc6f650318f17fc6bc7e4bc6b3b1d9af552ab58bd2449e5aacebf44aebc46e70d3338b3afab354b4f6f6a36fcbdf49407196e70bba9537af40905aaa6bd3cb9
|
data/lib/refile/mini_magick.rb
CHANGED
@@ -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.
|
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.
|
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
|
-
|
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
|
-
|
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
|
@@ -2,124 +2,157 @@ require "pry"
|
|
2
2
|
require "refile/mini_magick"
|
3
3
|
require "phashion"
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
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.
|
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-
|
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:
|