How to generate a EAN-13 barcode as a PNG file in PHP

Here is the easiest way to generate an EAN-13 barcode automatically. You will be amazed at just how easy it really is. Let’s start with installing our barcode API client. Run the following command in Composer:

composer require cloudmersive/cloudmersive_barcode_api_client

Now call the generateBarcodeEAN13 function using the following snippet:

<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Apikey', 'Bearer');
$apiInstance = new Swagger\Client\Api\GenerateBarcodeApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client(),
$config
);
$value = "value_example"; // string | Barcode value to generate from
try {
$result = $apiInstance->generateBarcodeEAN13($value);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling GenerateBarcodeApi->generateBarcodeEAN13: ', $e->getMessage(), PHP_EOL;
}
?>

And… that’s it. Your output with be a PNG image of the barcode value that you entered. You might be interested to know that our API also supports many other barcode formats as well as QR codes.

Image for post
Image for post

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store