You are here

function BARCODE::_ean13CheckDigit in Barcode 6

2 calls to BARCODE::_ean13CheckDigit()
BARCODE::_eanBarcode in ./barcode.inc.php
BARCODE::_upceBarcode in ./barcode.inc.php

File

./barcode.inc.php, line 1555

Class

BARCODE

Code

function _ean13CheckDigit($barnumber) {
  $csumTotal = 0;

  // The checksum working variable starts at zero
  // If the source message string is less than 12 characters long, we make it 12 characters
  if (strlen($barnumber) <= 12) {
    $barnumber = str_pad($barnumber, 13, "0", STR_PAD_LEFT);
  }

  /*if(strlen($barnumber) == 13)
  		$barnumber = substr($barnumber,0,12);*/

  // Calculate the checksum value for the message
  for ($i = 0; $i < strlen($barnumber); $i++) {
    if ($i % 2 == 0) {
      $csumTotal = $csumTotal + intval($barnumber[$i]);
    }
    else {
      $csumTotal = $csumTotal + 3 * intval($barnumber[$i]);
    }
  }

  // Calculate the checksum digit
  if ($csumTotal % 10 == 0) {
    $checksumDigit = '';
  }
  else {
    $checksumDigit = 10 - $csumTotal % 10;
  }
  return $barnumber . $checksumDigit;
}