Skip to content
This repository
Browse code

Warning: breaking backward compatibility.

Adding support for padding.
  • Loading branch information...
commit d51e1f099d2fcb817e66547df4b9a59bc6ac5e39 1 parent d878ced
Omar Abdel-Wahab authored

Showing 2 changed files with 24 additions and 11 deletions. Show diff stats Hide diff stats

  1. +23 10 lib/paperclip-ffmpeg.rb
  2. +1 1  lib/paperclip-ffmpeg/version.rb
33 lib/paperclip-ffmpeg.rb
@@ -23,7 +23,9 @@ def initialize file, options = {}, attachment = nil
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 @@ def make
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
2  lib/paperclip-ffmpeg/version.rb
... ... @@ -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

0 comments on commit d51e1f0

Please sign in to comment.
Something went wrong with that request. Please try again.