image_processing 0.9.0 → 0.10.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.

@@ -1,83 +0,0 @@
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