lib/carrierwave/processing/vips.rb in carrierwave-3.0.7 vs lib/carrierwave/processing/vips.rb in carrierwave-3.1.0.beta

- old
+ new

@@ -76,10 +76,14 @@ end def resize_and_pad(width, height, background=nil, gravity='centre', alpha=nil) process :resize_and_pad => [width, height, background, gravity, alpha] end + + def crop(left, top, width, height) + process :crop => [left, top, width, height] + end end ## # Changes the image encoding format to the given format # @@ -202,9 +206,36 @@ def resize_and_pad(width, height, background=nil, gravity='centre', alpha=nil, combine_options: {}) width, height = resolve_dimensions(width, height) vips! do |builder| builder.resize_and_pad(width, height, background: background, gravity: gravity, alpha: alpha) + .apply(combine_options) + end + end + + ## + # Crop the image to the contents of a box positioned at [left] and [top], with the dimensions given + # by [width] and [height]. The original image bottom/right edge is preserved if the cropping box falls + # outside the image bounds. + # + # === 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) + width = vips_image.width - left if width + left > vips_image.width + height = vips_image.height - top if height + top > vips_image.height + + vips! do |builder| + builder.crop(left, top, width, height) .apply(combine_options) end end ##