paperclip-ffmpeg 0.3.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module Paperclip
2
2
  module Ffmpeg
3
- VERSION = "0.3.1"
3
+ VERSION = "0.6.0"
4
4
  end
5
5
  end
@@ -23,7 +23,9 @@ module Paperclip
23
23
 
24
24
  @geometry = options[:geometry]
25
25
  @file = file
26
- @keep_aspect = @geometry.nil? || @geometry[-1,1] != '#'
26
+ @keep_aspect = !@geometry.nil? && @geometry[-1,1] != '!'
27
+ @enlarge_only = @keep_aspect && @geometry[-1,1] == '<'
28
+ @shrink_only = @keep_aspect && @geometry[-1,1] == '>'
27
29
  @whiny = options[:whiny].nil? ? true : options[:whiny]
28
30
  @format = options[:format]
29
31
  @time = options[:time].nil? ? 3 : options[:time]
@@ -41,30 +43,41 @@ module Paperclip
41
43
 
42
44
  begin
43
45
  parameters = []
44
- parameters << '-y'
45
46
  # Add geometry
46
47
  if @geometry
48
+ # Extract target dimensions
47
49
  if @geometry =~ /(\d*)x(\d*)/
48
50
  target_width = $1
49
51
  target_height = $2
50
52
  end
53
+ # Only calculate target dimensions if we have current dimensions
51
54
  unless @meta[:size].nil?
52
55
  current_geometry = @meta[:size].split('x')
56
+ # Current width and height
53
57
  current_width = current_geometry[0]
54
58
  current_height = current_geometry[1]
55
59
  if @keep_aspect
56
- # Correct size to keep aspect
57
- if current_width.to_i > target_width.to_i
58
- # Scaling down
60
+ if current_width.to_i >= target_width.to_i && !@enlarge_only
61
+ # Keep aspect ratio
59
62
  width = target_width.to_i
60
63
  height = (width.to_f / (@meta[:aspect].to_f)).to_i
61
- else
62
- # TODO: Padding
64
+ elsif current_width.to_i < target_width.to_i && !@shrink_only
65
+ # Keep aspect ratio
66
+ width = target_width.to_i
67
+ height = (width.to_f / (@meta[:aspect].to_f)).to_i
68
+ # We should add the delta as a padding area
69
+ pad_h = (target_height.to_i - current_height.to_i) / 2
70
+ pad_w = (target_width.to_i - current_width.to_i) / 2
71
+ @convert_options[:vf] = "pad=#{width.to_i}:#{height.to_i}:#{pad_h}:#{pad_w}:black"
63
72
  end
73
+ else
74
+ # Do not keep aspect ratio
75
+ width = target_width
76
+ height = target_height
64
77
  end
65
- @convert_options[:s] = "#{width.to_i}x#{height.to_i}" unless width.nil? || height.nil?
66
- else
67
- @convert_options[:s] = @geometry
78
+ end
79
+ unless width.nil? || height.nil? || @convert_options[:vf]
80
+ @convert_options[:s] = "#{width.to_i}x#{height.to_i}"
68
81
  end
69
82
  end
70
83
  # Add format
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: paperclip-ffmpeg
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.1
5
+ version: 0.6.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Omar Abdel-Wahab