Module: Vips::Process::Crop

Defined in:
lib/vips-process/crop.rb

Instance Method Summary collapse

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 | __

Parameters:

  • left (defaults to: 0)

    Number if it’s a Float between 0 and 1 it will use that to create a band in which it will displace the width of it. if it’s an Integer it’s the offset from the left.

  • top (defaults to: 0)

    Number if it’s a Float between 0 and 1 it will use that to create a band in which it will displace the height of it. if it’s an Integer it’s the offset from the top.

  • width (defaults to: nil)

    Integer the width to crop to

  • height (defaults to: nil)

    Integer the height to crop to



67
68
69
70
71
72
# File 'lib/vips-process/crop.rb', line 67

def crop(left: 0, top: 0, width: nil, height: nil)
  manipulate! do |image|
    do_crop image, left, top, width, height
  end
  self
end

#crop!(left: 0, top: 0, width: nil, height: nil) ⇒ Object

Same as #crop but it returns the current image if there was an area issue while cropping instead of raising an exception.



76
77
78
79
80
81
82
83
84
# File 'lib/vips-process/crop.rb', line 76

def crop!(left: 0, top: 0, width: nil, height: nil)
  manipulate! do |image|
    begin
      do_crop image, left, top, width, height
    rescue VIPS::Error => e
      e.message =~ /extract_area/ ? image : raise(e)
    end
  end
end