Commit 51bb0f9f by Prem Sichanugrist

Adding Attachment#geometry to get file's dimension

Fixes #768
parent 7088f5b9
...@@ -319,6 +319,11 @@ module Paperclip ...@@ -319,6 +319,11 @@ module Paperclip
instance.send(getter) if responds || attr.to_s == "file_name" instance.send(getter) if responds || attr.to_s == "file_name"
end end
# Returns a geometry object which you can get the image's dimension.
def geometry(style = :original)
Geometry.from_file(path(style))
end
private private
def path_option def path_option
......
...@@ -11,6 +11,11 @@ module Paperclip ...@@ -11,6 +11,11 @@ module Paperclip
@modifier = modifier @modifier = modifier
end end
# Returns the equality based on height, width, and modifier
def ==(other)
height == other.height && width == other.width && modifier == other.modifier
end
# Uses ImageMagick to determing the dimensions of a file, passed in as either a # Uses ImageMagick to determing the dimensions of a file, passed in as either a
# File or path. # File or path.
# NOTE: (race cond) Do not reassign the 'file' variable inside this method as it is likely to be # NOTE: (race cond) Do not reassign the 'file' variable inside this method as it is likely to be
......
...@@ -1158,4 +1158,19 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -1158,4 +1158,19 @@ class AttachmentTest < Test::Unit::TestCase
end end
end end
context "attachment's geometry" do
setup do
rebuild_model
@dummy = Dummy.new
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
@dummy.avatar = @file
@dummy.save!
@attachment = @dummy.avatar
@geometry = Paperclip::Geometry.from_file(@file.path)
end
should "return a correct geometry object" do
assert_equal @geometry, @attachment.geometry
end
end
end end
require './test/helper' require './test/helper'
class GeometryTest < Test::Unit::TestCase class GeometryTest < Test::Unit::TestCase
context "Paperclip::Geometry" do context "equality check" do
should "correctly report its given dimensions" do should "return true when width, height, modifier are all the same" do
assert @geo = Paperclip::Geometry.new(1024, 768) assert_equal Paperclip::Geometry.new(100, 100, "foo"),
assert_equal 1024, @geo.width Paperclip::Geometry.new(100, 100, "foo")
assert_equal 768, @geo.height
end end
should "set height to 0 if height dimension is missing" do should "return false if width, height, modifier are not the same" do
assert @geo = Paperclip::Geometry.new(1024) assert_not_equal Paperclip::Geometry.new(100, 100, "foo"),
assert_equal 1024, @geo.width Paperclip::Geometry.new(100, 200, "foo")
assert_equal 0, @geo.height assert_not_equal Paperclip::Geometry.new(100, 100, "foo"),
Paperclip::Geometry.new(200, 100, "foo")
assert_not_equal Paperclip::Geometry.new(100, 100, "foo"),
Paperclip::Geometry.new(100, 100, "bar")
end end
end
should "set width to 0 if width dimension is missing" do should "correctly report its given dimensions" do
assert @geo = Paperclip::Geometry.new(nil, 768) assert @geo = Paperclip::Geometry.new(1024, 768)
assert_equal 0, @geo.width assert_equal 1024, @geo.width
assert_equal 768, @geo.height assert_equal 768, @geo.height
end end
should "be generated from a WxH-formatted string" do should "set height to 0 if height dimension is missing" do
assert @geo = Paperclip::Geometry.parse("800x600") assert @geo = Paperclip::Geometry.new(1024)
assert_equal 800, @geo.width assert_equal 1024, @geo.width
assert_equal 600, @geo.height assert_equal 0, @geo.height
end end
should "be generated from a xH-formatted string" do should "set width to 0 if width dimension is missing" do
assert @geo = Paperclip::Geometry.parse("x600") assert @geo = Paperclip::Geometry.new(nil, 768)
assert_equal 0, @geo.width assert_equal 0, @geo.width
assert_equal 600, @geo.height assert_equal 768, @geo.height
end end
should "be generated from a Wx-formatted string" do should "be generated from a WxH-formatted string" do
assert @geo = Paperclip::Geometry.parse("800x") assert @geo = Paperclip::Geometry.parse("800x600")
assert_equal 800, @geo.width assert_equal 800, @geo.width
assert_equal 0, @geo.height assert_equal 600, @geo.height
end end
should "be generated from a W-formatted string" do should "be generated from a xH-formatted string" do
assert @geo = Paperclip::Geometry.parse("800") assert @geo = Paperclip::Geometry.parse("x600")
assert_equal 800, @geo.width assert_equal 0, @geo.width
assert_equal 0, @geo.height assert_equal 600, @geo.height
end end
should "ensure the modifier is nil if not present" do should "be generated from a Wx-formatted string" do
assert @geo = Paperclip::Geometry.parse("123x456") assert @geo = Paperclip::Geometry.parse("800x")
assert_nil @geo.modifier assert_equal 800, @geo.width
end assert_equal 0, @geo.height
end
should "treat x and X the same in geometries" do should "be generated from a W-formatted string" do
@lower = Paperclip::Geometry.parse("123x456") assert @geo = Paperclip::Geometry.parse("800")
@upper = Paperclip::Geometry.parse("123X456") assert_equal 800, @geo.width
assert_equal 123, @lower.width assert_equal 0, @geo.height
assert_equal 123, @upper.width end
assert_equal 456, @lower.height
assert_equal 456, @upper.height
end
['>', '<', '#', '@', '%', '^', '!', nil].each do |mod| should "ensure the modifier is nil if not present" do
should "ensure the modifier #{mod.inspect} is preserved" do assert @geo = Paperclip::Geometry.parse("123x456")
assert @geo = Paperclip::Geometry.parse("123x456#{mod}") assert_nil @geo.modifier
assert_equal mod, @geo.modifier end
assert_equal "123x456#{mod}", @geo.to_s
end
end
['>', '<', '#', '@', '%', '^', '!', nil].each do |mod| should "treat x and X the same in geometries" do
should "ensure the modifier #{mod.inspect} is preserved with no height" do @lower = Paperclip::Geometry.parse("123x456")
assert @geo = Paperclip::Geometry.parse("123x#{mod}") @upper = Paperclip::Geometry.parse("123X456")
assert_equal mod, @geo.modifier assert_equal 123, @lower.width
assert_equal "123#{mod}", @geo.to_s assert_equal 123, @upper.width
end assert_equal 456, @lower.height
end assert_equal 456, @upper.height
end
should "make sure the modifier gets passed during transformation_to" do ['>', '<', '#', '@', '%', '^', '!', nil].each do |mod|
assert @src = Paperclip::Geometry.parse("123x456") should "ensure the modifier #{mod.inspect} is preserved" do
assert @dst = Paperclip::Geometry.parse("123x456>") assert @geo = Paperclip::Geometry.parse("123x456#{mod}")
assert_equal ["123x456>", nil], @src.transformation_to(@dst) assert_equal mod, @geo.modifier
assert_equal "123x456#{mod}", @geo.to_s
end end
end
should "generate correct ImageMagick formatting string for W-formatted string" do ['>', '<', '#', '@', '%', '^', '!', nil].each do |mod|
assert @geo = Paperclip::Geometry.parse("800") should "ensure the modifier #{mod.inspect} is preserved with no height" do
assert_equal "800", @geo.to_s assert @geo = Paperclip::Geometry.parse("123x#{mod}")
assert_equal mod, @geo.modifier
assert_equal "123#{mod}", @geo.to_s
end end
end
should "generate correct ImageMagick formatting string for Wx-formatted string" do should "make sure the modifier gets passed during transformation_to" do
assert @geo = Paperclip::Geometry.parse("800x") assert @src = Paperclip::Geometry.parse("123x456")
assert_equal "800", @geo.to_s assert @dst = Paperclip::Geometry.parse("123x456>")
end assert_equal ["123x456>", nil], @src.transformation_to(@dst)
end
should "generate correct ImageMagick formatting string for xH-formatted string" do should "generate correct ImageMagick formatting string for W-formatted string" do
assert @geo = Paperclip::Geometry.parse("x600") assert @geo = Paperclip::Geometry.parse("800")
assert_equal "x600", @geo.to_s assert_equal "800", @geo.to_s
end end
should "generate correct ImageMagick formatting string for WxH-formatted string" do should "generate correct ImageMagick formatting string for Wx-formatted string" do
assert @geo = Paperclip::Geometry.parse("800x600") assert @geo = Paperclip::Geometry.parse("800x")
assert_equal "800x600", @geo.to_s assert_equal "800", @geo.to_s
end end
should "be generated from a file" do should "generate correct ImageMagick formatting string for xH-formatted string" do
file = File.join(File.dirname(__FILE__), "fixtures", "5k.png") assert @geo = Paperclip::Geometry.parse("x600")
file = File.new(file, 'rb') assert_equal "x600", @geo.to_s
assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) } end
assert @geo.height > 0
assert @geo.width > 0
end
should "be generated from a file path" do should "generate correct ImageMagick formatting string for WxH-formatted string" do
file = File.join(File.dirname(__FILE__), "fixtures", "5k.png") assert @geo = Paperclip::Geometry.parse("800x600")
assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) } assert_equal "800x600", @geo.to_s
assert @geo.height > 0 end
assert @geo.width > 0
end
should "not generate from a bad file" do should "be generated from a file" do
file = "/home/This File Does Not Exist.omg" file = File.join(File.dirname(__FILE__), "fixtures", "5k.png")
assert_raise(Paperclip::Errors::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) } file = File.new(file, 'rb')
end assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) }
assert @geo.height > 0
assert @geo.width > 0
end
should "not generate from a blank filename" do should "be generated from a file path" do
file = "" file = File.join(File.dirname(__FILE__), "fixtures", "5k.png")
assert_raise(Paperclip::Errors::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) } assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) }
end assert @geo.height > 0
assert @geo.width > 0
end
should "not generate from a nil file" do should "not generate from a bad file" do
file = nil file = "/home/This File Does Not Exist.omg"
assert_raise(Paperclip::Errors::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) } assert_raise(Paperclip::Errors::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) }
end end
should "not generate from a file with no path" do should "not generate from a blank filename" do
file = mock("file", :path => "") file = ""
file.stubs(:respond_to?).with(:path).returns(true) assert_raise(Paperclip::Errors::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) }
assert_raise(Paperclip::Errors::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) } end
end
should "let us know when a command isn't found versus a processing error" do should "not generate from a nil file" do
old_path = ENV['PATH'] file = nil
begin assert_raise(Paperclip::Errors::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) }
end
should "not generate from a file with no path" do
file = mock("file", :path => "")
file.stubs(:respond_to?).with(:path).returns(true)
assert_raise(Paperclip::Errors::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) }
end
should "let us know when a command isn't found versus a processing error" do
old_path = ENV['PATH']
begin
silence_stream(STDERR) do
ENV['PATH'] = '' ENV['PATH'] = ''
assert_raises(Paperclip::Errors::CommandNotFoundError) do assert_raises(Paperclip::Errors::CommandNotFoundError) do
file = File.join(File.dirname(__FILE__), "fixtures", "5k.png") file = File.join(File.dirname(__FILE__), "fixtures", "5k.png")
@geo = Paperclip::Geometry.from_file(file) @geo = Paperclip::Geometry.from_file(file)
end end
ensure
ENV['PATH'] = old_path
end end
ensure
ENV['PATH'] = old_path
end end
end
[['vertical', 900, 1440, true, false, false, 1440, 900, 0.625], [['vertical', 900, 1440, true, false, false, 1440, 900, 0.625],
['horizontal', 1024, 768, false, true, false, 1024, 768, 1.3333], ['horizontal', 1024, 768, false, true, false, 1024, 768, 1.3333],
['square', 100, 100, false, false, true, 100, 100, 1]].each do |args| ['square', 100, 100, false, false, true, 100, 100, 1]].each do |args|
context "performing calculations on a #{args[0]} viewport" do context "performing calculations on a #{args[0]} viewport" do
setup do setup do
@geo = Paperclip::Geometry.new(args[1], args[2]) @geo = Paperclip::Geometry.new(args[1], args[2])
end end
should "#{args[3] ? "" : "not"} be vertical" do should "#{args[3] ? "" : "not"} be vertical" do
assert_equal args[3], @geo.vertical? assert_equal args[3], @geo.vertical?
end end
should "#{args[4] ? "" : "not"} be horizontal" do should "#{args[4] ? "" : "not"} be horizontal" do
assert_equal args[4], @geo.horizontal? assert_equal args[4], @geo.horizontal?
end end
should "#{args[5] ? "" : "not"} be square" do should "#{args[5] ? "" : "not"} be square" do
assert_equal args[5], @geo.square? assert_equal args[5], @geo.square?
end end
should "report that #{args[6]} is the larger dimension" do should "report that #{args[6]} is the larger dimension" do
assert_equal args[6], @geo.larger assert_equal args[6], @geo.larger
end end
should "report that #{args[7]} is the smaller dimension" do should "report that #{args[7]} is the smaller dimension" do
assert_equal args[7], @geo.smaller assert_equal args[7], @geo.smaller
end end
should "have an aspect ratio of #{args[8]}" do should "have an aspect ratio of #{args[8]}" do
assert_in_delta args[8], @geo.aspect, 0.0001 assert_in_delta args[8], @geo.aspect, 0.0001
end
end end
end end
end
[[ [1000, 100], [64, 64], "x64", "64x64+288+0" ], [[ [1000, 100], [64, 64], "x64", "64x64+288+0" ],
[ [100, 1000], [50, 950], "x950", "50x950+22+0" ], [ [100, 1000], [50, 950], "x950", "50x950+22+0" ],
[ [100, 1000], [50, 25], "50x", "50x25+0+237" ]]. each do |args| [ [100, 1000], [50, 25], "50x", "50x25+0+237" ]]. each do |args|
context "of #{args[0].inspect} and given a Geometry #{args[1].inspect} and sent transform_to" do context "of #{args[0].inspect} and given a Geometry #{args[1].inspect} and sent transform_to" do
setup do setup do
@geo = Paperclip::Geometry.new(*args[0]) @geo = Paperclip::Geometry.new(*args[0])
@dst = Paperclip::Geometry.new(*args[1]) @dst = Paperclip::Geometry.new(*args[1])
@scale, @crop = @geo.transformation_to @dst, true @scale, @crop = @geo.transformation_to @dst, true
end end
should "be able to return the correct scaling transformation geometry #{args[2]}" do should "be able to return the correct scaling transformation geometry #{args[2]}" do
assert_equal args[2], @scale assert_equal args[2], @scale
end end
should "be able to return the correct crop transformation geometry #{args[3]}" do should "be able to return the correct crop transformation geometry #{args[3]}" do
assert_equal args[3], @crop assert_equal args[3], @crop
end
end end
end end
end end
......
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