image_proc 1.0.1 → 1.0.2
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/History.txt +4 -0
- data/lib/image_proc.rb +14 -5
- data/test/test_image_proc.rb +15 -1
- metadata +1 -1
data/History.txt
CHANGED
data/lib/image_proc.rb
CHANGED
@@ -9,7 +9,7 @@ require 'open3'
|
|
9
9
|
# The whole idea is: a backend does not have to support cropping (we don't do it), it has only to be able to resize,
|
10
10
|
# and a backend should have 2 public methods. That's the game.
|
11
11
|
class ImageProc
|
12
|
-
VERSION = '1.0.
|
12
|
+
VERSION = '1.0.2'
|
13
13
|
|
14
14
|
class Error < RuntimeError; end
|
15
15
|
class MissingInput < Error; end
|
@@ -64,11 +64,15 @@ class ImageProc
|
|
64
64
|
# Resizes with specific options passed as a hash, and return the destination path to the resized image
|
65
65
|
# ImageProc.resize "/tmp/foo.jpg", "bla.jpg", :width => 120, :height => 30
|
66
66
|
def resize(from_path, to_path, opts = {})
|
67
|
-
|
68
|
-
|
69
|
-
|
67
|
+
if opts.is_a?(String)
|
68
|
+
STDERR.puts "String argument to resize() is really deprecated"
|
69
|
+
w, h = opts.scan(/^\d+x\d+$/).to_a.flatten
|
70
|
+
return resize(from_path, to_path, :width => w, :height => h)
|
71
|
+
end
|
72
|
+
|
73
|
+
remove_nil_values!(opts)
|
74
|
+
raise InvalidOptions, "The only allowed options are :width, :height and :fill" if (opts.keys - [:width, :height, :fill]).any?
|
70
75
|
raise InvalidOptions, "Pass width, height or both" unless (opts.keys & [:width, :height]).any?
|
71
|
-
opts.each_pair { |k,v| raise InvalidOptions, "#{k.inspect} cannot be set to nil" if v.nil? }
|
72
76
|
|
73
77
|
if opts[:width] && opts[:height] && opts[:fill]
|
74
78
|
resize_fit_fill(from_path, to_path, opts[:width], opts[:height])
|
@@ -187,6 +191,11 @@ class ImageProc
|
|
187
191
|
end
|
188
192
|
|
189
193
|
private
|
194
|
+
|
195
|
+
def remove_nil_values!(from_hash)
|
196
|
+
from_hash.keys.map{|k| from_hash.delete(k) if from_hash[k].nil? }
|
197
|
+
end
|
198
|
+
|
190
199
|
def force_keys!(in_hash, *keynames)
|
191
200
|
unless (in_hash.keys & keynames).length == keynames.length
|
192
201
|
raise Error, "This method requires #{keynames.join(', ')}"
|
data/test/test_image_proc.rb
CHANGED
@@ -13,6 +13,7 @@ require 'image_proc'
|
|
13
13
|
#end
|
14
14
|
|
15
15
|
class TestQuickProcessWithOptions < Test::Unit::TestCase
|
16
|
+
|
16
17
|
def test_resize_with_options
|
17
18
|
source = File.dirname(__FILE__) + '/input/horizontal.jpg'
|
18
19
|
dest = File.dirname(__FILE__) + '/output/resized.jpg'
|
@@ -33,7 +34,7 @@ class TestQuickProcessWithOptions < Test::Unit::TestCase
|
|
33
34
|
opts = {:too => 4, :doo => 10}
|
34
35
|
ImageProc.resize(source, dest, opts)
|
35
36
|
end
|
36
|
-
|
37
|
+
|
37
38
|
assert_raise(ImageProc::InvalidOptions) do
|
38
39
|
source = File.dirname(__FILE__) + '/input/horizontal.jpg'
|
39
40
|
dest = File.dirname(__FILE__) + '/output/resized.jpg'
|
@@ -41,6 +42,19 @@ class TestQuickProcessWithOptions < Test::Unit::TestCase
|
|
41
42
|
ImageProc.resize(source, dest, opts)
|
42
43
|
end
|
43
44
|
end
|
45
|
+
|
46
|
+
def test_resize_with_nil_options
|
47
|
+
source = File.dirname(__FILE__) + '/input/horizontal.jpg'
|
48
|
+
dest = File.dirname(__FILE__) + '/output/resized.jpg'
|
49
|
+
opts = {:height => 75, :fill => nil, :width => nil}
|
50
|
+
begin
|
51
|
+
assert_nothing_raised do
|
52
|
+
path = ImageProc.resize(source, dest, opts)
|
53
|
+
end
|
54
|
+
ensure
|
55
|
+
File.unlink(dest) rescue nil
|
56
|
+
end
|
57
|
+
end
|
44
58
|
end
|
45
59
|
|
46
60
|
class TestEngineAssignmentSticks < Test::Unit::TestCase
|