PHP code example of sglms / gs1-gtin

1. Go to this page and download the library: Download sglms/gs1-gtin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sglms / gs1-gtin example snippets


use Sglms\Gs1Gtin\Gtin;

$gtin = Gtin::create(9876543210);
// GTIN: 10098765432102

$gtin = Gtin::create(
    itemNumber: 98765,
    companyPrefix: '0123'
);
// GTIN: 10123000987656

$gtin = Gtin::create(
    itemNumber: 987654,
    companyPrefix: '123',
    type: 'GTIN-14',
    packagingLevel: 2
);
// GTIN: 21230009876549


// Display barcode (SVG)
echo $gtin->barcode();

// Save barcode
echo $gtin->saveBarcode('path/barcode');
echo "<img src='path/barcode.jpg' />";

Gtin::validate(11230000456781); # TRUE

use Sglms\Gs1Gtin\Gtin12;

$gtin = Gtin12::create(
    itemNumber: 123,
    companyPrefix: '45678',
);
// GTIN / UPC-A: 456780001230

// Save barcode
$gtin->saveBarcode('path/barcode');

// Save barcode with numbers
$gtin->saveWithNumber('../resources/images/gtin12_numbers');
echo "<img src='../resources/images/gtin12_numbers.jpg'/>";

use Sglms\Gs1Gtin\Gtin8;

$gtin = Gtin8::create(
    itemNumber: 123,
    companyPrefix: '4567'
);

echo $gtin;
// GTIN-8 / EAN-8: 45671234

// Save barcode
$gtin->barcode();

$gtin->saveBarcode('../resources/images/gtin8');
echo "<img src='../resources/images/gtin8.jpg' />";

// Save barcode with numbers
$gtin->saveWithNumber('../resources/images/gtin8_numbers');
echo "<img src='../resources/images/gtin8_numbers.jpg'/>";

use Sglms\Gs1Gtin\Gs1;

$gs1 = Gs1::parse('(01)10012345678902(10)ABC123(3201)000500(3302)000700(17)250630(21)SN123456(37)10(11)230101');
echo $gs1;
// GS1: (01)10012345678902(10)ABC123(3102)002268(3302)000700(11)230101(17)250630(21)SN123456(37)10

// Display (SVG)
echo $gs1->barcode(showNumbers:true);

// Or, you can save a JPG image with numbers
$gs1->saveBarcode('../resources/images/images/gs1', ['01','37', '21','3102','3302'], 80);

$gs1 = Gs1::create(
    gtin: '00012345678905',
    serial: 'ABC123',
    netWeightPounds: 1000,
    grossWeight:6000,
    pieces:10,
);
echo $gs1;
echo $gs1->barcode(
    codes: ['01','37', '21','3102','3302'],
    showNumbers:true
);

enum Gs1Code: string
{
    case SSCC           = '00';
    case GTIN           = '01'; // n2+n14
    case Content        = '02'; // n2+n14
    case BatchNumber    = '10';
    case ProductionDate = '11'; // n2+n6
    case PaymentDate    = '12'; // n2+n6
    case BestBeforeDate = '15'; // n2+n6
    case ExpirationDate = '17'; // n2+n6
    case SerialNumber   = '21';
    case Units          = '37'; // n2+n..8
    case NetWeight      = '3102'; //n4+n6 (2 decimals)
    case GrossWeight    = '3302'; //n4+n6 (2 decimals)
}