How to Create Handwritten Text in PHP

Cloudmersive
2 min readMay 1, 2024

Writing PHP code to create visual effects like handwriting is a bit complex and challenging.

Thankfully, with the help of a free API, we don’t need to write any of that code ourselves.

Using the ready-to-run PHP code examples provided below, we can easily generate an image of handwritten. We’ll do so using a text input, a target width integer (i.e., how wide we ideally want our handwritten text box to be), a stroke width integer (i.e., how broad our handwritten text strokes should be), and a color definition in our request.

{
"TextInput": "string",
"TargetWidth": 0,
"StrokeWidth": 0,
"Color": "string"
}

To structure our API call, we can start by installing the PHP client via Composer. Let’s execute the following command from our command line:

composer require cloudmersive/cloudmersive_imagerecognition_api_client

After that, we can grab a free Cloudmersive API key to authorize our requests. This will allow us to make up to 800 API calls per month with no commitments (our total will simply reset the following month once we reach our limit).

With our API key ready to go, we can now copy the below code into our file and call the function.

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


new GuzzleHttp\Client(),
$config
);
$request = new \Swagger\Client\Model\CreateHandwritingRequest(); // \Swagger\Client\Model\CreateHandwritingRequest | Draw text parameters

try {
$result = $apiInstance->textGenerationCreateHandwritingPng($request);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling TextGenerationApi->textGenerationCreateHandwritingPng: ', $e->getMessage(), PHP_EOL;
}
?>

Just like that, we’re now able to easily generate images of handwritten text using just a few lines of PHP code.

--

--

Cloudmersive

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