Set Barcode Size

This documentation provides a guide on how to set barcode sizes using in the Aspose.BarCode Cloud SDK for PHP. This guide includes examples and descriptions for configuring the height, width, and resolution of barcode images.

Overview

The GenerateApi interface offers several methods for generating barcodes through different types of HTTP requests:

  • 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.

Key Parameters for Setting Barcode Size

To control the size of a barcode, the following properties in the request objects can be configured:

  • $image_height: Specifies the height of the barcode image.
  • $image_width: Specifies the width of the barcode image.
  • ``: Defines the resolution (DPI) of the barcode image.
  • $units: Defines the measurement units (e.g., pixel, inch, millimeter).

Example Usage

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\{BarcodeImageParams, EncodeBarcodeType};
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->image_height = 200;
    $request->image_width = 200;
    $request->resolution = 300;
    $request->units = 'Pixel';
    

    $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};
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([
            'image_height' => 2,
            'image_width' => 3,
            'resolution' => 96,
            'units' => 'Inch'
        ])
    ]);

    $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 (Form URL Encoded)

<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use Aspose\BarCode\Configuration;
use Aspose\BarCode\GenerateApi;
use Aspose\BarCode\Model\BarcodeImageParams;
use Aspose\BarCode\Model\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/Aztec.png';

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

    $request = new GenerateMultipartRequestWrapper(EncodeBarcodeType::Aztec, "Aspose.BarCode.Cloud");
    $request->image_height = 200;
    $request->image_width = 200;
    $request->resolution = 150;
    $request->units = 'Point';
   
    $generated = $generateApi->generateMultipart($request);

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

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

main();

Result Image is: Result image

Conclusion

Setting the correct barcode dimensions ensures optimal readability and compatibility with various scanning devices. With Aspose.BarCode Cloud SDK for PHP, you can customize the barcode’s height, width, and resolution to meet specific requirements, whether for product labeling, logistics, or QR code marketing.

Proper barcode sizing improves scanning accuracy and ensures a better user experience. By setting the right dimensions using Aspose.BarCode Cloud SDK for PHP, you can ensure your barcodes are perfectly tailored for their intended use.