snapimage 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -29,12 +29,14 @@ module SnapImage
29
29
  # Arguments:
30
30
  # * path:: Local path or URL
31
31
  def set_image_from_path(path)
32
+ destroy!
32
33
  @image = Magick::ImageList.new(path)
33
34
  end
34
35
 
35
36
  # Arguments:
36
37
  # * blob:: Image blob
37
38
  def set_image_from_blob(blob)
39
+ destroy!
38
40
  @image = Magick::ImageList.new
39
41
  @image.from_blob(blob)
40
42
  end
@@ -42,6 +44,7 @@ module SnapImage
42
44
  # Arguments:
43
45
  # * image:: RMagick Image
44
46
  def set_image_from_image(image)
47
+ destroy!
45
48
  @image = image
46
49
  end
47
50
 
@@ -58,11 +61,10 @@ module SnapImage
58
61
  end
59
62
 
60
63
  def destroy!
61
- @image.destroy!
64
+ @image.destroy! if @image && !@image.destroyed?
62
65
  end
63
66
 
64
- # Crops the image with the given parameters and returns a SnapImage::Image
65
- # object.
67
+ # Crops the image with the given parameters.
66
68
  #
67
69
  # Arguments:
68
70
  # * x:: x coordinate of the top left corner
@@ -70,10 +72,10 @@ module SnapImage
70
72
  # * width:: width of the crop rectangle
71
73
  # * height:: height of the crop rectangle
72
74
  def crop(x, y, width, height)
73
- SnapImage::Image.from_image(@image.crop(x, y, width, height))
75
+ @image.crop!(x, y, width, height)
74
76
  end
75
77
 
76
- # Generates a new resized image and returns it as a SnapImage::Image object.
78
+ # Resizes the image with the given paramaters.
77
79
  #
78
80
  # Arguments:
79
81
  # * width:: Width to resize to
@@ -85,16 +87,17 @@ module SnapImage
85
87
  # resizing.
86
88
  height ||= [width, @image.rows].max
87
89
  if maintain_aspect_ratio
88
- SnapImage::Image.from_image(@image.resize_to_fit(width, height))
90
+ @image.resize_to_fit!(width, height)
89
91
  else
90
- SnapImage::Image.from_image(@image.resize(width, height))
92
+ @image.resize!(width, height)
91
93
  end
92
94
  end
93
95
 
94
- # Generates a new sharpened image and returns it as a SnapImage::Image
95
- # object.
96
+ # Sharpens the image.
96
97
  def sharpen
97
- SnapImage::Image.from_image(@image.sharpen)
98
+ image = @image.sharpen
99
+ destroy!
100
+ @image = image
98
101
  end
99
102
  end
100
103
  end
@@ -26,7 +26,7 @@ module SnapImage
26
26
  image_height: stored_image.height
27
27
  )
28
28
  # Release memory.
29
- stored_image.destroy!
29
+ GC.start
30
30
  else
31
31
  @response.set_bad_request
32
32
  end
@@ -122,10 +122,7 @@ module SnapImage
122
122
  width: @request.json["crop_width"],
123
123
  height: @request.json["crop_height"]
124
124
  }
125
- cropped_image = image.crop(crop[:x], crop[:y], crop[:width], crop[:height])
126
- # Release memory.
127
- image.destroy!
128
- image = cropped_image
125
+ image.crop(crop[:x], crop[:y], crop[:width], crop[:height])
129
126
  end
130
127
 
131
128
  # Resize.
@@ -136,34 +133,21 @@ module SnapImage
136
133
  if width && height
137
134
  # When both width and height are specified, resize without
138
135
  # maintaining the aspect ratio.
139
- resized_image = image.resize(width, height, false)
136
+ image.resize(width, height, false)
140
137
  else
141
138
  # When only one of width/height is specified, set the other to the
142
139
  # max and maintain the aspect ratio.
143
- resized_image = image.resize(width || @config["max_width"], height || @config["max_height"])
140
+ image.resize(width || @config["max_width"], height || @config["max_height"])
144
141
  end
145
- # Release memeory.
146
- image.destroy!
147
- image = resized_image
148
142
  end
149
143
 
150
144
  # Resize to fit.
151
145
  resized_to_fit = resize_to_fit?(image)
152
- if resized_to_fit
153
- resized_image = image.resize(get_max_width, get_max_height)
154
- # Release memeory.
155
- image.destroy!
156
- image = resized_image
157
- end
146
+ image.resize(get_max_width, get_max_height) if resized_to_fit
158
147
 
159
148
  # Sharpen.
160
149
  sharpened = sharpen?
161
- if sharpened
162
- sharpened_image = image.sharpen
163
- # Release memeory.
164
- image.destroy!
165
- image = sharpened_image
166
- end
150
+ image.sharpen if sharpened
167
151
 
168
152
  # Get the dimensions at the end.
169
153
  if cropped || resized || resized_to_fit || sharpened
@@ -99,13 +99,11 @@ module SnapImage
99
99
  def resize_to_fit(image, name)
100
100
  # Resize the image if it's larger than the max width/height.
101
101
  if image.width > @config["max_width"] || image.height > @config["max_height"]
102
- resized_image = image.resize([image.width, @config["max_width"]].min, [image.height, @config["max_height"]].min)
102
+ image.resize([image.width, @config["max_width"]].min, [image.height, @config["max_height"]].min)
103
103
  # Generate a new name.
104
- resized_name = SnapImage::ImageNameUtils.get_resized_image_name(name, resized_image.width, resized_image.height)
105
- # Release memory.
106
- image.destroy!
104
+ name = SnapImage::ImageNameUtils.get_resized_image_name(name, image.width, image.height)
107
105
  end
108
- { image: resized_image || image, name: resized_name || name }
106
+ { image: image, name: name }
109
107
  end
110
108
  end
111
109
  end
@@ -1,3 +1,3 @@
1
1
  module SnapImage
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -9,20 +9,12 @@ describe SnapImage::Image do
9
9
  before do
10
10
  @image = SnapImage::Image.new
11
11
  @image.set_image_from_path(@image_path)
12
- @original_width = @image.width
13
- @original_height = @image.height
14
12
  end
15
13
 
16
- it "returns the correctly cropped image" do
17
- image = @image.crop(10, 30, 20, 30)
18
- image.width.should eq 20
19
- image.height.should eq 30
20
- end
21
-
22
- it "does not modify the original image" do
23
- image = @image.crop(10, 30, 20, 30)
24
- @image.width.should eq @original_width
25
- @image.height.should eq @original_height
14
+ it "correctly crops image" do
15
+ @image.crop(10, 30, 20, 30)
16
+ @image.width.should eq 20
17
+ @image.height.should eq 30
26
18
  end
27
19
  end
28
20
 
@@ -30,42 +22,34 @@ describe SnapImage::Image do
30
22
  before do
31
23
  @image = SnapImage::Image.new
32
24
  @image.set_image_from_path(@image_path)
33
- @original_width = @image.width
34
- @original_height = @image.height
35
25
  end
36
26
 
37
27
  it "raises an error when no height is specified and maintaining aspect ratio is false" do
38
28
  expect { @image.resize(100, nil, false) }.should raise_error
39
29
  end
40
30
 
41
- it "returns an appropriately resized image when the width is larger or equal to the height" do
42
- image = @image.resize(330)
43
- image.width.should eq 330
44
- image.height.should eq 220
45
- end
46
-
47
- it "returns an appropriately resized image when the width is less than the height" do
48
- image = @image.resize(150)
49
- image.width.should eq 150
50
- image.height.should eq 100
31
+ it "resizes the image when the width is larger or equal to the height" do
32
+ @image.resize(330)
33
+ @image.width.should eq 330
34
+ @image.height.should eq 220
51
35
  end
52
36
 
53
- it "returns an image that fits within the width/height while maintaining aspect ratio" do
54
- image = @image.resize(150, 200)
55
- image.width.should eq 150
56
- image.height.should eq 100
37
+ it "resizes the image when the width is less than the height" do
38
+ @image.resize(150)
39
+ @image.width.should eq 150
40
+ @image.height.should eq 100
57
41
  end
58
42
 
59
- it "returns a stretched image when not maintaining aspect ratio" do
60
- image = @image.resize(100, 200, false)
61
- image.width.should eq 100
62
- image.height.should eq 200
43
+ it "resizes the image to fit within the width/height while maintaining aspect ratio" do
44
+ @image.resize(150, 200)
45
+ @image.width.should eq 150
46
+ @image.height.should eq 100
63
47
  end
64
48
 
65
- it "does not modify the original image" do
66
- image = @image.resize(100)
67
- @image.width.should eq @original_width
68
- @image.height.should eq @original_height
49
+ it "stretches image when not maintaining aspect ratio" do
50
+ @image.resize(100, 200, false)
51
+ @image.width.should eq 100
52
+ @image.height.should eq 200
69
53
  end
70
54
  end
71
55
  end
@@ -79,16 +79,16 @@ describe SnapImage::StorageServer::Base do
79
79
  end
80
80
 
81
81
  it "returns the resized image and name given a base image" do
82
- image = @image.resize(2048, 100, false)
83
- result = @server.send(:resize_to_fit, image, "12345678-2048x100.png")
82
+ @image.resize(2048, 100, false)
83
+ result = @server.send(:resize_to_fit, @image, "12345678-2048x100.png")
84
84
  result[:image].width.should eq 1024
85
85
  result[:image].height.should eq 50
86
86
  result[:name].should eq "12345678-1024x50.png"
87
87
  end
88
88
 
89
89
  it "returns the resized image and name given a modified image" do
90
- image = @image.resize(2048, 100, false)
91
- result = @server.send(:resize_to_fit, image, "12345678-1x1-0x0x1x1-2048x100-0.png")
90
+ @image.resize(2048, 100, false)
91
+ result = @server.send(:resize_to_fit, @image, "12345678-1x1-0x0x1x1-2048x100-0.png")
92
92
  result[:image].width.should eq 1024
93
93
  result[:image].height.should eq 50
94
94
  result[:name].should eq "12345678-1x1-0x0x1x1-1024x50-0.png"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snapimage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -222,7 +222,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
222
222
  version: '0'
223
223
  segments:
224
224
  - 0
225
- hash: 1963487119924646018
225
+ hash: 1128448920208195157
226
226
  required_rubygems_version: !ruby/object:Gem::Requirement
227
227
  none: false
228
228
  requirements:
@@ -231,7 +231,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
231
231
  version: '0'
232
232
  segments:
233
233
  - 0
234
- hash: 1963487119924646018
234
+ hash: 1128448920208195157
235
235
  requirements: []
236
236
  rubyforge_project:
237
237
  rubygems_version: 1.8.23