Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

width and height methods for image processing #1405

Merged
merged 3 commits into from
Jul 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/carrierwave/processing/mini_magick.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,43 @@ def resize_and_pad(width, height, background=:transparent, gravity='Center')
end
end

##
# Return the mini magic instance of the image
#
# === Returns
#
# #<MiniMagick::Image>
#
def mini_magic_image
if url
::MiniMagick::Image.open(url)
else
::MiniMagick::Image.open(current_path)
end
end

##
# Gives the width of the image, useful for model validation
#
# === Returns
#
# [Integer] the image's width in pixels
#
def width
mini_magic_image[:width]
end

##
# Gives the height of the image, useful for model validation
#
# === Returns
#
# [Integer] the image's height in pixels
#
def height
mini_magic_image[:height]
end

##
# Manipulate the image with MiniMagick. This method will load up an image
# and then pass each of its frames to the supplied block. It will then
Expand Down
10 changes: 10 additions & 0 deletions spec/processing/mini_magick_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
FileUtils.cp(file_path('landscape.jpg'), file_path('landscape_copy.jpg'))
@instance.stub(:current_path).and_return(file_path('landscape_copy.jpg'))
@instance.stub(:cached?).and_return true
@instance.stub(:url).and_return nil
end

after do
Expand Down Expand Up @@ -160,4 +161,13 @@
end
end
end

describe "return_width_and_height" do
it "should return the width and height of the image" do
@instance.resize_to_fill(200, 300)
@instance.width.should == 200
@instance.height.should == 300
end
end

end