How to Detect, Highlight Edges in an Image using PHP

Cloudmersive
2 min readMar 8, 2023

--

Edge detection is an important step towards feature extraction, but implementing the logic is no small task. Thankfully, the below API will take care of that for you, automatically detecting and highlighting the edges within a certain radius of the center of an image (depending on custom input for each request). You can use the below ready-to-run code examples to make your request in PHP or visit our API console page to find code examples available in a variety of other common programming languages.

To install the PHP client, use Composer by executing this command from the command line:

composer require cloudmersive/cloudmersive_imagerecognition_api_client

After that, use the below code to structure your request. To authenticate your API call, include a free-tier API key in the $config line (you can get one by registering a free account on our website):

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

// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Apikey', 'Bearer');

$apiInstance = new Swagger\Client\Api\FilterApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client(),
$config
);
$radius = 56; // int | Radius in pixels of the edge detection operation; a larger radius will produce a greater effect
$image_file = "/path/to/file.txt"; // \SplFileObject | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.

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

After that, you’re all done — no more code required. Easy!

--

--

Cloudmersive

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