image_processing 0.4.5 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of image_processing might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +65 -0
- data/README.md +366 -26
- data/image_processing.gemspec +3 -2
- data/lib/image_processing.rb +1 -0
- data/lib/image_processing/mini_magick.rb +1 -1
- data/lib/image_processing/version.rb +1 -1
- data/lib/image_processing/vips.rb +13 -0
- data/lib/image_processing/vips/chainable.rb +61 -0
- data/lib/image_processing/vips/color.rb +584 -0
- data/lib/image_processing/vips/pipeline.rb +30 -0
- data/lib/image_processing/vips/processor.rb +83 -0
- metadata +24 -4
@@ -0,0 +1,30 @@
|
|
1
|
+
module ImageProcessing
|
2
|
+
module Vips
|
3
|
+
class Pipeline
|
4
|
+
include Chainable
|
5
|
+
|
6
|
+
def initialize(options)
|
7
|
+
@default_options = options
|
8
|
+
end
|
9
|
+
|
10
|
+
def call!(save: true)
|
11
|
+
processor = Processor.new(default_options[:source])
|
12
|
+
image = processor.load_image(default_options[:loader])
|
13
|
+
|
14
|
+
default_options[:operations].each do |name, args|
|
15
|
+
if name == :custom
|
16
|
+
image = args.first.call(image)
|
17
|
+
else
|
18
|
+
image = processor.apply_operation(name, image, *args)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
if save
|
23
|
+
processor.save_image(image, default_options[:format], default_options[:saver])
|
24
|
+
else
|
25
|
+
image
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
gem "ruby-vips", "~> 2.0"
|
2
|
+
require "vips"
|
3
|
+
fail "image_processing/vips requires libvips 8.6+" unless Vips.at_least_libvips?(8, 6)
|
4
|
+
|
5
|
+
require "image_processing/vips/color"
|
6
|
+
require "tempfile"
|
7
|
+
|
8
|
+
module ImageProcessing
|
9
|
+
module Vips
|
10
|
+
class Processor
|
11
|
+
# libvips has this arbitrary number as a sanity-check upper bound on image
|
12
|
+
# size.
|
13
|
+
MAX_COORD = 10_000_000
|
14
|
+
|
15
|
+
def initialize(source)
|
16
|
+
fail Error, "source file not provided" unless source
|
17
|
+
fail Error, "source file doesn't respond to #path" unless source.respond_to?(:path) || source.is_a?(::Vips::Image)
|
18
|
+
|
19
|
+
@source = source
|
20
|
+
end
|
21
|
+
|
22
|
+
def apply_operation(name, image, *args)
|
23
|
+
if respond_to?(name)
|
24
|
+
public_send(name, image, *args)
|
25
|
+
else
|
26
|
+
result = image.send(name, *args)
|
27
|
+
result.is_a?(::Vips::Image) ? result : image
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def resize_to_limit(image, width, height, **options)
|
32
|
+
width, height = default_dimensions(width, height)
|
33
|
+
image.thumbnail_image(width, height: height, size: :down, **options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def resize_to_fit(image, width, height, **options)
|
37
|
+
width, height = default_dimensions(width, height)
|
38
|
+
image.thumbnail_image(width, height: height, **options)
|
39
|
+
end
|
40
|
+
|
41
|
+
def resize_to_fill(image, width, height, **options)
|
42
|
+
image.thumbnail_image(width, height: height, crop: :centre, **options)
|
43
|
+
end
|
44
|
+
|
45
|
+
def resize_and_pad(image, width, height, background: "opaque", gravity: "centre", **options)
|
46
|
+
image.thumbnail_image(width, height: height, **options)
|
47
|
+
.gravity(gravity, width, height, extend: :background, background: Color.get(background))
|
48
|
+
end
|
49
|
+
|
50
|
+
def load_image(**options)
|
51
|
+
return @source if @source.is_a?(::Vips::Image)
|
52
|
+
|
53
|
+
::Vips::Image.new_from_file(@source.path, fail: true, **options)
|
54
|
+
end
|
55
|
+
|
56
|
+
def save_image(image, format, **options)
|
57
|
+
format ||= default_format
|
58
|
+
result = Tempfile.new(["image_processing-vips", ".#{format}"], binmode: true)
|
59
|
+
|
60
|
+
image.write_to_file(result.path, **options)
|
61
|
+
result.open # refresh content
|
62
|
+
|
63
|
+
result
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def default_dimensions(width, height)
|
69
|
+
raise Error, "either width or height must be specified" unless width || height
|
70
|
+
|
71
|
+
[width || MAX_COORD, height || MAX_COORD]
|
72
|
+
end
|
73
|
+
|
74
|
+
def default_format
|
75
|
+
File.extname(original_path.to_s)[1..-1] || "jpg"
|
76
|
+
end
|
77
|
+
|
78
|
+
def original_path
|
79
|
+
@source.path if @source.respond_to?(:path)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: image_processing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Janko Marohnić
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 1.4.2
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 1.4.2
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minispec-metadata
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 4.3.5
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: ruby-vips
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.0.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 2.0.0
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: phashion
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -87,12 +101,18 @@ executables: []
|
|
87
101
|
extensions: []
|
88
102
|
extra_rdoc_files: []
|
89
103
|
files:
|
104
|
+
- CHANGELOG.md
|
90
105
|
- LICENSE.txt
|
91
106
|
- README.md
|
92
107
|
- image_processing.gemspec
|
93
108
|
- lib/image_processing.rb
|
94
109
|
- lib/image_processing/mini_magick.rb
|
95
110
|
- lib/image_processing/version.rb
|
111
|
+
- lib/image_processing/vips.rb
|
112
|
+
- lib/image_processing/vips/chainable.rb
|
113
|
+
- lib/image_processing/vips/color.rb
|
114
|
+
- lib/image_processing/vips/pipeline.rb
|
115
|
+
- lib/image_processing/vips/processor.rb
|
96
116
|
homepage: https://github.com/janko-m/image_processing
|
97
117
|
licenses:
|
98
118
|
- MIT
|