source: trunk/xgraph/jpgraph/Examples/barcode_usps_example.php @ 42

Last change on this file since 42 was 42, checked in by marrucho, 10 years ago
File size: 2.2 KB
Line 
1<?php
2// =======================================================
3// Example of how to format US Postal shipping information
4// =======================================================
5require_once ('jpgraph/jpgraph_barcode.php');
6
7// The Full barcode standard is described in
8// http://www.usps.com/cpim/ftp/pubs/pub91/91c4.html#508hdr1
9//
10// The data start with AI=420 which means
11// "Ship to/Deliver To Postal Code (within single authority)
12//
13class USPS_Confirmation {
14    function USPS_Confirmation() {
15    }
16
17    // Private utility function
18    function _USPS_chkd($aData) {
19        $n = strlen($aData);
20
21        // Add all even numbers starting from position 1 from the end
22        $et = 0 ;
23        for( $i=1; $i <= $n; $i+=2 ) {
24            $d = intval(substr($aData,-$i,1));
25            $et += $d;
26        }
27
28        // Add all odd numbers starting from position 2 from the end
29        $ot = 0 ;
30        for( $i=2; $i <= $n; $i+=2 ) {
31            $d = intval(substr($aData,-$i,1));
32            $ot += $d;
33        }
34        $tot = 3*$et + $ot;
35        $chkdigit = (10 - ($tot % 10))%10;;
36        return $chkdigit;
37    }
38
39    // Get type 1 of confirmation code (with ZIP)
40    function GetPICwithZIP($aZIP,$aServiceType,$aDUNS,$aSeqNbr) {
41        // Convert to USPS format with AI=420 and extension starting with AI=91
42        $data = '420'. $aZIP . '91' . $aServiceType . $aDUNS . $aSeqNbr;
43        // Only calculate the checkdigit from the AI=91 and forward
44        // and do not include the ~1 (FUNC1) in the calculation
45        $cd = $this->_USPS_chkd(substr($data,8));
46        $data = '420'. $aZIP . '~191' . $aServiceType . $aDUNS . $aSeqNbr;
47        return $data . $cd;
48    }
49
50    // Get type 2 of confirmation code (without ZIP)
51    function GetPIC($aServiceType,$aDUNS,$aSeqNbr) {
52        // Convert to USPS format with AI=91
53        $data = '91' . $aServiceType . $aDUNS . $aSeqNbr;
54        $cd = $this->_USPS_chkd($data);
55        return $data . $cd;
56    }
57
58}
59
60$usps = new USPS_Confirmation();
61$zip     = '92663';
62$service = '21';
63$DUNS    = '805213907';
64$seqnr   = '04508735';
65$data = $usps->GetPICwithZIP($zip,$service,$DUNS,$seqnr);
66//$data = $usps->GetPIC('01','123456789','00000001');
67
68$encoder = BarcodeFactory::Create(ENCODING_EAN128);
69$e = BackendFactory::Create(BACKEND_IMAGE,$encoder);
70$e->SetModuleWidth(2);
71$e->SetFont(FF_ARIAL,FS_NORMAL,14);
72$e->Stroke($data);
73
74?>
Note: See TracBrowser for help on using the repository browser.