SHARE
TWEET

UPC-A to GTIN-14 Converter

sspence65 May 3rd, 2017 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. echo 'Existing UPC-A Barcode: ';
  3. echo '<br>';
  4. $barcode = '663121023756';
  5. echo $barcode;
  6. echo '<p>';
  7.  
  8. echo 'Strip check digit: ';
  9. echo '<br>';
  10. $barcode = substr($barcode, 0, -1);
  11. echo $barcode;
  12. echo '<p>';
  13.  
  14. echo 'Pre-fix 00 padding: ';
  15. echo '<br>';
  16. $barcode = '00'.$barcode;
  17. echo $barcode;
  18. echo '<p>';
  19.  
  20. echo 'Calculate Check Digit: ';
  21. echo '<br>';
  22.  
  23. for ($counter = 0; $counter < strlen($barcode); $counter++)
  24. {
  25.     if ($counter % 2 == 0)
  26.     {
  27.         $oddoutput .= $barcode[$counter];
  28.     }
  29.     else
  30.     {
  31.         $evenoutput .= $barcode[$counter];
  32.     }
  33. }
  34.  
  35. $arr1 = str_split($evenoutput);
  36. $arr2 = str_split($oddoutput);
  37. $odd = array_sum($arr2);
  38. $even = array_sum($arr1);
  39. $odd = $odd * 3;
  40. $arr_total = $odd + $even;
  41. $arr_mod = $arr_total % 10;
  42.  
  43. if ($arr_mod != 0){
  44.     $checkdigit = 10 - $arr_mod;
  45. } else {
  46.     $checkdigit = $arr_mod;
  47. }
  48.  
  49. echo 'Check Digit: ' . $checkdigit;
  50. echo '<p>';
  51.  
  52. echo 'Re-assemble GTIN-14 Barcode: ';
  53. echo '<br>';
  54. $barcode =  '01' .$barcode . $checkdigit;
  55.  
  56. echo $barcode;
  57. echo '<p>';
  58.  
  59. echo '<img src="http://barcode.tec-it.com/barcode.ashx?translate-esc=on&data='.$barcode.'&code=EANUCC128&unit=Fit&dpi=96&imagetype=Gif&rotation=0&color=000000&bgcolor=FFFFFF&qunit=Mm&quiet=0" alt="Barcode Generator TEC-IT"/>';
  60.  
  61. ?>
RAW Paste Data
Want to get better at PHP?
Learn to code PHP in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top