How to Crop Images in PHP

Cloudmersive
2 min readMar 2, 2023

--

Cutting the size of an image manually takes time we (and our website visitors) don’t have. Thankfully, using the below APIs in your applications, you can easily crop to rectangular or circular areas on any input image — just copy and paste from ready-to-run code examples (provided below for your convenience), and you’ll be done in a jiff. You can use either API for free — just register a free account on our website and include that in your request.

First things first, let’s install the SDK by running this command:

composer require cloudmersive/cloudmersive_imagerecognition_api_client

Next, let’s call the Rectangular Crop API using the following code examples. When we make calls to this API, we’ll need to define the left edge of our crop area first, followed by the top area. After that, we can define the width and height as well:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\EditApi(


new GuzzleHttp\Client(),
$config
);
$left = 56; // int | The left edge of the rectangular crop area in pixels (X).
$top = 56; // int | The top edge of the rectangular crop area in pixels (Y).
$width = 56; // int | The width of the rectangular crop area in pixels.
$height = 56; // int | The height of the rectangular crop area in pixels.
$image_file = "/path/to/inputfile"; // \SplFileObject | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.

try {
$result = $apiInstance->editCropRectangle($left, $top, $width, $height, $image_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EditApi->editCropRectangle: ', $e->getMessage(), PHP_EOL;
}
?>

Lastly, let’s call the Circular Crop API. We’ll also need to define the left and top edges of our circle at the outset, and after that we can define the radius of our circle to deteremine its size:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');



$apiInstance = new Swagger\Client\Api\EditApi(


new GuzzleHttp\Client(),
$config
);
$left = 56; // int | The left edge of the circular crop area in pixels (X).
$top = 56; // int | The top edge of the circular crop area in pixels (Y).
$radius = 56; // int | The radius of the circular crop area in pixels.
$image_file = "/path/to/inputfile"; // \SplFileObject | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.

try {
$result = $apiInstance->editCropCircle($left, $top, $radius, $image_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EditApi->editCropCircle: ', $e->getMessage(), PHP_EOL;
}
?>

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet