You are here

function barcode_upce_barcode in Barcode 7.2

Same name and namespace in other branches
  1. 6.2 plugins/upce.inc \barcode_upce_barcode()

File

plugins/upce.inc, line 13
Barcode plugin UPC-E: Short version of UPC symbol, 6 characters

Code

function barcode_upce_barcode($barnumber, $settings) {
  if (strlen($barnumber) > 6) {
    barcode_load_plugin('ean');
    barcode_ean13_check_digit($barnumber);
    $barnumber = substr($this
      ->barcode_ean13_check_digit($barnumber), 1);
    $encbit = $barnumber[0];
    $checkdigit = $barnumber[11];
    $barnumber = barcode_convert_upca_to_upce($barnumber, $settings);
  }
  else {
    $barnumber = barcode_check_digit($barnumber, 7);
    $encbit = $barnumber[0];
    $checkdigit = $barnumber[7];
    $barnumber = substr($barnumber, 1, 6);
  }
  $bars = barcode_upce_encode($barnumber, $settings, $encbit, $checkdigit);
  if (empty($settings->filename_no_format)) {
    header("Content-type: image/" . $settings->format);
  }
  $scale = $settings->scale;
  if ($scale < 1) {
    $scale = 2;
  }
  $total_y = (double) $scale * $settings->height;
  $space = array(
    'top' => 2 * $scale,
    'bottom' => 2 * $scale,
    'left' => 2 * $scale,
    'right' => 2 * $scale,
  );

  /* count total width */
  $xpos = $scale * strlen($bars) + $scale * 12;

  /* allocate the image */
  $total_x = $xpos + $space['left'] + $space['right'];
  $xpos = $space['left'] + $scale * 6;
  $height = floor($total_y - $scale * 10);
  $height2 = floor($total_y - $space['bottom']);
  $im = @imagecreatetruecolor($total_x, $total_y);
  $bg_color = @imagecolorallocate($im, $settings->bgcolor[0], $settings->bgcolor[1], $settings->bgcolor[2]);
  @imagefilledrectangle($im, 0, 0, $total_x, $total_y, $bg_color);
  $bar_color = @imagecolorallocate($im, $settings->color[0], $settings->color[1], $settings->color[2]);
  for ($i = 0; $i < strlen($bars); $i++) {
    $h = $height;
    $val = strtoupper($bars[$i]);
    if (preg_match("/[a-z]/i", $val)) {
      $val = ord($val) - 65;
      $h = $height2;
    }
    if ($val == 1) {
      @imagefilledrectangle($im, $xpos, $space['top'], $xpos + $scale - 1, $h, $bar_color);
    }
    $xpos += $scale;
  }
  @imagettftext($im, $scale * 6, 0, $space['left'], $height, $bar_color, $settings->font, $encbit);
  $x = $space['left'] + $scale * strlen($barnumber) + $scale * 6;
  @imagettftext($im, $scale * 6, 0, $x, $height2, $bar_color, $settings->font, $barnumber);
  $x = $total_x - $space['left'] - $scale * 6;
  @imagettftext($im, $scale * 6, 0, $x, $height, $bar_color, $settings->font, $checkdigit);
  if ($settings->format == "png") {
    if (!empty($settings->filename_no_format)) {
      @imagepng($im, $settings->filename_no_format . "." . $settings->format);
    }
    else {
      @imagepng($im);
    }
  }
  if ($settings->format == "gif") {
    if (!empty($settings->filename_no_format)) {
      @imagegif($im, $settings->filename_no_format . "." . $settings->format);
    }
    else {
      @imagegif($im);
    }
  }
  if ($settings->format == "jpg" || $settings->format == "jpeg") {
    if (!empty($settings->filename_no_format)) {
      @imagejpeg($im, $settings->filename_no_format . "." . $settings->format);
    }
    else {
      @imagejpeg($im);
    }
  }
  @imagedestroy($im);
}