Set Barcode Colorscheme

This documentation provides a comprehensive guide on how to set the colorscheme for barcodes using the Aspose.BarCode Cloud SDK for PHP. The API allows customization of barcode appearance, including foreground and background colors, ensuring your barcodes match your design needs.

Overview

The GenerateApi interface includes methods to generate barcodes using different request formats:

  • generate: GET request with parameters in route and query string.
  • generateBody: POST request with parameters in the body (JSON or XML).
  • generateMultipart: POST request with parameters in multipart form.

Colorscheme Parameters

The $foreground_color and $background_color properties enable you to define the colors for the barcode:

  • $foreground_color: Specifies the color of the bars and text.
  • $background_color: Specifies the background color of the barcode image.

Both properties accept standard color names or ARGB values (e.g., AliceBlue or #FF000000).

Examples

Example 1: Setting Colors Using GET Request

<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use Aspose\BarCode\Configuration;
use Aspose\BarCode\GenerateApi;
use Aspose\BarCode\Model\BarcodeImageFormat;
use Aspose\BarCode\Model\BarcodeImageParams;
use Aspose\BarCode\Model\EncodeBarcodeType;
use Aspose\BarCode\Model\GenerateParams;
use Aspose\BarCode\Model\EncodeData;
use Aspose\BarCode\Model\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());

    $request = new GenerateRequestWrapper(
        EncodeBarcodeType::QR,
        "https://products.aspose.cloud/barcode/family/"
    );
    $request->foreground_color = "DarkBlue";
    $request->background_color = "LightGray";
    $request->image_format = BarcodeImageFormat::Png;

    $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: Setting Colors Using POST Request with 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};
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' => 'Aspose.BarCode.Cloud',
            'data_type' => EncodeDataType::StringData
        ]),
        'barcode_image_params' => new BarcodeImageParams([
            'foreground_color' => '#FF5733',
            'background_color' => '#FFFFFF',
            'image_format' => BarcodeImageFormat::Jpeg
        ])
    ]);

    $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: Setting Colors Using Multipart Form POST

<?php

declare(strict_types=1);

require 'vendor/autoload.php';

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

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/Code39.png';

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

    $request = new GenerateMultipartRequestWrapper(EncodeBarcodeType::Code39, "Aspose");
    $request->foreground_color = "Green";
    $request->background_color = "Yellow";
    $request->image_format = BarcodeImageFormat::Gif;

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

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

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

main();

Result Image is: Result image

Conclusion

With GenerateApi in Aspose.BarCode Cloud SDK for PHP, you can easily generate barcodes with customized colors. Whether you prefer GET, POST with JSON, or multipart form POST, the flexibility of this API ensures seamless integration and customization of barcodes in your applications.