Skip to content

Commit

Permalink
Added support for the "crop" method to the vips processor.
Browse files Browse the repository at this point in the history
  • Loading branch information
clickworkorange committed Jan 24, 2024
1 parent d27c987 commit 9ccc2e3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/carrierwave/processing/vips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ def resize_to_fill(width, height, gravity='centre')
def resize_and_pad(width, height, background=nil, gravity='centre', alpha=nil)
process :resize_and_pad => [width, height, background, gravity, alpha]
end

def resize_to_fit(width, height)
process :resize_to_fit => [width, height]
end

def crop(left, top, width, height)
process :crop => [left, top, width, height]
end
end

##
Expand Down Expand Up @@ -208,6 +216,31 @@ def resize_and_pad(width, height, background=nil, gravity='centre', alpha=nil, c
end
end

##
# Crop the image to the contents of a box positioned at [left] and [top], with the dimensions given
# by [width] and [height].
#
#
# === Parameters
#
# [left (integer)] left edge of area to extract
# [top (integer)] top edge of area to extract
# [width (Integer)] width of area to extract
# [height (Integer)] height of area to extract
#
# === Yields
#
# [Vips::Image] additional manipulations to perform
#
def crop(left, top, width, height, combine_options: {})
width, height = resolve_dimensions(width, height)

vips! do |builder|
builder.crop(left, top, width, height)
.apply(combine_options)
end
end

##
# Returns the width of the image in pixels.
#
Expand Down
9 changes: 9 additions & 0 deletions spec/processing/vips_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@
end
end

describe '#crop' do
it 'extracts an area defined from the left and top positions, with the given width and height' do
instance.crop(70, 40, 500, 400)

expect(instance).to have_dimensions(500, 400)
expect(::Vips::Image.new_from_file(instance.current_path).get("vips-loader")).to match(/jpeg/)
end
end

describe "#width and #height" do
it "returns the width and height of the image" do
instance.resize_to_fill(200, 300)
Expand Down

0 comments on commit 9ccc2e3

Please sign in to comment.