Commit 8338024a by Jon Yurek

Fixes from Gabe and Mike comments

parent cbde60c1
......@@ -4,6 +4,8 @@ module Paperclip
class Geometry
attr_accessor :height, :width, :modifier, :orientation
EXIF_ROTATED_ORIENTATION_VALUES = [5, 6, 7, 8]
# Gives a Geometry representing the given height and width
def initialize(width = nil, height = nil, modifier = nil)
if width.is_a?(Hash)
......@@ -19,22 +21,21 @@ module Paperclip
end
end
# Uses ImageMagick to determing the dimensions of a file, passed in as either a
# File or path.
# NOTE: (race cond) Do not reassign the 'file' variable inside this method as it is likely to be
# a Tempfile object, which would be eligible for file deletion when no longer referenced.
# Extracts the Geometry from a file (or path to a file)
def self.from_file(file)
GeometryDetectorFactory.new(file).make
end
# Parses a "WxH" formatted string, where W is the width and H is the height.
# Extracts the Geometry from a "WxH,O" string
# Where W is the width, H is the height,
# and O is the EXIF orientation
def self.parse(string)
GeometryParserFactory.new(string).make
end
# Swaps the height and width if necessary
def auto_orient
if [5, 6, 7, 8].include?(@orientation)
if EXIF_ROTATED_ORIENTATION_VALUES.include?(@orientation)
@height, @width = @width, @height
@orientation -= 4
end
......
......@@ -12,10 +12,6 @@ module Paperclip
private
def path
@file.respond_to?(:path) ? @file.path : @file
end
def geometry_string
begin
silence_stream(STDERR) do
......@@ -28,6 +24,10 @@ module Paperclip
end
end
def path
@file.respond_to?(:path) ? @file.path : @file
end
def raise_if_blank_file
if path.blank?
raise Errors::NotIdentifiedByImageMagickError.new("Cannot find the geometry of a file with a blank name")
......
......@@ -19,10 +19,6 @@ module Paperclip
private
def match
@height = nil
@width = nil
@modifier = nil
@orientation = nil
if actual_match = @string && @string.match(FORMAT)
@width = actual_match[1]
@height = actual_match[2]
......
......@@ -71,5 +71,3 @@ class GeometryParserFactoryTest < Test::Unit::TestCase
assert_equal :correct, output
end
end
......@@ -50,26 +50,28 @@ class GeometryTest < Test::Unit::TestCase
end
should "recognize an EXIF orientation and not rotate with auto_orient if not necessary" do
assert @geo = Paperclip::Geometry.new(:width => 1024, :height => 768, :orientation => 1)
assert_equal 1024, @geo.width
assert_equal 768, @geo.height
geo = Paperclip::Geometry.new(:width => 1024, :height => 768, :orientation => 1)
assert geo
assert_equal 1024, geo.width
assert_equal 768, geo.height
@geo.auto_orient
geo.auto_orient
assert_equal 1024, @geo.width
assert_equal 768, @geo.height
assert_equal 1024, geo.width
assert_equal 768, geo.height
end
should "recognize an EXIF orientation and rotate with auto_orient if necessary" do
assert @geo = Paperclip::Geometry.new(:width => 1024, :height => 768, :orientation => 6)
assert_equal 1024, @geo.width
assert_equal 768, @geo.height
geo = Paperclip::Geometry.new(:width => 1024, :height => 768, :orientation => 6)
assert geo
assert_equal 1024, geo.width
assert_equal 768, geo.height
@geo.auto_orient
geo.auto_orient
assert_equal 768, @geo.width
assert_equal 1024, @geo.height
assert_equal 2, @geo.orientation
assert_equal 768, geo.width
assert_equal 1024, geo.height
assert_equal 2, geo.orientation
end
should "treat x and X the same in geometries" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment