You are here

function barcode_code128_barcode in Barcode 6.2

Same name and namespace in other branches
  1. 7.2 plugins/code128.inc \barcode_code128_barcode()

@file Barcode plugin Code 128: Very dense code, used extensively worldwide

File

plugins/code128.inc, line 9
Barcode plugin Code 128: Very dense code, used extensively worldwide

Code

function barcode_code128_barcode($barnumber, $settings) {
  $use_keys = "B";
  if (preg_match("/^[0-9" . chr(128) . chr(129) . chr(130) . "]+\$/", $barnumber)) {
    $use_keys = 'C';
    if (strlen($barnumber) % 2 != 0) {
      $barnumber = '0' . $barnumber;
    }
  }
  for ($i = 0; $i < 32; $i++) {
    $chr = chr($i);
  }
  if (preg_match("/[" . $chr . "]+/", $barnumber)) {
    $use_keys = 'A';
  }
  $bars = barcode_code128_encode($barnumber, $settings, $use_keys);
  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 + 10 * $scale;
  if (!$space) {
    $space = array(
      'top' => 2 * $scale,
      'bottom' => 2 * $scale,
      'left' => 2 * $scale,
      'right' => 2 * $scale,
    );
  }

  /* count total width */
  $xpos = 0;
  $xpos = $scale * strlen($bars) + 2 * $scale * 10;

  /* allocate the image */
  $total_x = $xpos + $space['left'] + $space['right'];
  $xpos = $space['left'] + $scale * 10;
  $height = floor($total_y - $scale * 20);
  $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 ($val == 1) {
      @imagefilledrectangle($im, $xpos, $space['top'], $xpos + $scale - 1, $h, $bar_color);
    }
    $xpos += $scale;
  }
  $font_arr = @imagettfbbox($scale * 10, 0, $settings->font, $barnumber);
  $x = floor($total_x - (int) $font_arr[0] - (int) $font_arr[2] + $scale * 10) / 2;
  @imagettftext($im, $scale * 10, 0, $x, $height2, $bar_color, $settings->font, $barnumber);
  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);
}