Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Adding Attachment#geometry to get file's dimension
Browse files Browse the repository at this point in the history
Fixes #768
  • Loading branch information
sikachu committed Mar 30, 2012
1 parent 7088f5b commit 51bb0f9
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 154 deletions.
5 changes: 5 additions & 0 deletions lib/paperclip/attachment.rb
Expand Up @@ -319,6 +319,11 @@ def instance_read(attr)
instance.send(getter) if responds || attr.to_s == "file_name"
end

# Returns a geometry object which you can get the image's dimension.
def geometry(style = :original)
Geometry.from_file(path(style))
end

private

def path_option
Expand Down
5 changes: 5 additions & 0 deletions lib/paperclip/geometry.rb
Expand Up @@ -11,6 +11,11 @@ def initialize width = nil, height = nil, modifier = nil
@modifier = modifier
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
# File or path.
# NOTE: (race cond) Do not reassign the 'file' variable inside this method as it is likely to be
Expand Down
15 changes: 15 additions & 0 deletions test/attachment_test.rb
Expand Up @@ -1158,4 +1158,19 @@ def do_after_all; 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

0 comments on commit 51bb0f9

Please sign in to comment.