Set Barcode Text

This documentation covers how to set barcode text in the Aspose.BarCode Cloud SDK for PHP. The Barcode API supports multiple methods to generate barcodes in different formats using GET and POST requests through the GenerateApi. Barcodes can be generated by specifying various parameters such as barcode type, data to encode, image format and display options. The data encoding process involves defining the type of data and the actual content.

GenerateApi

The GenerateApi interface includes methods for generating barcodes:

  • GET Request: Uses parameters in the URL route and query string.
  • POST Request (JSON/XML): Parameters are provided in the request body.
  • POST Request (Multipart Form): Parameters are sent as URL-encoded form data.

Methods

  • generate: Generates a barcode via GET request.
  • generateBody: Generates a barcode via POST request with JSON or XML body.
  • generateMultipart: Generates a barcode via POST request with multipart form data.

Setting Barcode Text

Properties

  • $data_type: Specifies the type of data encoding (can be string, base64, or hex). If not set, the $data value is used as plain text.
  • $data: A string that represents the data to be encoded.

Supported Data Types

Value Description
StringData (Default value) Plain text
Base64Bytes Base64 encoded binary data
HexBytes Hexadecimal encoded binary data

Barcode Generation Example

Below are examples to demonstrate barcode generation using the GenerateApi methods.

Example 1: Generating a Barcode with GET Request

<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use Aspose\BarCode\Configuration;
use Aspose\BarCode\GenerateApi;
use Aspose\BarCode\Model\{EncodeBarcodeType, EncodeData, EncodeDataType};
use Aspose\BarCode\Requests\GenerateRequestWrapper;

function makeConfiguration(): Configuration
{
    $config = new Configuration();

    $envToken = getenv("TEST_CONFIGURATION_ACCESS_TOKEN");
    if (empty($envToken)) {
        $config->setClientId("Client Id from https://dashboard.aspose.cloud/applications");
        $config->setClientSecret("Client Secret from https://dashboard.aspose.cloud/applications");
    } else {
        $config->setAccessToken($envToken);
    }

    return $config;
}

function main(): void
{
    $fileName = __DIR__ . '/../testdata/Qr.png';

    $generateApi = new GenerateApi(null, makeConfiguration());

    $getRequest = new GenerateRequestWrapper(EncodeBarcodeType::QR, "Aspose.BarCode.Cloud");
    $getRequest->data_type = EncodeDataType::StringData;

    $generated = $generateApi->generate($getRequest);

    file_put_contents($fileName, $generated->fread($generated->getSize()));

    echo "File '{$fileName}' generated.\n";
}

main();

Result Image is: Result image

Example 2: Generating a Barcode with POST Request (JSON Body)

<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use Aspose\BarCode\Configuration;
use Aspose\BarCode\GenerateApi;
use Aspose\BarCode\Model\{GenerateParams, EncodeBarcodeType, EncodeData, EncodeDataType};
use Aspose\BarCode\Requests\GenerateBodyRequestWrapper;

function makeConfiguration(): Configuration
{
    $config = new Configuration();

    $envToken = getenv("TEST_CONFIGURATION_ACCESS_TOKEN");
    if (empty($envToken)) {
        $config->setClientId("Client Id from https://dashboard.aspose.cloud/applications");
        $config->setClientSecret("Client Secret from https://dashboard.aspose.cloud/applications");
    } else {
        $config->setAccessToken($envToken);
    }

    return $config;
}

function main(): void
{
    $fileName = __DIR__ . '/../testdata/Pdf417.png';

    $generateApi = new GenerateApi(null, makeConfiguration());

    $generateParams = new GenerateParams([
        'barcode_type' => EncodeBarcodeType::Pdf417,
        'encode_data' => new EncodeData([
            'data' => 'QXNwb3NlLkJhckNvZGUuQ2xvdWQ=',
            'data_type' => EncodeDataType::Base64Bytes
        ])
    ]);

    $request = new GenerateBodyRequestWrapper($generateParams);
    $generated = $generateApi->generateBody($request);

    file_put_contents($fileName, $generated->fread($generated->getSize()));

    echo "File '{$fileName}' generated.\n";
}

main();

Result Image is: Result image

Example 3: Generating a Barcode with POST Request (Multipart Form)

<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use Aspose\BarCode\Configuration;
use Aspose\BarCode\GenerateApi;
use Aspose\BarCode\Requests\GenerateMultipartRequestWrapper;
use Aspose\BarCode\Model\{EncodeBarcodeType, EncodeDataType};

function makeConfiguration(): Configuration
{
    $config = new Configuration();

    $envToken = getenv("TEST_CONFIGURATION_ACCESS_TOKEN");
    if (empty($envToken)) {
        $config->setClientId("Client Id from https://dashboard.aspose.cloud/applications");
        $config->setClientSecret("Client Secret from https://dashboard.aspose.cloud/applications");
    } else {
        $config->setAccessToken($envToken);
    }

    return $config;
}

function main(): void
{
    $fileName = __DIR__ . '/../testdata/Code128.png';

    $generateApi = new GenerateApi(null, makeConfiguration());

    $formRequest = new GenerateMultipartRequestWrapper(EncodeBarcodeType::Code128, "4173706F73652E426172436F64652E436C6F7564");
    $formRequest->data_type = EncodeDataType::HexBytes;

    $generated = $generateApi->generateMultipart($formRequest);

    file_put_contents($fileName,  $generated->fread($generated->getSize()));

    echo "File '{$fileName}' generated.\n";
}

main();

Result Image is: Result image

This documentation covers how to use the GenerateApi to set barcodes text.