Skip to content

Instantly share code, notes, and snippets.

@JaggedJax
Created October 4, 2017 20:34
Show Gist options
  • Save JaggedJax/b36ebe1cd704e718eff2c08cc6286f64 to your computer and use it in GitHub Desktop.
Save JaggedJax/b36ebe1cd704e718eff2c08cc6286f64 to your computer and use it in GitHub Desktop.
Generate SSCC-18 barcode & verify SSCC-18 check digit
<?php
/**
* Generate and return an SSCC-18 barcode. DOES NOT include leading "00"
* Params can be passed as strings or ints and will be padded to proper length.
* @param string $next_serial_available
* @param string $extension
* @param string $gs1_prefix
* @param string $min_serial
* @param string $max_serial
* @return string 18 digit Barcode or NULL on error.
*/
public static function generate_sscc_barcode($next_serial_available, $extension, $gs1_prefix, $min_serial, $max_serial){
// Some basic sanity checks
if(!is_numeric($next_serial_available) || !is_numeric($gs1_prefix) || !is_numeric($min_serial) || !is_numeric($max_serial)){
return null;
}
if($max_serial > 999999999){
$max_serial = 999999999;
}
// Alternatively figure out next available serial number here. Make sure transaction is atomic.
$serial = $next_serial_available;
// Make sure extension digit is valid. Otherwise set it to 0
if(empty($extension) || strlen($extension) !== 1 || !preg_match('/[0-9]/', $extension)){
$extension = '0';
}
$barcode = $extension.str_pad($gs1_prefix, 7, '0', STR_PAD_LEFT).str_pad($serial, 9, '0', STR_PAD_LEFT);
$check_digit = sscc_check_digit($barcode);
// If we couldn't generate check digit, return null
if($check_digit === false){
return null;
}
return "$barcode$check_digit";
}
/**
* Given a sscc barcode w/ or w/o final check digit will either return check digit if missing or TRUE/FALSE if digit does/doesn't match or formatting is incorrect.
* @param string $barcode
* @return mixed Returns true/false if existing check digit is correct. Returns check digit if it's missing from the barcode.
*/
function sscc_check_digit($barcode){
// Strip off leading 0's if 20 digit format was provided
$len = strlen($barcode);
if (($len == 20 || $len == 19) && strpos($barcode, '00') === 0){
$barcode = substr($barcode, 2);
}
// Return false if invalid length
$len = strlen($barcode);
if ($len != 18 && $len != 17){
return false;
}
// For explanation see: http://www.gs1.org/how-calculate-check-digit-manually
$sum = 0;
for($i=0; $i<17; $i++){
$multiplier = ($i % 2 == 0) ? 3 : 1;
$sum += intval($barcode{$i}) * $multiplier;
}
$check_digit = (ceil($sum / 10) * 10) - $sum;
if($len == 17){
return "$check_digit";
}
if($len == 18){
return ($check_digit == intval($barcode{17}));
}
}
@igarpoor
Copy link

Hi JaggedJax, i cannot find a lot PHP resources about SSCC-18, i need to create images of a SSCC-18 in PHP, do you know where can i found something about this? Thanks.

@joeybab3
Copy link

Hi JaggedJax, i cannot find a lot PHP resources about SSCC-18, i need to create images of a SSCC-18 in PHP, do you know where can i found something about this? Thanks.

You can use the library at https://github.com/picqer/php-barcode-generator to do that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment