vips-process 0.0.1 → 0.1.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: 28664d855e6a202bf807e8d88369d9033c5a2161
4
- data.tar.gz: 5d784293a361981a59145d388c46107cac41703d
3
+ metadata.gz: 8138e1fa1e52d3b4e2b8dcc77678c1b5e98b3d24
4
+ data.tar.gz: ea0d7a7528ffffd0d2ee2f873c1e95eeac1c89d8
5
5
  SHA512:
6
- metadata.gz: 9473e1b51f4011cad6387e3c2994c36936ceb6cae84421e76fbea7230863c27c52f90e2b0f6475c6e516bc426c8214ee44e7b09c323d3af6780a499839d5494c
7
- data.tar.gz: 60445478ea6e610c7c59b008eacdcda359fdd9b6a34c96889a3891dcc8b2614994387acc3894f1c121d866c10acd83932e1b2ac409400c0cf363ed0c209d0828
6
+ metadata.gz: 9b1747770629763e8dfefb56b4f8306c1e1913586d83c6a5531f020910583f3e41342d5d1cf10ee60dce9ba894226e71ba417e652702edb5a5b669195e8dd4c6
7
+ data.tar.gz: 782ad36bcd197d06e71be2c2b4d911db552abef3fc0d54406df762baae44fdf70ab8f3111208848504c0763fb0a14592cf581f2c1f7036107566a72dbf997ed8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- vips-process (0.0.1)
4
+ vips-process (0.1.0)
5
5
  ruby-vips (~> 0.3.9)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -74,6 +74,57 @@ class MyImage < Vips::Process::Base
74
74
  end
75
75
  ```
76
76
 
77
+ ### Working with versions
78
+
79
+ Version dependencies are recursive. I.e., if you have:
80
+
81
+ ```
82
+ class MyImage < Vips::Process::Base
83
+ # ...
84
+
85
+ version(:a) { ... }
86
+ version(:b, [:a])
87
+ version(:c, [:b])
88
+
89
+ # ...
90
+ end
91
+ ```
92
+ Calling `image.c_version` will actually call `image.b_version` which will in turn call
93
+ `image.a_version` and then run version `b`'s code.
94
+
95
+
96
+ `MyImage.versions` will return an array with the list of versions your image supports.
97
+
98
+
99
+ Calling `image.versions!` will process all versions at once and will return an array with tuples
100
+ like `[version_name, output_path]`. E.g. with the image class above:
101
+
102
+ ```
103
+ image = MyImage.new('/path/to/src.jpg')
104
+ image.versions! #=> [[:a, '/path/to/src-a.jpg'], [:b, '/path/to/src-b.jpg'], [:c, '/path/to/src-c.jpg']]
105
+ ```
106
+
107
+ `versions!` takes one optional argument: the `base` destination. This could be either a directory
108
+ or a filename.
109
+
110
+ If you use a directory, all new files will be written to that directory
111
+ (which will be created recursively if it doesn't exist) with the version's name followed by the `src`
112
+ file extension. E.g.:
113
+ ```
114
+ image = MyImage.new('/path/to/src.jpg')
115
+ image.versions!('/path/to/dir/') #=> [[:a, '/path/to/dir/a.jpg'], [:b, '/path/to/dir/b.jpg'], [:c, '/path/to/dir/c.jpg']]
116
+ ```
117
+
118
+ If you use a filename, all new files will be written next to that file prepending the version's name
119
+ and using its extension. E.g.:
120
+
121
+ ```
122
+ image = MyImage.new('/path/to/src.jpg')
123
+ image.versions!('/path/to/dst.jpeg') #=> [[:a, '/path/to/dst-a.jpeg'], [:b, '/path/to/dest-b.jpeg'], [:c, '/path/to/dest-c.jpg']]
124
+ ```
125
+
126
+ By default we use the `src`'s filename.
127
+
77
128
  ## Supported processes
78
129
 
79
130
  All examples live in the [/examples](https://github.com/dariocravero/vips-process/tree/master/examples)
data/lib/vips-process.rb CHANGED
@@ -55,6 +55,22 @@ module Vips
55
55
  self
56
56
  end
57
57
 
58
+ ##
59
+ # Process all versions using and output them in a directory
60
+ #
61
+ # @param base String the base filename/path where the versions will be saved
62
+ def versions!(base=@dst)
63
+ base_dir, base_ext, base_filename = File.file?(base) ?
64
+ [File.dirname(base), File.extname(base), "#{File.basename(base, File.extname(base))}-"] :
65
+ [base, File.extname(@src), nil]
66
+
67
+ FileUtils.mkdir_p base_dir unless File.directory?(base_dir) && File.exist?(base_dir)
68
+
69
+ self.class.versions.map do |name|
70
+ [name, send("#{name}_version", File.join(base_dir, "#{base_filename}#{name}#{base_ext}"))]
71
+ end
72
+ end
73
+
58
74
  private def reset!
59
75
  @_on_process = []
60
76
  @_format_opts = nil
@@ -80,6 +96,11 @@ module Vips
80
96
  ##
81
97
  # Define a version
82
98
  #
99
+ # A version will then take optional arguments: `new_dst` which sets the output for
100
+ # the version (and reverts back to the previous dt) and `should_process` which tells
101
+ # whether we should process the version after running its code or not - comes in handy to
102
+ # stack them up.
103
+ #
83
104
  # @param name String the version's name
84
105
  # @param deps [] the version's dependencies, it's a list of version names
85
106
  # @param &block block if you send a block to it
@@ -89,13 +110,31 @@ module Vips
89
110
  @@_versions ||= {}
90
111
  @@_versions[name] = {deps: deps, block: block}
91
112
 
92
- define_method "#{name}_version" do |new_dst=nil|
93
- @dst = new_dst if new_dst
94
- @@_versions[name][:deps].each { |dep| instance_eval &@@_versions[dep][:block] }
113
+ define_method "#{name}_version" do |new_dst=nil, should_process=true|
114
+ # Make sure we have a reference to the old version if it's being changed
115
+ if new_dst
116
+ old_dst = @dst
117
+ @dst = new_dst
118
+ end
119
+
120
+ # Recursively call dependencies but don't process them yet
121
+ @@_versions[name][:deps].each { |dep| send "#{dep}_version", new_dst, false }
122
+
123
+ # Run the version's block
95
124
  instance_eval &@@_versions[name][:block]
96
- process!
125
+
126
+ # Process if we were explicitly told to do so
127
+ version_dst = process! if should_process
128
+
129
+ # Revert to the old destination if we changed it during the version
130
+ @dst = old_dst if old_dst
131
+
132
+ version_dst
97
133
  end
98
134
  end
135
+
136
+ # Get all the version keys
137
+ def versions; @@_versions.keys; end
99
138
  end
100
139
  end
101
140
  end
@@ -12,7 +12,7 @@ module Vips
12
12
  ##
13
13
  # Apply gaussian blur to an image.
14
14
  #
15
- # @param sigma Integer roughly the radius
15
+ # @param sigma Float roughly the radius
16
16
  # @param min_ampl Float minimium amplitude we consider, it sets how far out the mask goes
17
17
  def gaussian_blur(sigma, min_ampl=0.2)
18
18
  manipulate! do |image|
@@ -65,6 +65,17 @@ module Vips
65
65
  self
66
66
  end
67
67
 
68
+ ##
69
+ # Resize the image to a certain width. It will keep its height in relation.
70
+ #
71
+ # @param width Integer the width to scale the image to
72
+ def resize_to_width(width)
73
+ manipulate! do |image|
74
+ resize_image image, width, image.y_size
75
+ end
76
+ self
77
+ end
78
+
68
79
  private def resize_image(image, width, height, min_or_max = :min)
69
80
  ratio = get_ratio image, width, height, min_or_max
70
81
  return image if ratio == 1
@@ -83,7 +94,7 @@ module Vips
83
94
  image
84
95
  end
85
96
 
86
- private def get_ratio(image, width,height, min_or_max = :min)
97
+ private def get_ratio(image, width, height, min_or_max = :min)
87
98
  width_ratio = width.to_f / image.x_size
88
99
  height_ratio = height.to_f / image.y_size
89
100
  [width_ratio, height_ratio].send min_or_max
@@ -1,5 +1,5 @@
1
1
  module Vips
2
2
  module Process
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
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.0.1
4
+ version: 0.1.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-26 00:00:00.000000000 Z
11
+ date: 2014-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-vips