Skip to content
This repository
Browse code

Refactoring enlarging, padding and shrinking modes

  • Loading branch information...
commit 9cd554a936825ed0ed992c0402d0f5adc3133789 1 parent d51e1f0
Omar Abdel-Wahab authored
95 lib/paperclip-ffmpeg.rb
@@ -24,6 +24,7 @@ def initialize file, options = {}, attachment = nil
24 24 @geometry = options[:geometry]
25 25 @file = file
26 26 @keep_aspect = !@geometry.nil? && @geometry[-1,1] != '!'
  27 + @pad_only = @keep_aspect && @geometry[-1,1] == '#'
27 28 @enlarge_only = @keep_aspect && @geometry[-1,1] == '<'
28 29 @shrink_only = @keep_aspect && @geometry[-1,1] == '>'
29 30 @whiny = options[:whiny].nil? ? true : options[:whiny]
@@ -41,58 +42,72 @@ def make
41 42 dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
42 43 dst.binmode
43 44
44   - begin
45   - parameters = []
46   - # Add geometry
47   - if @geometry
48   - # Extract target dimensions
49   - if @geometry =~ /(\d*)x(\d*)/
50   - target_width = $1
51   - target_height = $2
52   - end
53   - # Only calculate target dimensions if we have current dimensions
54   - unless @meta[:size].nil?
55   - current_geometry = @meta[:size].split('x')
56   - # Current width and height
57   - current_width = current_geometry[0]
58   - current_height = current_geometry[1]
59   - if @keep_aspect
60   - if current_width.to_i >= target_width.to_i && !@enlarge_only
  45 + parameters = []
  46 + # Add geometry
  47 + if @geometry
  48 + # Extract target dimensions
  49 + if @geometry =~ /(\d*)x(\d*)/
  50 + target_width = $1
  51 + target_height = $2
  52 + end
  53 + # Only calculate target dimensions if we have current dimensions
  54 + unless @meta[:size].nil?
  55 + current_geometry = @meta[:size].split('x')
  56 + # Current width and height
  57 + current_width = current_geometry[0]
  58 + current_height = current_geometry[1]
  59 + if @keep_aspect
  60 + if @enlarge_only
  61 + if current_width.to_i < target_width.to_i
61 62 # Keep aspect ratio
62 63 width = target_width.to_i
63 64 height = (width.to_f / (@meta[:aspect].to_f)).to_i
64   - elsif current_width.to_i < target_width.to_i && !@shrink_only
  65 + @convert_options[:s] = "#{width.to_i}x#{height.to_i}"
  66 + else
  67 + return nil
  68 + end
  69 + elsif @shrink_only
  70 + if current_width.to_i > target_width.to_i
65 71 # Keep aspect ratio
66 72 width = target_width.to_i
67 73 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"
  74 + @convert_options[:s] = "#{width.to_i}x#{height.to_i}"
  75 + else
  76 + return nil
72 77 end
  78 + elsif @pad_only
  79 + # Keep aspect ratio
  80 + width = target_width.to_i
  81 + height = (width.to_f / (@meta[:aspect].to_f)).to_i
  82 + # We should add half the delta as a padding offset Y
  83 + pad_y = (target_height.to_f - height.to_f).abs.to_i / 2
  84 + @convert_options[:vf] = "scale=#{width}:-1,pad=#{width.to_i}:#{target_height.to_i}:0:#{pad_y}:black"
73 85 else
74   - # Do not keep aspect ratio
75   - width = target_width
76   - height = target_height
  86 + # Keep aspect ratio
  87 + width = target_width.to_i
  88 + height = (width.to_f / (@meta[:aspect].to_f)).to_i
  89 + @convert_options[:s] = "#{width.to_i}x#{height.to_i}"
77 90 end
  91 + else
  92 + # Do not keep aspect ratio
  93 + @convert_options[:s] = "#{target_width.to_i}x#{target_height.to_i}"
78 94 end
79   - unless width.nil? || height.nil? || @convert_options[:vf]
80   - @convert_options[:s] = "#{width.to_i}x#{height.to_i}"
81   - end
82   - end
83   - # Add format
84   - case @format
85   - when 'jpg', 'jpeg', 'png', 'gif' # Images
86   - @convert_options[:f] = 'image2'
87   - @convert_options[:ss] = @time
88   - @convert_options[:vframes] = 1
89 95 end
90   -
91   - parameters << '-i :source'
92   - parameters << @convert_options.map { |k,v| "-#{k.to_s} #{v} "}
93   - parameters << ":dest"
  96 + end
  97 + # Add format
  98 + case @format
  99 + when 'jpg', 'jpeg', 'png', 'gif' # Images
  100 + @convert_options[:f] = 'image2'
  101 + @convert_options[:ss] = @time
  102 + @convert_options[:vframes] = 1
  103 + end
  104 +
  105 + parameters << '-i :source'
  106 + parameters << @convert_options.map { |k,v| "-#{k.to_s} #{v} "}
  107 + parameters << ":dest"
94 108
95   - parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
  109 + parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
  110 + begin
96 111 success = Paperclip.run("ffmpeg", parameters, :source => "#{File.expand_path(src.path)}", :dest => File.expand_path(dst.path))
97 112
98 113 rescue PaperclipCommandLineError => e
5 lib/paperclip-ffmpeg/version.rb
... ... @@ -1,5 +0,0 @@
1   -module Paperclip
2   - module Ffmpeg
3   - VERSION = "0.6.0"
4   - end
5   -end
3  paperclip-ffmpeg.gemspec
... ... @@ -1,10 +1,9 @@
1 1 # -*- encoding: utf-8 -*-
2 2 $:.push File.expand_path("../lib", __FILE__)
3   -require "paperclip-ffmpeg/version"
4 3
5 4 Gem::Specification.new do |s|
6 5 s.name = "paperclip-ffmpeg"
7   - s.version = Paperclip::Ffmpeg::VERSION
  6 + s.version = '0.6.2'
8 7 s.platform = Gem::Platform::RUBY
9 8 s.authors = ["Omar Abdel-Wahab"]
10 9 s.email = ["owahab@gmail.com"]

0 comments on commit 9cd554a

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