Crop Images Online – API Tutorial

Crop Images
Image by https://unsplash.com/@rawpixel

 

Image editing is a big part of the image converter options of online-convert.com’s RESTful API.

Next to basic image editing when it comes to color, quality, DPI, and image size, there is something else you can do to your pictures and photos using the API: cropping. For tutorials for the basic image editing, check out this article: Basic Image Editing Via API. The cropping will be handled in this tutorial.

Beforehand, consider checking out how to use our API with Postman, a cost-free tool for API usage. But now, let’s have a look at how to crop images.

You can also crop images online using the online-convert.com webservice.

 

How To Crop Images

The cropping of images is done by adding to the options of an API POST request. If you look at the options, they are rather self-explanatory, showing exactly where the crop will take place. Consider the following example of a code snippet:

"options": {
        "crop_top": 100,
        "crop_bottom": 50,
        "crop_left": 100,
        "crop_right": 150
      }

The options are conveniently named. Thus, it’s easy to determine where part of the image will be removed aka cropped. For example, crop_bottom will crop the lower part of the image, namely the amount you specified in the POST. Crop_left will crop from the left, and so on.

 

It is also possible to specify only one, two, or three of the options above. This means you don’t have to crop from all four sides of your picture. In these cases, use the crop options of the side(s) you want to crop only:

"options": {
        "crop_left": 300
      }

 

The whole POST command to send a complete conversion including the cropping options would look like this:

{
    "input": [{
      "type": "remote",
      "source": "https://cdn.online-convert.com/example-file/raster%20image/png/example.png"
    }],
    "conversion": [{
      "category": "image",
      "target": "jpg",
      "options": {
        "crop_top": 100,
        "crop_bottom": 100,
        "crop_left": 50,
        "crop_right": 50
      }
    }]
}

 

Alternative Cropping Options

As an alternative, you can use different options that are also provided in the API for cropping an image:

"options": {
        "crop_origin_x": 50,
        "crop_origin_y": 150,
        "crop_width": 500,
        "crop_height": 500
      }

This approach is a little less obvious. Consider the following explanation:

  • “crop_origin_x”: specifies a point in the image along the x coordinate, starting from the upmost left corner
  • “crop_origin_y”: specifies a point in the image along the y coordinate, starting from the upmost left corner
  • crop_width“: specifies the width of the cropped out area
  • crop_height“: specifies the height of the cropped out area

 

Note that you can use only one of these two option-sets! A combination of crop_left and crop_origin_y, for example, is not possible!

 


Want to get in touch with us? Hit us up on our social media channels!