How to Convert Images to PNG Format in PHP

Cloudmersive
1 min readFeb 27, 2023

--

With Image Conversion APIs powering your web applications, you won’t need to worry about manually converting files to common formats like PNG in the future. Using the ready-to-run PHP code examples provided below, you can easily take advantage of a free API which converts dozens of common image formats into PNG format with simple request parameters.

You’ll first need to install the SDK using this command:

composer require cloudmersive/cloudmersive_imagerecognition_api_client

After that, you can refer to the remaining code examples to finish structuring your API call. This stage requires an API key to authenticate your request (you can get one for free by registering a free account on our website):

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\ConvertApi(


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->convertToPng($image_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertApi->convertToPng: ', $e->getMessage(), PHP_EOL;
}
?>

And just like that, you’re already done! Each request will return the encoding for a newly converted PNG file.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet