Generating HTML to PNG in PHP is a common need for creating thumbnails, email previews, and reports. Aspose.HTML Cloud SDK for PHP provides a powerful library that makes server‑side conversion straightforward. In this guide you will learn how to set up the SDK, configure conversion options, run a complete code example, and perform the same task with cURL REST calls for automated workflows.
HTML to PNG Conversion in PHP - Step by Step Guide
- Install the SDK via Composer: Use the official Composer command to add the library to your project.
composer require aspose-html-cloud
- Configure your Aspose Cloud credentials: Create a
Configurationobject and set yourAppSidandAppKey.
$config = new Configuration();
$config->setAppSid('YOUR_APP_SID');
$config->setAppKey('YOUR_APP_KEY');
- Initialize the Convert API: Instantiate
ConvertApiwith the configuration object.
$convertApi = new ConvertApi($config);
- Set conversion options: Define output format, dimensions, quality, and background color using
ConvertOptions.
$options = new ConvertOptions();
$options->setOutputFormat('png');
$options->setWidth(1024);
$options->setHeight(0); // Preserve aspect ratio
$options->setQuality(90);
$options->setBackgroundColor('#FFFFFF');
- Perform the conversion: Call
convertLocalwith the source HTML file and destination PNG path.
try {
$convertApi->convertLocal($inputHtmlPath, $outputPngPath, $options);
echo "Conversion successful: {$outputPngPath}\n";
} catch (Exception $e) {
echo "Conversion failed: " . $e->getMessage() . "\n";
}
For detailed class and method information, refer to the API reference.
HTML to PNG in PHP - Complete Code Example
This example demonstrates how to convert a local HTML file to a PNG image using the Aspose.HTML Cloud SDK for PHP.
<?php
require __DIR__ . '/vendor/autoload.php';
use Aspose\HTML\Cloud\Configuration;
use Aspose\HTML\Cloud\Api\ConvertApi;
use Aspose\HTML\Cloud\Model\ConvertOptions;
// -----------------------------------------------------------------------------
// Configuration – replace with your actual Aspose Cloud credentials
// -----------------------------------------------------------------------------
$config = new Configuration();
$config->setAppSid('YOUR_APP_SID');
$config->setAppKey('YOUR_APP_KEY');
// -----------------------------------------------------------------------------
// Initialize the Convert API
// -----------------------------------------------------------------------------
$convertApi = new ConvertApi($config);
// -----------------------------------------------------------------------------
// Define source HTML and destination PNG paths (generic, adjust as needed)
// -----------------------------------------------------------------------------
$inputHtmlPath = __DIR__ . '/sample.html';
$outputPngPath = __DIR__ . '/sample.png';
// -----------------------------------------------------------------------------
// Set conversion options – adjust width/height, quality, and background color
// -----------------------------------------------------------------------------
$options = new ConvertOptions();
$options->setOutputFormat('png'); // Target format
$options->setWidth(1024); // Desired width (pixels)
$options->setHeight(0); // Height 0 preserves aspect ratio
$options->setQuality(90); // PNG compression level (0‑100)
$options->setBackgroundColor('#FFFFFF'); // Optional background for transparent pages
// -----------------------------------------------------------------------------
// Perform the conversion
// -----------------------------------------------------------------------------
try {
$convertApi->convertLocal($inputHtmlPath, $outputPngPath, $options);
echo "Conversion successful: {$outputPngPath}\n";
} catch (Exception $e) {
echo "Conversion failed: " . $e->getMessage() . "\n";
}
// -----------------------------------------------------------------------------
// No explicit cleanup required – SDK handles temporary resources internally
// -----------------------------------------------------------------------------
?>
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (
sample.html,sample.png, etc.) to match your actual file locations, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Convert HTML to PNG Using cURL and the REST API
The following cURL commands show how to achieve the same HTML to PNG conversion using the Aspose.HTML Cloud REST API.
- Obtain an access token (replace placeholders with your client credentials).
curl -X POST "https://api.aspose.cloud/connect/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
- Upload the source HTML file to the storage.
curl -X PUT "https://api.aspose.cloud/v4.0/html/storage/file/sample.html" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: text/html" \
--data-binary "@sample.html"
- Request the conversion to PNG.
curl -X POST "https://api.aspose.cloud/v4.0/html/convert?format=png&width=1024&quality=90&backgroundColor=%23FFFFFF" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"inputPath":"sample.html","outputPath":"sample.png"}'
- Download the generated PNG file.
curl -X GET "https://api.aspose.cloud/v4.0/html/storage/file/sample.png" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o sample.png
For a full list of parameters and additional options, see the official API documentation.
Prerequisites and Setup for Aspose.HTML Cloud SDK for PHP
Before you begin, ensure you have:
- PHP 7.4 or later installed.
- Composer installed on your development machine.
- An Aspose Cloud account with valid
AppSidandAppKey.
Install the SDK using Composer:
composer require aspose-html-cloud
You can download the latest package from the official release page: Aspose.HTML Cloud SDK for PHP Release.
Configure your credentials as shown in the steps section, then you are ready to start converting HTML to PNG.
Conclusion
Converting HTML to PNG in PHP becomes effortless with the Aspose.HTML Cloud SDK for PHP. The library handles rendering, scaling, and image optimization on the server side, eliminating the need for a browser or external tools. By following the step‑by‑step guide, you can integrate high‑quality PNG generation into any PHP application, whether for thumbnail creation, email rendering, or report generation. Remember to acquire a proper license for production use; pricing details are available on the product page, and a temporary license can be obtained from the temporary license page while you evaluate the library.
FAQs
-
How does HTML to PNG in PHP work without a browser?
The Aspose.HTML Cloud SDK for PHP renders HTML using its own rendering engine on the server, so no browser or headless Chrome is required. -
Can I control the image dimensions and quality?
Yes. Use theConvertOptionsclass to set width, height, quality (0‑100), and background color before calling the Convert API. -
Is it possible to batch convert multiple HTML files to PNG?
Absolutely. Loop through your file list, reuse the sameConvertApiinstance, and adjust the input and output paths for each iteration. -
What licensing is needed for commercial projects?
A paid license is required for production. You can view pricing on the product page and obtain a temporary evaluation license from the temporary license page.