Permalink
Browse files

Refactoring enlarging, padding and shrinking modes

  • Loading branch information...
1 parent d51e1f0 commit 9cd554a936825ed0ed992c0402d0f5adc3133789 @owahab committed Jul 3, 2011
Showing with 56 additions and 47 deletions.
  1. +55 −40 lib/paperclip-ffmpeg.rb
  2. +0 −5 lib/paperclip-ffmpeg/version.rb
  3. +1 −2 paperclip-ffmpeg.gemspec
View
@@ -24,6 +24,7 @@ def initialize file, options = {}, attachment = nil
@geometry = options[:geometry]
@file = file
@keep_aspect = !@geometry.nil? && @geometry[-1,1] != '!'
+ @pad_only = @keep_aspect && @geometry[-1,1] == '#'
@enlarge_only = @keep_aspect && @geometry[-1,1] == '<'
@shrink_only = @keep_aspect && @geometry[-1,1] == '>'
@whiny = options[:whiny].nil? ? true : options[:whiny]
@@ -41,58 +42,72 @@ def make
dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
dst.binmode
- begin
- parameters = []
- # 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
- if current_width.to_i >= target_width.to_i && !@enlarge_only
+ parameters = []
+ # 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
+ if @enlarge_only
+ if current_width.to_i < target_width.to_i
# Keep aspect ratio
width = target_width.to_i
height = (width.to_f / (@meta[:aspect].to_f)).to_i
- elsif current_width.to_i < target_width.to_i && !@shrink_only
+ @convert_options[:s] = "#{width.to_i}x#{height.to_i}"
+ else
+ return nil
+ end
+ elsif @shrink_only
+ if current_width.to_i > target_width.to_i
# 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"
+ @convert_options[:s] = "#{width.to_i}x#{height.to_i}"
+ else
+ return nil
end
+ elsif @pad_only
+ # Keep aspect ratio
+ width = target_width.to_i
+ height = (width.to_f / (@meta[:aspect].to_f)).to_i
+ # We should add half the delta as a padding offset Y
+ pad_y = (target_height.to_f - height.to_f).abs.to_i / 2
+ @convert_options[:vf] = "scale=#{width}:-1,pad=#{width.to_i}:#{target_height.to_i}:0:#{pad_y}:black"
else
- # Do not keep aspect ratio
- width = target_width
- height = target_height
+ # Keep aspect ratio
+ width = target_width.to_i
+ height = (width.to_f / (@meta[:aspect].to_f)).to_i
+ @convert_options[:s] = "#{width.to_i}x#{height.to_i}"
end
+ else
+ # Do not keep aspect ratio
+ @convert_options[:s] = "#{target_width.to_i}x#{target_height.to_i}"
end
- unless width.nil? || height.nil? || @convert_options[:vf]
- @convert_options[:s] = "#{width.to_i}x#{height.to_i}"
- end
- end
- # Add format
- case @format
- when 'jpg', 'jpeg', 'png', 'gif' # Images
- @convert_options[:f] = 'image2'
- @convert_options[:ss] = @time
- @convert_options[:vframes] = 1
end
-
- parameters << '-i :source'
- parameters << @convert_options.map { |k,v| "-#{k.to_s} #{v} "}
- parameters << ":dest"
+ end
+ # Add format
+ case @format
+ when 'jpg', 'jpeg', 'png', 'gif' # Images
+ @convert_options[:f] = 'image2'
+ @convert_options[:ss] = @time
+ @convert_options[:vframes] = 1
+ end
+
+ parameters << '-i :source'
+ parameters << @convert_options.map { |k,v| "-#{k.to_s} #{v} "}
+ parameters << ":dest"
- parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
+ parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
+ begin
success = Paperclip.run("ffmpeg", parameters, :source => "#{File.expand_path(src.path)}", :dest => File.expand_path(dst.path))
rescue PaperclipCommandLineError => e
@@ -1,5 +0,0 @@
-module Paperclip
- module Ffmpeg
- VERSION = "0.6.0"
- end
-end
View
@@ -1,10 +1,9 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
-require "paperclip-ffmpeg/version"
Gem::Specification.new do |s|
s.name = "paperclip-ffmpeg"
- s.version = Paperclip::Ffmpeg::VERSION
+ s.version = '0.6.2'
s.platform = Gem::Platform::RUBY
s.authors = ["Omar Abdel-Wahab"]
s.email = ["owahab@gmail.com"]

0 comments on commit 9cd554a

Please sign in to comment.