fastimage_resize 1.2.4 → 2.0.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.
data/README CHANGED
@@ -11,11 +11,11 @@ FastImage Resize is an extremely light solution for resizing images in ruby by u
11
11
 
12
12
  RubyInline
13
13
 
14
- sudo gem install RubyInline
14
+ gem install RubyInline
15
15
 
16
16
  FastImage
17
17
 
18
- sudo gem install sdsykes-fastimage -s http://gems.github.com
18
+ gem install fastimage
19
19
 
20
20
  Libgd
21
21
 
data/README.textile CHANGED
@@ -10,18 +10,34 @@ And it doesn't rely on installing external heavy libraries such as RMagick (whic
10
10
 
11
11
  FastImage Resize uses *Libgd*, which is commonly available on most unix platforms, including OSX. It is very likely that you already have this software installed on your server.
12
12
 
13
+ As its input, FastImage Resize can take a URI, a filename, or an IO object (or anything that responds to :read).
14
+ If you do not supply an output filename in the :outfile option, FastImage Resize will return you a Tempfile object. This will be unlinked when it is finalized during garbage collection.
15
+
16
+ FastImage Resize relies on "RubyInline":https://github.com/seattlerb/rubyinline for compiling and managing the C extension code.
17
+
18
+ h4. Incompatible API change Version 2.0.0 and above
19
+
20
+ Note that the parameters for version 2.0.0 have changed, the output filename is no longer the second parameter. See the examples.
21
+
13
22
  h2. Examples
14
23
 
15
24
  <pre>
16
25
  <code>
17
26
  require 'fastimage_resize'
18
27
 
19
- FastImage.resize("http://stephensykes.com/images/ss.com_x.gif", "my.gif", 100, 20)
20
- => 1
21
- </pre>
22
- </code>
28
+ FastImage.resize("http://stephensykes.com/images/ss.com_x.gif", 100, 20, :outfile=>"my.gif")
29
+ => nil
23
30
 
24
- The file my.gif is created locally, containing the resized image.
31
+ outfile = FastImage.resize("nonexistentfile.png", 50, 50)
32
+ =>FastImage::ImageFetchFailure: FastImage::ImageFetchFailure
33
+
34
+ outfile = FastImage.resize("afile.png", 50, 150)
35
+ => #<File:/var/folders/vm/vd65y2597xl_by6c73_m9j1h0000gn/T/FastImage20111003-7638-x3l8r7-0>
36
+
37
+ File.open("afile.png", "r") {|f| FastImage.resize(f, 100, 100)}
38
+ => #<File:/var/folders/vm/vd65y2597xl_by6c73_m9j1h0000gn/T/FastImage20111003-7638-y1ofh-0>
39
+ </code>
40
+ </pre>
25
41
 
26
42
  h2. Installation
27
43
 
@@ -31,7 +47,7 @@ h4. Gem
31
47
 
32
48
  <pre>
33
49
  <code>
34
- sudo gem install fastimage_resize
50
+ gem install fastimage_resize
35
51
  </pre>
36
52
  </code>
37
53
 
@@ -74,7 +90,7 @@ h2. Requirements
74
90
 
75
91
  <pre>
76
92
  <code>
77
- sudo gem install RubyInline
93
+ gem install RubyInline
78
94
  </pre>
79
95
  </code>
80
96
 
@@ -82,7 +98,7 @@ h2. Requirements
82
98
 
83
99
  <pre>
84
100
  <code>
85
- sudo gem install fastimage
101
+ gem install fastimage
86
102
  </pre>
87
103
  </code>
88
104
 
@@ -113,8 +129,16 @@ Because of the way that libgd works, gif files that have transparency may not al
113
129
 
114
130
  h2. Tests
115
131
 
116
- You'll need to 'sudo gem install fakeweb' to be able to run the tests
132
+ You'll need to 'gem install fakeweb' to be able to run the tests
117
133
 
118
134
  h2. References
119
135
 
120
136
  * "http://blog.new-bamboo.co.uk/2007/12/3/super-f-simple-resizing":http://blog.new-bamboo.co.uk/2007/12/3/super-f-simple-resizing
137
+
138
+ h2. Licence
139
+
140
+ MIT, see file MIT_LICENCE
141
+
142
+ h2. Author
143
+
144
+ Stephen Sykes, @sdsykes
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
- :patch: 4
3
- :major: 1
2
+ :patch: 0
3
+ :major: 2
4
4
  :build:
5
- :minor: 2
5
+ :minor: 0
@@ -4,18 +4,18 @@
4
4
  #
5
5
  # require 'fastimage_resize'
6
6
  #
7
- # FastImage.resize("http://stephensykes.com/images/ss.com_x.gif", "my.gif", 100, 20)
7
+ # FastImage.resize("http://stephensykes.com/images/ss.com_x.gif", 100, 20, :outfile=>"my.gif")
8
8
  # => 1
9
9
  #
10
10
  # === Requirements
11
11
  #
12
12
  # RubyInline
13
13
  #
14
- # sudo gem install RubyInline
14
+ # gem install RubyInline
15
15
  #
16
16
  # FastImage
17
17
  #
18
- # sudo gem install sdsykes-fastimage -s http://gems.github.com
18
+ # gem install fastimage
19
19
  #
20
20
  # Libgd
21
21
  #
@@ -37,28 +37,73 @@ class FastImage
37
37
  class FormatNotSupported < FastImageException # :nodoc:
38
38
  end
39
39
 
40
- def self.resize(uri_in, file_out, w, h, options={})
41
- jpeg_quality = options[:jpeg_quality] || -1
40
+ # Resizes an image, storing the result in a file given in file_out
41
+ #
42
+ # Input can be a filename, a uri, or an IO object.
43
+ #
44
+ # FastImage Resize can resize GIF, JPEG and PNG files.
45
+ #
46
+ # === Example
47
+ #
48
+ # require 'fastimage_resize'
49
+ #
50
+ # FastImage.resize("http://stephensykes.com/images/ss.com_x.gif", 100, 20, :outfile=>"my.gif")
51
+ #
52
+ # === Supported options
53
+ # [:jpeg_quality]
54
+ # A figure passed to libgd to determine quality of output jpeg (only useful if input is jpeg)
55
+ # [:outfile]
56
+ # Name of a file to store the output in, in this case a temp file is not used
57
+ #
58
+ def self.resize(input, w, h, options={})
59
+ jpeg_quality = options[:jpeg_quality].to_i || -1
60
+ file_out = options[:outfile]
42
61
 
43
- u = URI.parse(uri_in)
44
- if u.scheme == "http" || u.scheme == "https" || u.scheme == "ftp"
45
- f = Tempfile.new(name)
46
- f.write(open(u).read)
47
- f.close
48
- resize_local(f.path, file_out, w, h, jpeg_quality)
49
- File.unlink(f.path)
62
+ if input.respond_to?(:read)
63
+ file_in = read_to_local(input)
50
64
  else
51
- resize_local(uri_in, file_out, w, h, jpeg_quality)
65
+ u = URI.parse(input)
66
+ if u.scheme == "http" || u.scheme == "https" || u.scheme == "ftp"
67
+ file_in = read_to_local(open(u))
68
+ else
69
+ file_in = input.to_s
70
+ end
52
71
  end
72
+
73
+ if !file_out
74
+ temp_file = Tempfile.new(name)
75
+ file_out = temp_file.path
76
+ else
77
+ temp_file = nil
78
+ end
79
+
80
+ fast_image = new(file_in, :raise_on_failure=>true)
81
+ type_index = SUPPORTED_FORMATS.index(fast_image.type)
82
+ raise FormatNotSupported unless type_index
83
+
84
+ in_path = file_in.respond_to?(:path) ? file_in.path : file_in
85
+
86
+ fast_image.resize_image(in_path, file_out.to_s, w.to_i, h.to_i, type_index, jpeg_quality)
87
+
88
+ if file_in.respond_to?(:close)
89
+ file_in.close
90
+ file_in.unlink
91
+ end
92
+
93
+ temp_file
53
94
  rescue OpenURI::HTTPError, SocketError, URI::InvalidURIError, RuntimeError => e
54
95
  raise ImageFetchFailure, e.class
55
96
  end
56
97
 
57
- def self.resize_local(file_in, file_out, w, h, jpeg_quality)
58
- fi = new(file_in, :raise_on_failure=>true)
59
- type_index = SUPPORTED_FORMATS.index(fi.type)
60
- raise FormatNotSupported unless type_index
61
- fi.resize_image(file_in, file_out, w.to_i, h.to_i, type_index, jpeg_quality)
98
+ private
99
+
100
+ # returns readable tempfile
101
+ def self.read_to_local(readable)
102
+ temp = Tempfile.new(name)
103
+ temp.write(readable.read)
104
+ temp.close
105
+ temp.open
106
+ temp
62
107
  end
63
108
 
64
109
  def resize_image(filename_in, filename_out, w, h, image_type, jpeg_quality); end
data/test/test.rb CHANGED
@@ -41,20 +41,49 @@ END
41
41
  FakeWeb.register_uri(:get, "#{TestUrl}/redirect", :response=>redirect_response)
42
42
 
43
43
  class FastImageResizeTest < Test::Unit::TestCase
44
- def test_resize_image_types
44
+ def test_resize_image_types_from_http
45
45
  GoodFixtures.each do |fn, info|
46
46
  outfile = File.join(PathHere, "fixtures", "resized_" + fn)
47
- FastImage.resize(TestUrl + fn, outfile, info[1][0] / 3, info[1][1] / 2)
47
+ FastImage.resize(TestUrl + fn, info[1][0] / 3, info[1][1] / 2, :outfile=>outfile)
48
48
  assert_equal [info[1][0] / 3, info[1][1] / 2], FastImage.size(outfile)
49
49
  File.unlink outfile
50
50
  end
51
51
  end
52
52
 
53
+ def test_resize_image_types_from_files
54
+ GoodFixtures.each do |fn, info|
55
+ outfile = File.join(PathHere, "fixtures", "resized_" + fn)
56
+ FastImage.resize(File.join(FixturePath, fn), info[1][0] / 3, info[1][1] / 2, :outfile=>outfile)
57
+ assert_equal [info[1][0] / 3, info[1][1] / 2], FastImage.size(outfile)
58
+ File.unlink outfile
59
+ end
60
+ end
61
+
62
+ def test_resize_image_types_from_io_objects
63
+ GoodFixtures.each do |fn, info|
64
+ outfile = File.join(PathHere, "fixtures", "resized_" + fn)
65
+ File.open(File.join(FixturePath, fn)) do |io|
66
+ FastImage.resize(io, info[1][0] / 3, info[1][1] / 2, :outfile=>outfile)
67
+ assert_equal [info[1][0] / 3, info[1][1] / 2], FastImage.size(outfile)
68
+ File.unlink outfile
69
+ end
70
+ end
71
+ end
72
+
73
+ def test_resize_to_temp_file
74
+ GoodFixtures.each do |fn, info|
75
+ File.open(File.join(FixturePath, fn)) do |io|
76
+ outfile = FastImage.resize(io, info[1][0] / 3, info[1][1] / 2)
77
+ assert_equal [info[1][0] / 3, info[1][1] / 2], FastImage.size(outfile)
78
+ end
79
+ end
80
+ end
81
+
53
82
  def test_should_raise_for_bmp_files
54
83
  fn = BadFixtures[0]
55
84
  outfile = File.join(PathHere, "fixtures", "resized_" + fn)
56
85
  assert_raises(FastImage::FormatNotSupported) do
57
- FastImage.resize(TestUrl + fn, outfile, 20, 20)
86
+ FastImage.resize(TestUrl + fn, 20, 20, :outfile=>outfile)
58
87
  end
59
88
  end
60
89
 
@@ -62,7 +91,7 @@ class FastImageResizeTest < Test::Unit::TestCase
62
91
  fn = BadFixtures[1]
63
92
  outfile = File.join(PathHere, "fixtures", "resized_" + fn)
64
93
  assert_raises(FastImage::SizeNotFound) do
65
- FastImage.resize(TestUrl + fn, outfile, 20, 20)
94
+ FastImage.resize(TestUrl + fn, 20, 20, :outfile=>outfile)
66
95
  end
67
96
  end
68
97
 
@@ -70,19 +99,19 @@ class FastImageResizeTest < Test::Unit::TestCase
70
99
  fn = BadFixtures[2]
71
100
  outfile = File.join(PathHere, "fixtures", "resized_" + fn)
72
101
  assert_raises(FastImage::UnknownImageType) do
73
- FastImage.resize(TestUrl + fn, outfile, 20, 20)
102
+ FastImage.resize(TestUrl + fn, 20, 20, :outfile=>outfile)
74
103
  end
75
104
  end
76
105
 
77
106
  def test_should_raise_for_invalid_uri
78
107
  assert_raises(FastImage::ImageFetchFailure) do
79
- FastImage.resize("#{TestUrl}////%&redirect", "foo", 20, 20)
108
+ FastImage.resize("#{TestUrl}////%&redirect", 20, 20)
80
109
  end
81
110
  end
82
111
 
83
112
  def test_should_raise_for_redirect
84
113
  assert_raises(FastImage::ImageFetchFailure) do
85
- FastImage.resize("#{TestUrl}/redirect", "foo", 20, 20)
114
+ FastImage.resize("#{TestUrl}/redirect", 20, 20)
86
115
  end
87
116
  end
88
117
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastimage_resize
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
- - 1
8
7
  - 2
9
- - 4
10
- version: 1.2.4
8
+ - 0
9
+ - 0
10
+ version: 2.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Stephen Sykes
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-27 00:00:00 +03:00
18
+ date: 2011-10-03 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -42,12 +42,12 @@ dependencies:
42
42
  requirements:
43
43
  - - ">="
44
44
  - !ruby/object:Gem::Version
45
- hash: 31
45
+ hash: 13
46
46
  segments:
47
47
  - 1
48
48
  - 2
49
- - 0
50
- version: 1.2.0
49
+ - 9
50
+ version: 1.2.9
51
51
  type: :runtime
52
52
  version_requirements: *id002
53
53
  description: FastImage Resize is an extremely light solution for resizing images in ruby by using libgd.