Customize Barcode Appearance

This documentation explains how to customize the appearance of barcodes using the Aspose.BarCode Cloud SDK for PHP. It provides details on the available methods, customization options, and examples of usage.

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.

Customization Options

When generating a barcode, you can customize various aspects of its appearance using the properties of the request classes.

Key Properties:

  • $barcode_type: Specifies the type of barcode to generate (e.g. QR, Aztec, Pdf417, Code39, etc).
  • $data: The data to encode in the barcode.
  • $image_format: Specifies the output format of the barcode image (e.g., Png, Jpeg, Svg).
  • $foreground_color: Color of the barcode bars, supports standard color names or ARGB values (e.g., AliceBlue or #FF000000).
  • $background_color: Background color of the barcode image.
  • $text_location: Position of the code text relative to the barcode (Below, Above, None).
  • $units: Measurement units for image dimensions (Pixel, Inch, Millimeter).
  • $resolution: Resolution of the image in dpi.
  • $image_height and $image_width: Dimensions of the barcode image.
  • $rotation_angle: Rotation angle of the barcode (e.g., 0, 90, 180).

Barcode Type-Specific Parameters

In addition to the common appearance settings above, the API exposes advanced parameters that apply only to specific barcode types. They are ignored when a different barcode type is generated.

QR, MicroQR and RectMicroQR

  • $qr_encode_mode: QR barcode encode mode (Auto, Extended, Binary, ECI).
  • $qr_error_level: QR error correction level (LevelL, LevelM, LevelQ, LevelH).
  • $qr_version: QR barcode version (Auto, Version01Version40). Auto selects the smallest version that fits the data.
  • $qr_eci_encoding: ECI encoding for the QR barcode data (e.g., UTF8, ISO_8859_1).
  • $qr_aspect_ratio: QR barcode aspect ratio, from 0 to 1.
  • $micro_qr_version: MicroQR version (Auto, M1M4). Used when the barcode type is MicroQR.
  • $rect_micro_qr_version: RectMicroQR version (Auto, R7x43R17x139). Used when the barcode type is RectMicroQR.

Code128

  • $code128_encode_mode: Controls which Code 128 subset is used (Auto, CodeA, CodeB, CodeAB, CodeC, CodeAC, CodeBC).

PDF417, MacroPDF417 and MicroPDF417

  • $pdf417_encode_mode: PDF417 encode mode (Auto, Binary, ECI, Extended).
  • $pdf417_error_level: PDF417 error correction level (Level0Level8).
  • $pdf417_eci_encoding: ECI encoding for the PDF417 barcode data.
  • $pdf417_truncate: Whether to use the truncated PDF417 format (removes the right-side stop pattern).
  • $pdf417_columns: Number of columns (130, or 0 for auto).
  • $pdf417_rows: Number of rows (390, or 0 for automatic).
  • $pdf417_aspect_ratio: Height/width ratio of the barcode module (25 for MicroPDF417; 35 for PDF417 and MacroPDF417).
  • $pdf417_macro_characters: Macro character to prepend for structured append (None, Macro05, Macro06).
  • $pdf417_is_reader_initialization: Whether the barcode is used for reader initialization (programming).
  • $pdf417_is_linked: Whether to use linked mode (for MicroPDF417).
  • $pdf417_is_code128_emulation: Whether to use Code128 emulation (for MicroPDF417).

Examples

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\{BarcodeImageFormat, BarcodeImageParams, CodeLocation, EncodeBarcodeType, QrParams, QREncodeMode, QRErrorLevel, QRVersion};
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());

    $request = new GenerateRequestWrapper(
        EncodeBarcodeType::QR,
        'Aspose.BarCode.Cloud'
    );
    $request->barcode_image_params = new BarcodeImageParams([
        'image_format' => BarcodeImageFormat::Png,
        'foreground_color' => 'Black',
        'background_color' => 'White',
        'text_location' => CodeLocation::Below,
        'resolution' => 300,
        'image_height' => 200,
        'image_width' => 200
    ]);
    $request->qr_params = new QrParams([
        'qr_encode_mode' => QREncodeMode::Auto,
        'qr_error_level' => QRErrorLevel::LevelM,
        'qr_version' => QRVersion::Auto,
        'qr_aspect_ratio' => 0.75
    ]);

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

    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, BarcodeImageParams, BarcodeImageFormat, QrParams, QREncodeMode, QRErrorLevel, QRVersion};
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/QrCustom.jpeg";

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

    $generateParams = new GenerateParams([
        'barcode_type' => EncodeBarcodeType::QR,
        'encode_data' => new EncodeData([
            'data' => 'Aspose',
            'data_type' => EncodeDataType::StringData
        ]),
        'barcode_image_params' => new BarcodeImageParams([
            'foreground_color' => '#FF0000',
            'background_color' => '#FFFF00',
            'image_format' => BarcodeImageFormat::Jpeg,
            'rotation_angle' => 90
        ]),
        'qr_params' => new QrParams([
            'qr_encode_mode' => QREncodeMode::Auto,
            'qr_error_level' => QRErrorLevel::LevelM,
            'qr_version' => QRVersion::Auto,
            'qr_aspect_ratio' => 0.75
        ])
    ]);

    $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\Model\{BarcodeImageFormat, BarcodeImageParams, CodeLocation, Pdf417Params, Pdf417EncodeMode, Pdf417ErrorLevel};
use Aspose\BarCode\Requests\GenerateMultipartRequestWrapper;
use Aspose\BarCode\Model\EncodeBarcodeType;

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.svg';

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

    $request = new GenerateMultipartRequestWrapper(
        EncodeBarcodeType::Pdf417,
        "Aspose.BarCode.Cloud"
    );
    $request->barcode_image_params = new BarcodeImageParams([
        'text_location' => CodeLocation::Above,
        'image_format' => BarcodeImageFormat::Svg
    ]);
    $request->pdf417_params = new Pdf417Params([
        'pdf417_encode_mode' => Pdf417EncodeMode::Auto,
        'pdf417_error_level' => Pdf417ErrorLevel::Level2,
        'pdf417_aspect_ratio' => 3
    ]);

    $barcodeStream = $generateApi->generateMultipart($request);

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

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

main();

Result Image is: Result image