How to Merge PNG Files in PHP

Cloudmersive
2 min readMay 8, 2024

--

We can easily merge PNG files in a PHP file upload form using just a few lines of code.

In this case, we’ll use a free API to merge our PNG files. It’ll simplify the operation significantly, and it’ll ensure we don’t have to waste precious time maintaining our code in the future.

We can merge up to 10 PNG files at a time in each API call, and we can authorize our API calls for free with a free Cloudmersive API key (this allows up to 800 API calls per month with no additional commitments).

First, we can install the PHP client with Composer using the below command:

composer require cloudmersive/cloudmersive_document_convert_api_client

Then we can call the PNG merging function using the below code examples:

<?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\MergeDocumentApi(


new GuzzleHttp\Client(),
$config
);
$input_file1 = "/path/to/inputfile"; // \SplFileObject | First input file to perform the operation on.
$input_file2 = "/path/to/inputfile"; // \SplFileObject | Second input file to perform the operation on.
$input_file3 = "/path/to/inputfile"; // \SplFileObject | Third input file to perform the operation on.
$input_file4 = "/path/to/inputfile"; // \SplFileObject | Fourth input file to perform the operation on.
$input_file5 = "/path/to/inputfile"; // \SplFileObject | Fifth input file to perform the operation on.
$input_file6 = "/path/to/inputfile"; // \SplFileObject | Sixth input file to perform the operation on.
$input_file7 = "/path/to/inputfile"; // \SplFileObject | Seventh input file to perform the operation on.
$input_file8 = "/path/to/inputfile"; // \SplFileObject | Eighth input file to perform the operation on.
$input_file9 = "/path/to/inputfile"; // \SplFileObject | Ninth input file to perform the operation on.
$input_file10 = "/path/to/inputfile"; // \SplFileObject | Tenth input file to perform the operation on.

try {
$result = $apiInstance->mergeDocumentPngMulti($input_file1, $input_file2, $input_file3, $input_file4, $input_file5, $input_file6, $input_file7, $input_file8, $input_file9, $input_file10);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling MergeDocumentApi->mergeDocumentPngMulti: ', $e->getMessage(), PHP_EOL;
}
?>

We can paste our API key in the $config snippet at the top, and we can include our PNG file paths in each of the $input_file request variables.

That’s all there is to it — no more code required!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet