How to adaptively adjust the contrast of an image in PHP

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:

  1. Install the Cloudmersive image processing API using the command line of Composer:
composer require cloudmersive/cloudmersive_imagerecognition_api_client

2. Call editContrastAdaptive:

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(),
$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);
} 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.

Image for post

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store