How to generate a EAN-13 barcode as a PNG file in PHP
1 min readAug 6, 2019
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 fromtry {
$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.