Skip to content

Commit

Permalink
Warning: breaking backward compatibility.
Browse files Browse the repository at this point in the history
Adding support for padding.
  • Loading branch information
owahab committed Jun 20, 2011
1 parent d878ced commit d51e1f0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
33 changes: 23 additions & 10 deletions lib/paperclip-ffmpeg.rb
Expand Up @@ -23,7 +23,9 @@ def initialize file, options = {}, attachment = nil

@geometry = options[:geometry]
@file = file
@keep_aspect = @geometry.nil? || @geometry[-1,1] != '#'
@keep_aspect = !@geometry.nil? && @geometry[-1,1] != '!'
@enlarge_only = @keep_aspect && @geometry[-1,1] == '<'
@shrink_only = @keep_aspect && @geometry[-1,1] == '>'
@whiny = options[:whiny].nil? ? true : options[:whiny]
@format = options[:format]
@time = options[:time].nil? ? 3 : options[:time]
Expand All @@ -41,30 +43,41 @@ def make

begin
parameters = []
parameters << '-y'
# Add geometry
if @geometry
# Extract target dimensions
if @geometry =~ /(\d*)x(\d*)/
target_width = $1
target_height = $2
end
# Only calculate target dimensions if we have current dimensions
unless @meta[:size].nil?
current_geometry = @meta[:size].split('x')
# Current width and height
current_width = current_geometry[0]
current_height = current_geometry[1]
if @keep_aspect
# Correct size to keep aspect
if current_width.to_i > target_width.to_i
# Scaling down
if current_width.to_i >= target_width.to_i && !@enlarge_only
# Keep aspect ratio
width = target_width.to_i
height = (width.to_f / (@meta[:aspect].to_f)).to_i
else
# TODO: Padding
elsif current_width.to_i < target_width.to_i && !@shrink_only
# Keep aspect ratio
width = target_width.to_i
height = (width.to_f / (@meta[:aspect].to_f)).to_i
# We should add the delta as a padding area
pad_h = (target_height.to_i - current_height.to_i) / 2
pad_w = (target_width.to_i - current_width.to_i) / 2
@convert_options[:vf] = "pad=#{width.to_i}:#{height.to_i}:#{pad_h}:#{pad_w}:black"
end
else
# Do not keep aspect ratio
width = target_width
height = target_height
end
@convert_options[:s] = "#{width.to_i}x#{height.to_i}" unless width.nil? || height.nil?
else
@convert_options[:s] = @geometry
end
unless width.nil? || height.nil? || @convert_options[:vf]
@convert_options[:s] = "#{width.to_i}x#{height.to_i}"
end
end
# Add format
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip-ffmpeg/version.rb
@@ -1,5 +1,5 @@
module Paperclip
module Ffmpeg
VERSION = "0.3.1"
VERSION = "0.6.0"
end
end

0 comments on commit d51e1f0

Please sign in to comment.