Image Operations / Cropping
With image cropping you can remove unwanted parts of the image or extract a portion of the image with the most interesting bits. The Optidash Image API offers you three modes of image cropping: rect, ratio and face.
Rect
This mode allows you to extract a rectangular portion of the image by providing the width
and height
of the fragment you'd like to extract and optionally a gravity
parameter. The default crop gravity
is center
and you may use one of the following values: top-left
, top
, top-right
, right
, bottom-right
, bottom
, bottom-left
, left
.
{
"crop": {
"mode": "rect",
"width": 375,
"height": 150,
"gravity": "center"
}
}
If you know the exact position of the fragment you'd like to extract, you may ignore the gravity
parameter and specify x
and y
coordinates. The 0,0
point (the origin) is located at the top-left corner of the image.
{
"crop": {
"mode": "rect",
"width": 375,
"height": 150,
"x": 340,
"y": 100
}
}
Ratio
This mode allows you to specify aspect ratio of the resulting image. The Optidash API will automatically calculate width and height based on the provided ratio
parameter.
Along with ratio cropping you may optionally provide a gravity
parameter. The default crop gravity value is center
and you may use one of the following values: top-left
, top
, top-right
, right
, bottom-right
, bottom
, bottom-left
, left
.
In order to use ratio cropping simply set mode
to ratio
within the crop
hash and pass the desired aspec ratio string as a value for the ratio
key:
{
"crop": {
"mode": "ratio",
"ratio": "5:9"
}
}
The aspect ratio of 1:1
will produce square image, which is a great choice for social media and grid layouts:
{
"crop": {
"mode": "ratio",
"ratio": "1:1"
}
}
You can excercise even more control over aspect ratio cropping by providing an extra gravity
parameter. For example the result of crop ratio set to 4:3
and gravity set to left
will look like so:
{
"crop": {
"mode": "ratio",
"ratio": "4:3",
"gravity": "left"
}
}
Face
With the face
mode, you can leverage face detection algorithms and easily extract the portion of the image that contains a face. You may specify a padding
parameter which takes an integer and describes the distance between the face and image bounds. The bigger the padding
, the more space around the face will be visible on the output image.
{
"crop": {
"mode": "face",
"padding": 100
}
}