How to adaptively adjust the contrast of an image in PHP
2 min readAug 6, 2019
Let’s take a look at how to automatically adjust Gamma to make pictures look more appealing. It’s very simple, just two quick steps:
- Install the Cloudmersive image processing API using the command line of Composer:
composer require cloudmersive/cloudmersive_imagerecognition_api_client
2. Call editContrastAdaptive:
<?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\EditApi(
// 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
);
$gamma = 1.2; // double | Gamma value to adjust the contrast in the image. Recommended value is 2.0. Values between 0.0 and 1.0 will reduce contrast, while values above 1.0 will increase contrast.
$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->editContrastAdaptive($gamma, $image_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EditApi->editContrastAdaptive: ', $e->getMessage(), PHP_EOL;
}
?>
Any image run through this will immediately look better and grab the attention of the view.