How to Invert Image Colors in PHP
1 min readMar 2, 2023
Inverting (negating) the color in an image gives us a fresh glance at our content, replacing the current scheme with color values on the opposite end of the spectrum. The below API makes it easy to implement color inversion/negation into your applications — just copy and paste from the code examples provided below, supply a free-tier API key, and you’re all done.
You can install the SDK using this command:
composer require cloudmersive/cloudmersive_imagerecognition_api_client
After that, you can call the below function (make sure your API key and image file path are included):
<?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
);
$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->editInvert($image_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EditApi->editInvert: ', $e->getMessage(), PHP_EOL;
}
?>
And that’s it — done already!