How to Create Composite Images in PHP

Cloudmersive
2 min readMar 3, 2023

--

Composite images have both surreal and practical real-world applications, but their use-case is limited given the difficulty in creating them.

Thankfully, the free-to-use API provided below makes it possible to precisely composite images at scale with minimal code and simple request parameters. The layered Image will automatically stack on top of the base Image, and (optional) integer values can be defined to determine various composite details including the top, bottom, left and right distance between the two. The layered image can also be resized by defining (optional) width and height parameters.

You can use the following command to install the SDK:

composer require cloudmersive/cloudmersive_imagerecognition_api_client

With that out of the way, you can use the remaining code to structure your API call. You’ll need to include an API key to authorize your request (you can get a free-tier API by registering a free account on our website):

<?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
);
$base_image = "/path/to/inputfile"; // \SplFileObject | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
$layered_image = "/path/to/inputfile"; // \SplFileObject | Image to layer on top of the base image.
$top = 56; // int | Optional; Desired distance in pixels from the top of the base image to the top of the layered image.
$bottom = 56; // int | Optional; Desired distance in pixels from the bottom of the base image to the bottom of the layered image.
$left = 56; // int | Optional; Desired distance in pixels from the left side of the base image to the left side of the layered image.
$right = 56; // int | Optional; Desired distance in pixels from the right side of the base image to the right side of the layered image.
$width = 56; // int | Optional; Desired width of the layered image in pixels. Leave height empty or 0 to automatically scale the image proportionally.
$height = 56; // int | Optional; Desired height of the layered image in pixels. Leave width empty or 0 to automatically scale the image proportionally.

try {
$result = $apiInstance->editCompositePrecise($base_image, $layered_image, $top, $bottom, $left, $right, $width, $height);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EditApi->editCompositePrecise: ', $e->getMessage(), PHP_EOL;
}
?>

Now you can composite images with ease!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet