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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11dc0a78dfd751b1b510ac507825a4d15ef091e3
4
- data.tar.gz: 446f94708dc90cf9cd4fd5398d329f381c163efc
3
+ metadata.gz: f139f5fd7ec173ae67a63fe36a94e2ecbcc1bb30
4
+ data.tar.gz: 66ff2ddb1abcbece1223698681e04dc75f41ce2a
5
5
  SHA512:
6
- metadata.gz: 2f23f096b5e3faa8980068b4eb27337ef8e2285b506c5f907864f2c0de0a3ff1ea62299ca546e1cb6fbb4cb855fe95e0ec19510449b1ae1054e6ef065992fc4b
7
- data.tar.gz: 8918d774bac3a4041343775324b24b76767241cef91f707eaba78f08c38f02688dbbf88f9f65b5b682265d5bed9c537e3fb9e8de411f58eb5172b510a7efa0b0
6
+ metadata.gz: dbee1495a2c08ff0fdeda1ff1aa5dc6dd13e1a8e743472e1a09f1f62d010186e8113b7c62a08bd9be7aa698e05e6b4a6959c0af43af5b99c8092ff75dbb10a95
7
+ data.tar.gz: 096d0478e2cad463e793fd6c5cc6b8d6c294e0c8f76fb4923d60b7a054e429b526b35470c23c37c9326453620738bfc8b51437bc2756edd718476f9bc396a08f
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  .idea
2
2
  .DS_Store
3
3
  Gemfile.lock
4
- *.gem
4
+ *.gem
5
+ .zoho
@@ -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
- # do we have width and height?
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
- mini_image = MiniMagick::Image.open(image.file_path)
57
- mini_image.auto_orient
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
- mini_image.write file_path
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 resize(mini_image, width, height)
72
- if mini_image[:width] > mini_image[:height]
73
- mini_image.resize "#{width}x#{height}>"
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
- # portrait
76
- mini_image.resize "#{height}x#{width.to_i * 1.25}"
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
- def crop(mini_image, width, height)
81
- max = width > height ? width : height
82
- if mini_image[:width] > mini_image[:height]
83
- resize(mini_image, max * 2, height)
84
- else
85
- resize(mini_image, width, max * 2)
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
- mini_image.crop "#{width}x#{height}+0+0"
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
@@ -6,7 +6,7 @@ require 'logger'
6
6
 
7
7
  module GallerizeCli
8
8
 
9
- VERSION='0.3.2'
9
+ VERSION='0.4.0'
10
10
 
11
11
  class << self
12
12
  def perform
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.3.2
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: 2014-11-12 00:00:00.000000000 Z
11
+ date: 2015-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mini_magick