Save Generated Barcodes

The Aspose.BarCode Cloud SDK for PHP by the GenerateApi provides methods to generate barcodes to the \SplFileObject. Then you can work with result in order to obtain the desired outcomes. This API supports various barcode formats and configurations, enabling users to customize barcode generation to fit their needs.

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. All of these methods return a \SplFileObject.

Saving the Barcode to File

Below are examples demonstrating how to save the generated barcode stream to a file.

Example: Using generate

<?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\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/Code128.jpeg';

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

    $request = new GenerateRequestWrapper(EncodeBarcodeType::Code128, "Aspose.BarCode.Cloud");
    $request->image_format = BarcodeImageFormat::Png;

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

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

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

main();

Download Result Image

Example: Using generateBody

<?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();

Download Result Image

Example: Using generateMultipart

<?php

declare(strict_types=1);

require 'vendor/autoload.php';

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

    $generateApi = new GenerateApi(null, makeConfiguration());
    
    $request = new GenerateMultipartRequestWrapper(EncodeBarcodeType::Pdf417, "Aspose.BarCode.Cloud");

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

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

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

main();

Download Result Image

Conclusion

The Aspose.BarCode Cloud SDK for PHP offers a robust solution for generating barcodes in various formats, with flexible configuration options. By using the provided methods, developers can easily create and save barcode images to files, streamlining workflows that require barcode generation. Whether you’re working with simple barcodes like Code128 or more complex ones like QR Codes and DataMatrix, this API provides the tools needed to customize and produce high-quality barcode images.