Converting Word documents to image previews is a frequent need when building web portals or reporting tools. Aspose.OMR Cloud SDK for PHP empowers PHP developers to programmatically convert DOCX to JPG in PHP with high fidelity. In this tutorial you will see a complete PHP example, the equivalent cURL REST workflow, and everything required to get the environment ready. By the end you’ll have a reusable solution that can be integrated into any PHP application.
Full Working Example for Convert DOCX to JPG in PHP Using Aspose.OMR Cloud SDK
This example demonstrates how to use the Aspose.OMR Cloud SDK for PHP to convert a DOCX file into a JPG image.
<?php
require __DIR__ . '/vendor/autoload.php';
use Aspose\OMR\Configuration;
use Aspose\OMR\Api\ConvertApi;
use Aspose\OMR\Model\ConvertDocumentRequest;
use Aspose\OMR\ApiException;
// -----------------------------------------------------------------------------
// Configuration – replace with your actual credentials
// -----------------------------------------------------------------------------
$config = new Configuration();
$config->setAppSid('YOUR_APP_SID');
$config->setApiKey('YOUR_API_KEY');
// -----------------------------------------------------------------------------
// Initialize the Convert API
// -----------------------------------------------------------------------------
$convertApi = new ConvertApi($config);
// -----------------------------------------------------------------------------
// File paths (adjust as needed)
// -----------------------------------------------------------------------------
$inputDocxPath = __DIR__ . '/input.docx';
$outputJpgPath = __DIR__ . '/output.jpg';
// -----------------------------------------------------------------------------
// Prepare request – stream the input file to avoid loading whole file into memory
// -----------------------------------------------------------------------------
$inputStream = fopen($inputDocxPath, 'rb');
if ($inputStream === false) {
throw new RuntimeException("Unable to open input file: $inputDocxPath");
}
$request = new ConvertDocumentRequest();
$request->setFile($inputStream); // Input DOCX stream
$request->setOutputFormat('jpg'); // Desired output format
$request->setOutputFileName('output.jpg'); // Optional: name for the generated file
try {
// -------------------------------------------------------------------------
// Perform conversion
// -------------------------------------------------------------------------
$responseStream = $convertApi->convertDocument($request);
// -------------------------------------------------------------------------
// Write the resulting JPG to disk using a buffered copy (efficient for large files)
// -------------------------------------------------------------------------
$outputHandle = fopen($outputJpgPath, 'wb');
if ($outputHandle === false) {
throw new RuntimeException("Unable to open output file: $outputJpgPath");
}
while (!feof($responseStream)) {
$buffer = fread($responseStream, 8192);
if ($buffer === false) {
throw new RuntimeException('Error reading from response stream.');
}
fwrite($outputHandle, $buffer);
}
fclose($outputHandle);
fclose($inputStream);
fclose($responseStream);
echo "Conversion successful. JPG saved to: $outputJpgPath\n";
} catch (ApiException $e) {
// -------------------------------------------------------------------------
// Handle API errors (e.g., authentication, unsupported format)
// -------------------------------------------------------------------------
fclose($inputStream);
echo 'API Exception: ', $e->getMessage(), PHP_EOL;
exit(1);
} catch (Exception $e) {
// -------------------------------------------------------------------------
// General error handling
// -------------------------------------------------------------------------
if (is_resource($inputStream)) {
fclose($inputStream);
}
echo 'Error: ', $e->getMessage(), PHP_EOL;
exit(1);
}
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, 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.
DOCX to JPG Conversion via REST API Using cURL
If you prefer a pure REST approach, the same conversion can be performed with a few cURL commands.
- Obtain an access token - replace the 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 DOCX file - use the token from the previous step.
curl -X PUT "https://api.aspose.cloud/v4.0/omr/storage/file/input.docx" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document" \
--data-binary "@input.docx"
- Request conversion to JPG.
curl -X POST "https://api.aspose.cloud/v4.0/omr/convert" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"FileName": "input.docx",
"OutputFormat": "jpg",
"OutputFileName": "output.jpg"
}' -o response.json
- Download the generated JPG.
curl -X GET "https://api.aspose.cloud/v4.0/omr/storage/file/output.jpg" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o output.jpg
For a complete reference, see the official API documentation.
Breaking Down Convert DOCX to JPG in PHP with Aspose.OMR Cloud SDK
The following numbered breakdown explains each part of the PHP code that performs the convert DOCX to JPG in PHP workflow.
- Configuration Setup - The
Configurationclass holds yourAppSidandApiKey.
$config = new Configuration();
$config->setAppSid('YOUR_APP_SID');
$config->setApiKey('YOUR_API_KEY');
- API Initialization - An instance of
ConvertApiis created with the configuration.
$convertApi = new ConvertApi($config);
- Preparing the Input Stream - The DOCX file is opened as a binary stream to avoid loading the whole file into memory.
$inputStream = fopen($inputDocxPath, 'rb');
- Building the Request -
ConvertDocumentRequestspecifies the input stream, the desired output format (jpg), and an optional output file name.
$request = new ConvertDocumentRequest();
$request->setFile($inputStream);
$request->setOutputFormat('jpg');
$request->setOutputFileName('output.jpg');
- Executing the Conversion and Saving the Result -
convertDocumentreturns a response stream that is written tooutput.jpgusing a buffered copy.
$responseStream = $convertApi->convertDocument($request);
$outputHandle = fopen($outputJpgPath, 'wb');
while (!feof($responseStream)) {
$buffer = fread($responseStream, 8192);
fwrite($outputHandle, $buffer);
}
For more details on the ConvertApi class, refer to the API reference.
Getting the Environment Ready for Aspose.OMR Cloud SDK in PHP
- Install the SDK via Composer
composer require aspose/aspose-omr-cloud
The package can also be downloaded from the release page.
-
Verify PHP version - The SDK requires PHP 7.2 or later.
-
Set up credentials - Replace
YOUR_APP_SIDandYOUR_API_KEYin the code with the values from your Aspose Cloud account. -
Autoload dependencies - The
vendor/autoload.phpfile generated by Composer loads all required classes.
With these steps completed, you are ready to convert DOCX to JPG in PHP using the Aspose.OMR Cloud SDK.
Conclusion
You now know how to convert DOCX to JPG in PHP efficiently with the Aspose.OMR Cloud SDK. The provided code sample, REST cURL workflow, and setup instructions give you a complete, production‑ready solution that can be integrated into any PHP project, including Laravel applications. For commercial use, purchase a license on the Aspose.OMR Cloud SDK for PHP product page; a temporary license is also available via the temporary license page. Start converting your Word documents to high‑quality JPG images today.
FAQs
How can I convert DOCX to JPG in PHP?
Use the Aspose.OMR Cloud SDK for PHP. After installing the SDK and configuring your credentials, the PHP code example above shows the exact steps to convert a DOCX file to a JPG image.
Is it possible to convert DOCX to JPG in Laravel in PHP?
Yes. The SDK works with any PHP framework, including Laravel. Place the conversion logic inside a controller or service class and call it from your routes.
What is the best way to convert DOCX to High Resolution JPG in PHP?
Adjust the output resolution parameters (if supported) in the conversion request. The SDK allows you to specify DPI settings in the request body for higher‑quality JPG output.
Can I convert Multiple DOCX to JPG in PHP with a single script?
Absolutely. Loop through an array of DOCX file paths, reuse the same ConvertApi instance, and invoke convertDocument for each file. This approach scales well for batch processing.