gallerize-cli 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/lib/gallerize_cli/image/version.rb +58 -22
- data/lib/gallerize_cli.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f139f5fd7ec173ae67a63fe36a94e2ecbcc1bb30
|
4
|
+
data.tar.gz: 66ff2ddb1abcbece1223698681e04dc75f41ce2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbee1495a2c08ff0fdeda1ff1aa5dc6dd13e1a8e743472e1a09f1f62d010186e8113b7c62a08bd9be7aa698e05e6b4a6959c0af43af5b99c8092ff75dbb10a95
|
7
|
+
data.tar.gz: 096d0478e2cad463e793fd6c5cc6b8d6c294e0c8f76fb4923d60b7a054e429b526b35470c23c37c9326453620738bfc8b51437bc2756edd718476f9bc396a08f
|
data/.gitignore
CHANGED
@@ -8,6 +8,8 @@ module GallerizeCli
|
|
8
8
|
|
9
9
|
attr_accessor :image, :name, :options
|
10
10
|
|
11
|
+
GRAVITY_TYPES = [:north_west, :north, :north_east, :east, :south_east, :south, :south_west, :west, :center]
|
12
|
+
|
11
13
|
def initialize(image, name, options)
|
12
14
|
@image = image
|
13
15
|
@name = name
|
@@ -46,46 +48,80 @@ module GallerizeCli
|
|
46
48
|
private
|
47
49
|
|
48
50
|
def generate
|
49
|
-
|
51
|
+
|
52
|
+
|
50
53
|
if width <= 0 || height <= 0
|
51
54
|
GallerizeCli.logger.debug "version: #{name} is missing width: #{width} or height: #{height}"
|
52
55
|
|
53
56
|
elsif !File.exists?(file_path)
|
54
57
|
GallerizeCli.logger.debug "generating #{options.approach} #{file_path}"
|
55
58
|
# open it up
|
56
|
-
|
57
|
-
|
58
|
-
if options.approach == 'crop'
|
59
|
-
crop(mini_image, width, height)
|
60
|
-
else
|
61
|
-
resize(mini_image, width, height)
|
62
|
-
end
|
59
|
+
# do we have width and height?
|
60
|
+
new_image = resize_with_crop(MiniMagick::Image.open(image.file_path), width, height)
|
63
61
|
# landscape?
|
64
|
-
|
62
|
+
new_image.write file_path
|
65
63
|
end
|
66
64
|
rescue => err
|
67
65
|
@valid = false
|
68
66
|
GallerizeCli.logger.debug "#{err} image.file_name: #{image.file_name} name: #{name} options: #{options}"
|
69
67
|
end
|
70
68
|
|
71
|
-
def
|
72
|
-
|
73
|
-
|
69
|
+
def resize_with_crop(img, w, h, options = {})
|
70
|
+
gravity = options[:gravity] || :center
|
71
|
+
|
72
|
+
w_original, h_original = [img[:width].to_f, img[:height].to_f]
|
73
|
+
|
74
|
+
op_resize = ''
|
75
|
+
|
76
|
+
# check proportions
|
77
|
+
if w_original * h < h_original * w
|
78
|
+
op_resize = "#{w.to_i}x"
|
79
|
+
w_result = w
|
80
|
+
h_result = (h_original * w / w_original)
|
74
81
|
else
|
75
|
-
#
|
76
|
-
|
82
|
+
op_resize = "x#{h.to_i}"
|
83
|
+
w_result = (w_original * h / h_original)
|
84
|
+
h_result = h
|
77
85
|
end
|
78
|
-
end
|
79
86
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
resize(
|
84
|
-
|
85
|
-
|
87
|
+
w_offset, h_offset = crop_offsets_by_gravity(gravity, [w_result, h_result], [w, h])
|
88
|
+
|
89
|
+
img.combine_options do |i|
|
90
|
+
i.resize(op_resize)
|
91
|
+
i.gravity(gravity)
|
92
|
+
i.crop "#{w.to_i}x#{h.to_i}+#{w_offset}+#{h_offset}!"
|
86
93
|
end
|
87
94
|
|
88
|
-
|
95
|
+
img
|
96
|
+
end
|
97
|
+
|
98
|
+
def crop_offsets_by_gravity(gravity, original_dimensions, cropped_dimensions)
|
99
|
+
raise(ArgumentError, "Gravity must be one of #{GRAVITY_TYPES.inspect}") unless GRAVITY_TYPES.include?(gravity.to_sym)
|
100
|
+
raise(ArgumentError, "Original dimensions must be supplied as a [ width, height ] array") unless original_dimensions.kind_of?(Enumerable) && original_dimensions.size == 2
|
101
|
+
raise(ArgumentError, "Cropped dimensions must be supplied as a [ width, height ] array") unless cropped_dimensions.kind_of?(Enumerable) && cropped_dimensions.size == 2
|
102
|
+
|
103
|
+
original_width, original_height = original_dimensions
|
104
|
+
cropped_width, cropped_height = cropped_dimensions
|
105
|
+
|
106
|
+
vertical_offset = case gravity
|
107
|
+
when :north_west, :north, :north_east then
|
108
|
+
0
|
109
|
+
when :center, :east, :west then
|
110
|
+
[((original_height - cropped_height) / 2.0).to_i, 0].max
|
111
|
+
when :south_west, :south, :south_east then
|
112
|
+
(original_height - cropped_height).to_i
|
113
|
+
end
|
114
|
+
|
115
|
+
horizontal_offset = case gravity
|
116
|
+
when :north_west, :west, :south_west then
|
117
|
+
0
|
118
|
+
when :center, :north, :south then
|
119
|
+
[((original_width - cropped_width) / 2.0).to_i, 0].max
|
120
|
+
when :north_east, :east, :south_east then
|
121
|
+
(original_width - cropped_width).to_i
|
122
|
+
end
|
123
|
+
|
124
|
+
[horizontal_offset, vertical_offset]
|
89
125
|
end
|
90
126
|
|
91
127
|
def load_file_path
|
data/lib/gallerize_cli.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gallerize-cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Blake Hilscher
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-05-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mini_magick
|