vips-process 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: 8138e1fa1e52d3b4e2b8dcc77678c1b5e98b3d24
4
- data.tar.gz: ea0d7a7528ffffd0d2ee2f873c1e95eeac1c89d8
3
+ metadata.gz: 8c8e1e34b16c24916ce653fbfe90b964274f682a
4
+ data.tar.gz: 73aaec174e1753e341b62d09071e1df0fdbacd16
5
5
  SHA512:
6
- metadata.gz: 9b1747770629763e8dfefb56b4f8306c1e1913586d83c6a5531f020910583f3e41342d5d1cf10ee60dce9ba894226e71ba417e652702edb5a5b669195e8dd4c6
7
- data.tar.gz: 782ad36bcd197d06e71be2c2b4d911db552abef3fc0d54406df762baae44fdf70ab8f3111208848504c0763fb0a14592cf581f2c1f7036107566a72dbf997ed8
6
+ metadata.gz: a3b161ce5417e820d6b41e69174df595119902fea500ccc7c0e18be1c07f058a2583d14b175b209ef406c96c68dca8f298f157227fbeb35239c04eb60ac9de3f
7
+ data.tar.gz: d4168159430c88bc9a7dab8ad5ff0b5bc17e876900ad5fa5bf11d7c83ddaacfcd27b8f18c203e0cdbe1d9cd313b1e19679b2795d152c5a1ec1075ca0adeb9790
data/README.md CHANGED
@@ -164,6 +164,78 @@ The first argument is the `format` we'll conver the file to (jpeg or png) and
164
164
  the second one is an optional hash of `options` to be passed to the converting function
165
165
  (ie, :interlace => true for png).
166
166
 
167
+ ### Crop
168
+
169
+ Crop an image in the desired dimentions.
170
+
171
+ Usage:
172
+
173
+ Include it in your image class `include Vips::Process::Crop` and
174
+ call it `MyImage.new('/path/to/src.jpg').crop(height: 100, top: 0.5)`.
175
+
176
+ Because it's arguments can be used in so many different ways I've decided to make them keyword
177
+ arguments with a default that would give you the same image back.
178
+
179
+ There are four possible arguments in total: `width`, `height`, `left` and `top`.
180
+
181
+ `width` and `height` do just that: they crop the resulting image in size.
182
+
183
+ `left` and `top` are slightly different as they operate in two modes: if you use an Integer it's
184
+ the just the offset. However, if a Float between 0 and 1 is passed it's used to position the cropping
185
+ mask accordingly to the `width` and or `height` respectively. This is probable best seen with an
186
+ example:
187
+
188
+ ```
189
+ Given:
190
+
191
+ i=image cm=crop mask
192
+ ________ ________
193
+ | | | |
194
+ | | | |
195
+ | | --------
196
+ | |
197
+ | |
198
+ ________
199
+
200
+
201
+ crop height: cm.height, top: 0.0 will result in:
202
+
203
+ ________
204
+ | final|
205
+ | img |
206
+ --------
207
+ | |
208
+ | x |
209
+ ________
210
+
211
+ crop height: cm.height, top: 0.5 will result in:
212
+
213
+ ________
214
+ | x |
215
+ --------
216
+ | final|
217
+ | img |
218
+ --------
219
+ | x |
220
+ ________
221
+
222
+ crop height: cm.height, top: 1.0 will result in:
223
+ ________
224
+ | |
225
+ | x |
226
+ --------
227
+ | final|
228
+ | img |
229
+ ________
230
+ ```
231
+
232
+ It's also very powerful when used together with resize.
233
+ E.g.: say you have an image that is 3000x2000 px.
234
+ `image.resize_to_width(300).crop(height: 150, top: 0.5).process!` will first resize it to
235
+ 300x200 px and then it will crop it using a 150 height mask positioned in the middle of the
236
+ resized image. It will give you an image of full width but with height starting at 25px and
237
+ finishing at 175px. Here's a graphical example:
238
+
167
239
  ### Quality
168
240
 
169
241
  Changes quality of the image (if supported by the file format)
@@ -0,0 +1,80 @@
1
+ module Vips
2
+ module Process
3
+ module Crop
4
+ ##
5
+ # Crop an image in the desired dimentions.
6
+ #
7
+ # Pretty much all arguments are optional making it very flexible for you to create all
8
+ # sort of croppings.
9
+ #
10
+ # @param left Number if it's a Float between 0 and 1 it will use that to create a band
11
+ # in which it will displace the width of it.
12
+ # if it's an Integer it's the offset from the left.
13
+ # @param top Number if it's a Float between 0 and 1 it will use that to create a band
14
+ # in which it will displace the height of it.
15
+ # if it's an Integer it's the offset from the top.
16
+ # @param width Integer the width to crop to
17
+ # @param height Integer the height to crop to
18
+ #
19
+ # It's very powerful when used with resize. E.g.: say you have an image that is 3000x2000 px.
20
+ # `image.resize_to_width(300).crop(height: 150, top: 0.5).process!` will first resize it to
21
+ # 300x200 px and then it will crop it using a 150 height mask positioned in the middle of the
22
+ # resized image. It will give you an image of full width but with height starting at 25px and
23
+ # finishing at 175px. Here's a graphical example:
24
+ #
25
+ # Given:
26
+ #
27
+ # i=image cm=crop mask
28
+ # ________ ________
29
+ # | | | |
30
+ # | | | |
31
+ # | | --------
32
+ # | |
33
+ # | |
34
+ # ________
35
+ #
36
+ #
37
+ # crop height: cm.height, top: 0.0 will result in:
38
+ #
39
+ # ________
40
+ # | final|
41
+ # | img |
42
+ # --------
43
+ # | |
44
+ # | x |
45
+ # ________
46
+ #
47
+ # crop height: cm.height, top: 0.5 will result in:
48
+ #
49
+ # ________
50
+ # | x |
51
+ # --------
52
+ # | final|
53
+ # | img |
54
+ # --------
55
+ # | x |
56
+ # ________
57
+ #
58
+ # crop height: cm.height, top: 1.0 will result in:
59
+ # ________
60
+ # | |
61
+ # | x |
62
+ # --------
63
+ # | final|
64
+ # | img |
65
+ # ________
66
+ #
67
+ def crop(left: 0, top: 0, width: nil, height: nil)
68
+ manipulate! do |image|
69
+ width ||= image.x_size
70
+ height ||= image.y_size
71
+ top = top.is_a?(Float) && top.between?(0,1) ? (image.y_size - height) * top : top
72
+ left = left.is_a?(Float) && left.between?(0,1) ? (image.x_size - width) * left : left
73
+
74
+ image.extract_area left, top, width, height
75
+ end
76
+ self
77
+ end
78
+ end # Crop
79
+ end # Process
80
+ end # Vips
@@ -7,7 +7,10 @@ module Vips
7
7
  # @param percent Integer quality from 0 to 100
8
8
  def quality(percent=75)
9
9
  manipulate! do |image|
10
- @_format_opts = { quality: percent } if jpeg? || @_format == JPEG
10
+ if jpeg? || @_format == JPEG
11
+ @_format_opts ||= {}
12
+ @_format_opts[:quality]
13
+ end
11
14
  image
12
15
  end
13
16
  self
@@ -76,6 +76,18 @@ module Vips
76
76
  self
77
77
  end
78
78
 
79
+ ##
80
+ # Resize the image to a certain height. It will keep its width in relation.
81
+ #
82
+ # @param height Integer the height to scale the image to
83
+ def resize_to_height(height)
84
+ manipulate! do |image|
85
+ resize_image image, image.x_size, height
86
+ end
87
+ self
88
+ end
89
+
90
+
79
91
  private def resize_image(image, width, height, min_or_max = :min)
80
92
  ratio = get_ratio image, width, height, min_or_max
81
93
  return image if ratio == 1
@@ -0,0 +1,26 @@
1
+ # module Vips
2
+ # module Process
3
+ # module Shrink
4
+ # def shrink_load()
5
+ # if jpeg?
6
+ # image = VIPS::Image.jpeg @src
7
+
8
+ # largest_dimension = [image.x_size, image.y_size].max
9
+
10
+ # shrink = largest_dimension / size.to_f
11
+ # @_format_opts[:shrink_factor] = factor
12
+
13
+ # end
14
+ # end
15
+
16
+ # def shrink(factor)
17
+ # manipulate! do |image|
18
+ # if jpeg? || @_format == JPEG
19
+ # end
20
+ # image
21
+ # end
22
+ # self
23
+ # end
24
+ # end # Shrink
25
+ # end # Process
26
+ # end # Vips
@@ -1,5 +1,5 @@
1
1
  module Vips
2
2
  module Process
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
data/lib/vips-process.rb CHANGED
@@ -6,26 +6,23 @@ module Vips
6
6
  JPEG = 'jpeg'.freeze
7
7
  PNG = 'png'.freeze
8
8
 
9
+ def sequential(val=true)
10
+ @_load_opts[:sequential] = val if jpeg? || png?
11
+ self
12
+ end
13
+
9
14
  ##
10
15
  # Manipulate the image with Vips. Saving of the image is delayed until after
11
16
  # all the process blocks have been called. Make sure you always return an
12
17
  # VIPS::Image object from the block
13
18
  #
14
19
  # This method yields VIPS::Image for further manipulation.
15
- #
16
- # It also raises an Exception if the manipulation failed.
17
20
  def manipulate!
21
+ @_load_opts ||= {}
18
22
  @_on_process ||= []
19
- @_vimage ||= if jpeg?
20
- VIPS::Image.jpeg @src, sequential: true
21
- elsif png?
22
- VIPS::Image.png @src, sequential: true
23
- else
24
- VIPS::Image.new @src
25
- end
26
- @_vimage = yield @_vimage
27
- rescue => e
28
- raise Exception.new("Failed to manipulate file, maybe it is not a supported image? Original Error: #{e}")
23
+ @_type ||= jpeg? ? :jpeg : (png? ? :png : :new)
24
+ @_vimage ||= VIPS::Image.send @_type, @src, @_load_opts
25
+ @_vimage = yield @_vimage
29
26
  end
30
27
 
31
28
  def process!
@@ -72,8 +69,9 @@ module Vips
72
69
  end
73
70
 
74
71
  private def reset!
72
+ @_load_opts = {}
75
73
  @_on_process = []
76
- @_format_opts = nil
74
+ @_format_opts = {}
77
75
  @_vimage = nil
78
76
  end
79
77
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vips-process
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
  - Darío Javier Cravero
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-27 00:00:00.000000000 Z
11
+ date: 2014-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-vips
@@ -96,9 +96,11 @@ files:
96
96
  - lib/vips-process/auto-orient.rb
97
97
  - lib/vips-process/base.rb
98
98
  - lib/vips-process/convert.rb
99
+ - lib/vips-process/crop.rb
99
100
  - lib/vips-process/gaussian-blur.rb
100
101
  - lib/vips-process/quality.rb
101
102
  - lib/vips-process/resize.rb
103
+ - lib/vips-process/shrink.rb
102
104
  - lib/vips-process/strip.rb
103
105
  - lib/vips-process/version.rb
104
106
  - test/helpers.rb