cropBy method

void cropBy(
  1. int width,
  2. int height,
  3. {String region,
  4. String offset}
)

The crop function allows you to remove pixels from an image. You can crop an image by specifying the height and width in pixels or percentage value, or defining height and width in aspect ratio. You can also define a sub region (i.e., define the starting point for crop) before cropping the image, or you can offset the image on its X and Y axis (i.e., define the centre point of the crop) before cropping the image. You can set the X-axis and Y-axis position of the top left corner of the crop by using the query ?crop={width_value},{height_value},x{value},y{value}. This lets you define the starting point of the crop region. The x-axis value and y-axis value can be defined in pixels or percentage.

An example of this would be ?crop=300,400,x150,y75 or ?crop=300,400,x0.50,y0.60. Compulsory parameters width and height Optional parameters: region Optional parameters: offset For more details, Read documentation: For more details read the doc: https://www.contentstack.com/docs/developers/apis/image-delivery-api/#crop-images

Example: With Aspect Ratio:

final stack = contentstack.Stack(apiKey, deliveryToken, environment);
final imageTransformation = stack.imageTransform(imageUrl);
final response = await imageTransformation.
                  cropBy(150, 100, cropRatio: '1:3').fetch();
log.fine(response);

Example: Without aspect Ratio:

final stack = contentstack.Stack(apiKey, deliveryToken, environment);
final imageTransformation = stack.imageTransform(imageUrl);
final response = await imageTransformation.cropBy(150, 100).fetch();
log.fine(response);

Implementation

/// You can set the X-axis and Y-axis position of the
/// top left corner of the crop by
/// using the query ?crop={width_value},{height_value},x{value},y{value}.
/// This lets you define the starting point of the crop region.
/// The x-axis value and y-axis value can be defined in pixels or percentage.
///
/// An `example` of this would be
/// ?crop=300,400,x150,y75 or ?crop=300,400,x0.50,y0.60.
/// Compulsory parameters [width] and [height]
/// Optional parameters: [region]
/// Optional parameters: [offset]
/// For more details, Read documentation:
/// For more details read the doc: https://www.contentstack.com/docs/developers/apis/image-delivery-api/#crop-images
///
/// Example: With Aspect Ratio:
///
/// ```dart
/// final stack = contentstack.Stack(apiKey, deliveryToken, environment);
/// final imageTransformation = stack.imageTransform(imageUrl);
/// final response = await imageTransformation.
///                   cropBy(150, 100, cropRatio: '1:3').fetch();
/// log.fine(response);
/// ```
///
/// Example: Without aspect Ratio:
///
/// ```dart
/// final stack = contentstack.Stack(apiKey, deliveryToken, environment);
/// final imageTransformation = stack.imageTransform(imageUrl);
/// final response = await imageTransformation.cropBy(150, 100).fetch();
/// log.fine(response);
/// ```
void cropBy(int width, int height, {String region, String offset}) {
  /// checks if cropRatio is not null then takes height, width and
  /// cropRatio as prams else it takes crop params and comas
  /// separated width & height
  final cropLRBL = [];
  if (width != null) {
    cropLRBL.add(width);
  }
  if (height != null) {
    cropLRBL.add(height);
  }
  if (region != null) {
    cropLRBL.add(region);
  }
  if (offset != null) {
    cropLRBL.add(offset);
  }
  final commaSeparated = cropLRBL.join(', ');
  query.append('crop', commaSeparated);
}