Module: Vips::Process::Crop
- Defined in:
- lib/vips-process/crop.rb
Instance Method Summary collapse
-
#crop(left: 0, top: 0, width: nil, height: nil) ⇒ Object
Crop an image in the desired dimentions.
Instance Method Details
#crop(left: 0, top: 0, width: nil, height: nil) ⇒ Object
Crop an image in the desired dimentions.
Pretty much all arguments are optional making it very flexible for you to create all sort of croppings.
It’s very powerful when used with resize. E.g.: say you have an image that is 3000x2000 px. ‘image.resize_to_width(300).crop(height: 150, top: 0.5).process!` will first resize it to 300x200 px and then it will crop it using a 150 height mask positioned in the middle of the resized image. It will give you an image of full width but with height starting at 25px and finishing at 175px. Here’s a graphical example:
Given:
i=image cm=crop mask
__ __ | | | | | | | | | | ——– | | | | __
crop height: cm.height, top: 0.0 will result in:
__ | final| | img |
| | | x | __
crop height: cm.height, top: 0.5 will result in:
__ | x |
| final| | img |
| x | __
crop height: cm.height, top: 1.0 will result in: __ | | | x |
| final| | img | __
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/vips-process/crop.rb', line 67 def crop(left: 0, top: 0, width: nil, height: nil) manipulate! do |image| width ||= image.x_size height ||= image.y_size top = top.is_a?(Float) && top.between?(0,1) ? (image.y_size - height) * top : top left = left.is_a?(Float) && left.between?(0,1) ? (image.x_size - width) * left : left image.extract_area left, top, width, height end self end |