PHP - XLSX to JPG

Using PHP to convert XLSX to JPG

Zamzar offers a simple file conversion API to convert files from your applications with support for 100's of formats. Below we have listed an example to convert a XLSX file to JPG using PHP. We also support a variety of other programming languages.

If you have any questions check out our comprehensive FAQ which contains further information on how to use the API.

PHP Code Sample

Our example code assumes that you're using PHP 5.3 or newer.

To convert your first file with the Zamzar API, send an HTTP request to POST https://sandbox.zamzar.com/v1/jobs containing your source file, and the your desired target format. If the source file is on the web or in S3, send us the URL: the source file doesn't need to hit your servers.

<?php

$endpoint = "https://sandbox.zamzar.com/v1/jobs";
$apiKey = "GiVUYsF4A8ssq93FR48H";
$sourceFile = "https://s3.amazonaws.com/zamzar-samples/sample.xlsx";
$targetFormat = "JPG";

$postData = array(
  "source_file" => $sourceFile,
  "target_format" => $targetFormat
);

$ch = curl_init(); // Init curl
curl_setopt($ch, CURLOPT_URL, $endpoint); // API endpoint
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as a string
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":"); // Set the API key as the basic auth username
$body = curl_exec($ch);
curl_close($ch);

$response = json_decode($body, true);

echo "Response:\n---------\n";
print_r($response);

Your source file is now being converted. Send an HTTP request to GET https://sandbox.zamzar.com/v1/jobs/$jobId to check its progress. The response will also give you details about your converted file.

<?php

$jobID = 15;
$endpoint = "https://sandbox.zamzar.com/v1/jobs/$jobID";
$apiKey = "GiVUYsF4A8ssq93FR48H";

$ch = curl_init(); // Init curl
curl_setopt($ch, CURLOPT_URL, $endpoint); // API endpoint
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as a string
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":"); // Set the API key as the basic auth username
$body = curl_exec($ch);
curl_close($ch);

$job = json_decode($body, true);

echo "Job:\n----\n";
print_r($job);

Once the status of your job is successful, your converted file is ready to download. Send an HTTP request to GET https://sandbox.zamzar.com/v1/file/$fileId/content to download it. We store your files for a day by default, and for longer on our paid plans.

<?php

$fileID = 3;
$localFilename = "converted.jpg";;
$endpoint = "https://sandbox.zamzar.com/v1/files/$fileID/content";
$apiKey = "GiVUYsF4A8ssq93FR48H";

$ch = curl_init(); // Init curl
curl_setopt($ch, CURLOPT_URL, $endpoint); // API endpoint
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":"); // Set the API key as the basic auth username
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

$fh = fopen($localFilename, "wb");
curl_setopt($ch, CURLOPT_FILE, $fh);

$body = curl_exec($ch);
curl_close($ch);

echo "File downloaded\n";

If you like what you see and want to start converting files under your own API account then please click the "Get Started Now" button to signup for your own API account. Please feel free to get in touch with us should you have any specific questions or refer to our extensive docs and FAQ for further information.

Get Started Now

Why use Zamzar?

  • Hosting

    Don't worry about hosting and using your own servers we do this for you.

  • S3 Integration

    Automatically import and export to S3 with 2 lines of code.

  • Simple Pricing

    Fixed price monthly accounts which come bundled with conversion credits.

  • Great Support

    Our support team is staffed by software developers who will help to fix your problem.

  • Conversion Experts

    File conversion experts, having converted 350 million files over the past decade.

Using XLSX with PHP

Reading Excel files with PHP is not straight-forward but there's an excellent open source solution (licensed under LGPL version 3) known as PHP Spreadsheet. Perhaps unlike other libraries the best way to use this is to convert the excel sheets into PHP arrays and then use the data in PHP. There are three easy to follow steps that need to be followed - from loading the file in, to defining the reader options to then being able to load and display the data.

Another option is to use the open source, community driven PEAR package for generating Excel Spreadsheets.

A third alternative is using the Zamzar API. The API will allow you to manipulate your Excel file seamlessly and with the same quality as the native Microsoft Office toolset. There's no need to worry about hosting, and with S3 Integration you can automatically import and export to S3 with just two lines of code. There's a dedicated support team in place who can provide assistance at any stage of the process plus there's an extensive documentation library that should cover any issues/FAQ's.

Resources:
Related StackOverflow Questions:

Using JPG with PHP

You have a number of options when looking to convert JPG files using PHP and each of them have various pros and cons. You could consider using the GD PHP library which has quite extensive support for image processing and has a large number of online resources which you can use as a great reference guide when writing your code.

Another option is to use an open source tool such as ImageMagick, which is known as the "Swiss Army Knife" of image processing. It has a large number of configurable options and is fairly easily configurable through a handy object-oriented interface - see these examples. It also has extensive documentation and there is a wide body of online support in forums should you hit technical issues.

If you'd prefer to use commercial software with dedicated support from developers and a high quality rendering engine you could consider using the Zamzar API. With code examples in many of the major languages including PHP, simple low cost conversion credits and support for direct import and export to S3, it may cover most of the use cases you have. Feel free to reach out to our support team with any questions or dip into the getting started guide in our docs.

Resources:
Related StackOverflow Questions: