Customize Barcode Appearance
Contents
[
Hide
]
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).
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, CodeLocation, 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_format = BarcodeImageFormat::Png;
$request->foreground_color = 'Black';
$request->background_color = 'White';
$request->text_location = CodeLocation::Below;
$request->resolution = 300;
$request->image_height = 200;
$request->image_width = 200;
$generated = $generateApi->generate($request);
file_put_contents($fileName, $generated->fread($generated->getSize()));
echo "File '{$fileName}' generated.\n";
}
main();
Result Image is:
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};
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/Code39.jpeg";
$generateApi = new GenerateApi(null, makeConfiguration());
$generateParams = new GenerateParams([
'barcode_type' => EncodeBarcodeType::Code39,
'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
])
]);
$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:
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};
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->text_location = "Above";
$request->image_format = BarcodeImageFormat::Svg;
$barcodeStream = $generateApi->generateMultipart($request);
file_put_contents($fileName, $barcodeStream->fread($barcodeStream->getSize()));
echo "File '{$fileName}' generated.\n";
}
main();
Result Image is: