title: print barcode generated in div
body: <p>I have a screen where I load value from the database then I load controller for barcode and generate an image. I want to print generated barcode picture.</p>
  
  <p>This is barcode controller
  <a href="https://github.com/jahangiralammahi/codeigniter_barcode_master" rel="nofollow noreferrer">https://github.com/jahangiralammahi/codeigniter_barcode_master</a>
  I believe there is also a chance to save to file .png and the print. But also failed at that.
  So any help would be much appreciated</p>
  
  <pre><code>&lt;td&gt;
  &lt;div id="printableArea"&gt;
  &lt;img src="&lt;?php echo base_url('Barcode/barcode_generator')?&gt;/code128a/30/&lt;?php echo $operarios-&gt;codigoean?&gt;/true"/&gt;
  &lt;!-- &lt;img src="&lt;?php// echo base_url('Barcode/barcode_generator')?&gt;/Code39/40/EDT45SGHJKLE/true"/&gt; --&gt;  
  
  &lt;/div&gt;  &lt;input type="button" onclick="printDiv('printableArea')" value="print" /&gt;
  &lt;/td&gt;
  </code></pre>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  function printDiv(printableArea)
      {
          Popup($(printableArea).html());
      }
  
      function Popup(data) 
      {
          var mywindow = window.open('', 'my div', 'height=100,width=100');
          mywindow.document.write('&lt;img src="&lt;?php echo base_url('Barcode/barcode_generator')?&gt;/code128a/30/&lt;?php echo $operarios-&gt;codigoean?&gt;/true"/&gt;');
       //   mywindow.document.write('&lt;link rel="stylesheet" href="main.css" type="text/css" /&gt;');
      //    mywindow.document.write('&lt;/head&gt;&lt;body &gt;');
          mywindow.document.write(data);
      //    mywindow.document.write('&lt;/body&gt;&lt;/html&gt;');
  
          mywindow.print();
          mywindow.close();
  
          return true;
      }
  
  
  &lt;/script&gt;
  </code></pre>
  
  <p>I can have print view of the last row barcode, si i guess it grabs last used id reference. So any print button i press i get the same last barcode.</p>
  
  <pre><code>
  MY VIEW PAGE
  
  &lt;link rel="stylesheet" href="assets/css/bootstrap/bootstrap.min.css"&gt;
  &lt;link rel="stylesheet" href="assets/datatables/dataTables.bootstrap.css"&gt;
  
  &lt;div class="content-wrapper"&gt;
        &lt;section class="content-header"&gt;
              &lt;h1&gt;Lista Operários&lt;/h1&gt;
              &lt;ol class="breadcrumb"&gt;
                    &lt;li&gt;&lt;a href="#"&gt;&lt;i class="fa fa-dashboard"&gt;&lt;/i&gt; Home&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;Operários&lt;/li&gt;
                    &lt;li class="active"&gt;Lista operários&lt;/li&gt;
              &lt;/ol&gt;
  
  
        &lt;/section&gt;
  
        &lt;section class="content"&gt;
              &lt;div class="row"&gt;
                    &lt;div class="col-xs-12 col-sm-12 col-lg-12"&gt;
                          &lt;div class="box box-warning"&gt;
  
                                &lt;?php
                                if (isset($msg)) {
                                      echo '&lt;div class="box-header with-border"&gt;' . $msg . '&lt;/div&gt;';
                                }
                                ?&gt;
                                &lt;div class="box"&gt;
  &lt;!--                                    &lt;div class="box-header with-border"&gt;
                                            &lt;h3 class="box-title"&gt;Lista de operarios&lt;/h3&gt;
                                      &lt;/div&gt;--&gt;
                                      &lt;!-- /.box-header --&gt;
                                      &lt;div class="box-body"&gt;
                                            &lt;form role="form" name="formulariooperario" id="formulariooperario" action="alteraoperario" method="post" class="form-horizontal"&gt;
                                                  &lt;input name="idoperario" id="idoperario" type="hidden" value="" readonly="readonly"&gt;
                                            &lt;/form&gt;
                                            &lt;table id="example1" class="table table-bordered table-striped"&gt;
                                                  &lt;thead&gt;
                                                        &lt;tr&gt;
                                                               &lt;th style="width:24px"&gt;    &lt;/th&gt;
                                                              &lt;th&gt;id&lt;/th&gt;
                                                              &lt;th&gt;nome&lt;/th&gt;
                                                              &lt;th&gt;Profissão&lt;/th&gt;
  
                                                              &lt;th&gt;email&lt;/th&gt;
                                                              &lt;th&gt;codigoean&lt;/th&gt;
                                                              &lt;th&gt;Status&lt;/th&gt;
                                                              &lt;th style="width:1px;"&gt;&amp;nbsp;&lt;/th&gt;
  
                                                        &lt;/tr&gt;
                                                  &lt;/thead&gt;
                                                  &lt;tbody&gt;
                                                        &lt;?php
                                                        if ((isset($resultadoOperario)) &amp;&amp; (!empty($resultadoOperario))) {
                                                              foreach ($resultadoOperario as $operarios) {
                                                                    ?&gt;
                                                                    &lt;tr&gt;
                                                                          &lt;td&gt;
  
                                                                          &lt;?php  if($edit_oper ==='1' ) {  ?&gt;
                                                                          &lt;a href="alteraoperario?id=&lt;?php echo $operarios-&gt;id; ?&gt;"&gt;&lt;i class="fa fa-pencil-square-o"&gt;&lt;/i&gt; &lt;/a&gt; 
                                                                          &lt;?php       }  ;?&gt;
                                                                          &lt;?php  if($del_oper ==='1' ) {  ?&gt;
                                                                          &lt;a href="&lt;?php echo base_url().'home/deleteoperario/'.$operarios-&gt;id; ?&gt;" class="btn btn-danger btn-xs"&gt;&lt;span class="fa fa-trash" title="!!!Elimina todas as informações do colaborador"&gt;&lt;/a&gt;
                                                                          &lt;?php       }  ;?&gt;
  
                                                                          &lt;a href="alteraoperario2?id=&lt;?php echo $operarios-&gt;id;  ?&gt;" class="btn btn-default"&gt;&lt;i  class="fa fa-plus"&gt;&lt;/i&gt; Documentação operários&lt;/a&gt;
  
  
  
  
  
  
  
  
                                                                          &lt;/td&gt;
                                                                           &lt;td&gt;&lt;?php echo $operarios-&gt;id; ?&gt;&lt;/td&gt;
                                                                          &lt;td&gt;&lt;?php echo $operarios-&gt;nomefantasia; ?&gt;&lt;/td&gt;
                                                                          &lt;td&gt;
  
  
                                                                                                                                                      &lt;?php
                                                                                if ((int)$operarios-&gt;profissao === 1) {
                                                                                      echo "soldador";                                                                                
  
                                                                              } else {
                                                                                       if ((int)$operarios-&gt;profissao === 2) {                                                                                   
                                                                                            echo "tubista";                                                                               
                                                                                } else {
                                                                                       if ((int)$operarios-&gt;profissao === 3) {                                                                                   
                                                                                            echo "serralheiro";
                                                                               } else {
                                                                                       if ((int)$operarios-&gt;profissao === 4) {
                                                                                            echo "pintor";
                                                                              } else {
                                                                                       if ((int)$operarios-&gt;profissao === 5) {
                                                                                            echo "motorista"; 
                                                                              } else {
                                                                                       if ((int)$operarios-&gt;profissao === 6) {
                                                                                            echo "operador";  
                                                                              } else {
                                                                                       if ((int)$operarios-&gt;profissao === 7) {
                                                                                            echo "encarregado";   
                                                                              } else {
                                                                                       if ((int)$operarios-&gt;profissao === 8) {
                                                                                            echo "eletromecânico";    
                                                                              } else {
                                                                                       if ((int)$operarios-&gt;profissao === 9) {
                                                                                            echo "armador";   
                                                                              } else {
                                                                                       if ((int)$operarios-&gt;profissao === 10) {
                                                                                            echo "indiferenciado";    
                                                                              } else {
                                                                                       if ((int)$operarios-&gt;profissao === 11) {
                                                                                            echo "outro"; 
                                                                              } else {
                                                                                       if ((int)$operarios-&gt;profissao === 12) {
                                                                                            echo "resp armazém";  
                                                                              } else {
                                                                                       if ((int)$operarios-&gt;profissao === 13) {
                                                                                            echo "ferramenteiro"; 
                                                                              } else {
                                                                                       if ((int)$operarios-&gt;profissao === 14) {
                                                                                            echo "ajudante";  
  
  
                                                                                     } else {
                                                                                            echo '&lt;i class="fa fa-circle 2x" style="color: red;"&gt;';
                                                                                      }
                                                                              }}}}}}}}}}}}}
                                                                                ?&gt;
  
  
  
  
  
  
  
  
  
  
                                                                          &lt;/td&gt;
  
                                                                          &lt;td&gt;&lt;?php echo $operarios-&gt;email; ?&gt;&lt;/td&gt;
                                                                          &lt;td&gt;
                                                                          &lt;div id="printableArea"&gt;
  
                                                                          &lt;img src="&lt;?php echo base_url('Barcode/barcode_generator')?&gt;/code128a/30/&lt;?php echo $operarios-&gt;codigoean?&gt;/true"/&gt;
                                                                      &lt;!--        &lt;img src="&lt;?php// echo base_url('Barcode/barcode_generator')?&gt;/Code39/40/EDT45SGHJKLE/true"/&gt; --&gt;   
  
                                                                          &lt;/div&gt;
                                                                          &lt;input type="button" onclick="printDiv('printableArea')" value="print" /&gt;
  
  
                                                                          &lt;/td&gt;
  
  
  
                                                                               &lt;td&gt; &lt;?php
  
                                                                               if ((int)$operarios-&gt;status === 1) {
                                                                                      echo '&lt;i class="fa fa-circle 2x" style="color: green;"&gt;';
                                                                                } else {
                                                                                       if ((int)$operarios-&gt;status === 2) {
                                                                                            echo '&lt;i class="fa fa-circle 2x" style="color: red;"&gt;';
                                                                                } else {
                                                                                          if ((int)$operarios-&gt;status === 3) {
                                                                                            echo '&lt;i class="fa fa-circle 2x" style="color: yellow;"&gt;';
                                                                                } else {
                                                                                          if ((int)$operarios-&gt;status === 4) {
                                                                                            echo '&lt;i class="fa fa-circle 2x" style="color: blue;"&gt;';
                                                                                } else {
                                                                                    echo '&lt;i class="fa fa-circle 2x" style="color: black;"&gt;';
                                                                                      }
                                                                              }}}
                                                                                ?&gt;
                                                                          &lt;/td&gt;
                                                                    &lt;/tr&gt;
                                                                    &lt;?php
                                                              }
                                                        }
                                                        ?&gt;
  
                                                  &lt;/tbody&gt;
                                            &lt;/table&gt;
                                      &lt;/div&gt;
                                &lt;/div&gt;
                          &lt;/div&gt;
                    &lt;/div&gt;
              &lt;/div&gt;
        &lt;/section&gt;
  &lt;/div&gt;
  
  &lt;script src="assets/js/jquery/jquery-2.2.3.min.js"&gt;&lt;/script&gt;
  
  
  &lt;script src="assets/js/bootstrap/bootstrap.min.js"&gt;&lt;/script&gt;
  &lt;script src="assets/datatables/jquery.dataTables.min.js"&gt;&lt;/script&gt;
  &lt;script src="assets/datatables/dataTables.bootstrap.min.js"&gt;&lt;/script&gt;
  
  &lt;!-- page script --&gt;
  &lt;script&gt;
        var base_url = '&lt;?php echo base_url() ?&gt;';
        $(document).ready(function () {
  
        });
        $(function () {
              $('#example1').DataTable({
                    "paging": true,
                    "lengthChange": false,
                    "searching": false,
                    "ordering": true,
                    "info": true,
                    "autoWidth": false
              });
        });
        function consultapeca(id){
              $('#idoperario').val(id);
              $('#formulariooperario').submit();
        }
  &lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
  
  
    function printDiv(printableArea)
      {
          Popup($(printableArea).html());
      }
  
      function Popup(data) 
      {
          var mywindow = window.open('', 'my div', 'height=100,width=100');
          mywindow.document.write('&lt;img src="&lt;?php echo base_url('Barcode/barcode_generator')?&gt;/code128a/30/&lt;?php echo $operarios-&gt;codigoean?&gt;/true"/&gt;');
       //   mywindow.document.write('&lt;link rel="stylesheet" href="main.css" type="text/css" /&gt;');
      //    mywindow.document.write('&lt;/head&gt;&lt;body &gt;');
          mywindow.document.write(data);
      //    mywindow.document.write('&lt;/body&gt;&lt;/html&gt;');
  
          mywindow.print();
          mywindow.close();
  
          return true;
      }
  
  
  &lt;/script&gt;
  
  </code></pre>
  
  <pre><code>
  MY controller
  
  &lt;?php
  defined('BASEPATH') OR exit('No direct script access allowed');
  
  class Barcode extends CI_Controller {
  
      public function barcode_print(){
          $this-&gt;load-&gt;view('print_page');
      }
  
      //Barcode Generator
      public function barcode_generator($code_type,$size,$text,$print){
  
          $filepath = "";
          $orientation= "horizontal";
          $sizefactor = "1";
  
          // This function call can be copied into your project and can be made from anywhere in your code
          $this-&gt;barcode_create( $filepath, $text, $size, $orientation, $code_type, $print, $sizefactor );
      }
  
      //Barcode create
      public function barcode_create( $filepath="", $text="0", $size="20", $orientation="horizontal", $code_type="code128", $print=false, $SizeFactor=1 ) {
          $code_string = "";
          // Translate the $text into barcode the correct $code_type
          if ( in_array(strtolower($code_type), array("code128", "code128b")) ) {
              $chksum = 104;
              // Must not change order of array elements as the checksum depends on the array's key to validate final code
              $code_array = array(" "=&gt;"212222","!"=&gt;"222122","\""=&gt;"222221","#"=&gt;"121223","$"=&gt;"121322","%"=&gt;"131222","&amp;"=&gt;"122213","'"=&gt;"122312","("=&gt;"132212",")"=&gt;"221213","*"=&gt;"221312","+"=&gt;"231212",","=&gt;"112232","-"=&gt;"122132","."=&gt;"122231","/"=&gt;"113222","0"=&gt;"123122","1"=&gt;"123221","2"=&gt;"223211","3"=&gt;"221132","4"=&gt;"221231","5"=&gt;"213212","6"=&gt;"223112","7"=&gt;"312131","8"=&gt;"311222","9"=&gt;"321122",":"=&gt;"321221",";"=&gt;"312212","&lt;"=&gt;"322112","="=&gt;"322211","&gt;"=&gt;"212123","?"=&gt;"212321","@"=&gt;"232121","A"=&gt;"111323","B"=&gt;"131123","C"=&gt;"131321","D"=&gt;"112313","E"=&gt;"132113","F"=&gt;"132311","G"=&gt;"211313","H"=&gt;"231113","I"=&gt;"231311","J"=&gt;"112133","K"=&gt;"112331","L"=&gt;"132131","M"=&gt;"113123","N"=&gt;"113321","O"=&gt;"133121","P"=&gt;"313121","Q"=&gt;"211331","R"=&gt;"231131","S"=&gt;"213113","T"=&gt;"213311","U"=&gt;"213131","V"=&gt;"311123","W"=&gt;"311321","X"=&gt;"331121","Y"=&gt;"312113","Z"=&gt;"312311","["=&gt;"332111","\\"=&gt;"314111","]"=&gt;"221411","^"=&gt;"431111","_"=&gt;"111224","\`"=&gt;"111422","a"=&gt;"121124","b"=&gt;"121421","c"=&gt;"141122","d"=&gt;"141221","e"=&gt;"112214","f"=&gt;"112412","g"=&gt;"122114","h"=&gt;"122411","i"=&gt;"142112","j"=&gt;"142211","k"=&gt;"241211","l"=&gt;"221114","m"=&gt;"413111","n"=&gt;"241112","o"=&gt;"134111","p"=&gt;"111242","q"=&gt;"121142","r"=&gt;"121241","s"=&gt;"114212","t"=&gt;"124112","u"=&gt;"124211","v"=&gt;"411212","w"=&gt;"421112","x"=&gt;"421211","y"=&gt;"212141","z"=&gt;"214121","{"=&gt;"412121","|"=&gt;"111143","}"=&gt;"111341","~"=&gt;"131141","DEL"=&gt;"114113","FNC 3"=&gt;"114311","FNC 2"=&gt;"411113","SHIFT"=&gt;"411311","CODE C"=&gt;"113141","FNC 4"=&gt;"114131","CODE A"=&gt;"311141","FNC 1"=&gt;"411131","Start A"=&gt;"211412","Start B"=&gt;"211214","Start C"=&gt;"211232","Stop"=&gt;"2331112");
              $code_keys = array_keys($code_array);
              $code_values = array_flip($code_keys);
              for ( $X = 1; $X &lt;= strlen($text); $X++ ) {
                  $activeKey = substr( $text, ($X-1), 1);
                  $code_string .= $code_array[$activeKey];
                  $chksum=($chksum + ($code_values[$activeKey] * $X));
              }
              $code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]];
  
              $code_string = "211214" . $code_string . "2331112";
          } elseif ( strtolower($code_type) == "code128a" ) {
              $chksum = 103;
              $text = strtoupper($text); // Code 128A doesn't support lower case
              // Must not change order of array elements as the checksum depends on the array's key to validate final code
              $code_array = array(" "=&gt;"212222","!"=&gt;"222122","\""=&gt;"222221","#"=&gt;"121223","$"=&gt;"121322","%"=&gt;"131222","&amp;"=&gt;"122213","'"=&gt;"122312","("=&gt;"132212",")"=&gt;"221213","*"=&gt;"221312","+"=&gt;"231212",","=&gt;"112232","-"=&gt;"122132","."=&gt;"122231","/"=&gt;"113222","0"=&gt;"123122","1"=&gt;"123221","2"=&gt;"223211","3"=&gt;"221132","4"=&gt;"221231","5"=&gt;"213212","6"=&gt;"223112","7"=&gt;"312131","8"=&gt;"311222","9"=&gt;"321122",":"=&gt;"321221",";"=&gt;"312212","&lt;"=&gt;"322112","="=&gt;"322211","&gt;"=&gt;"212123","?"=&gt;"212321","@"=&gt;"232121","A"=&gt;"111323","B"=&gt;"131123","C"=&gt;"131321","D"=&gt;"112313","E"=&gt;"132113","F"=&gt;"132311","G"=&gt;"211313","H"=&gt;"231113","I"=&gt;"231311","J"=&gt;"112133","K"=&gt;"112331","L"=&gt;"132131","M"=&gt;"113123","N"=&gt;"113321","O"=&gt;"133121","P"=&gt;"313121","Q"=&gt;"211331","R"=&gt;"231131","S"=&gt;"213113","T"=&gt;"213311","U"=&gt;"213131","V"=&gt;"311123","W"=&gt;"311321","X"=&gt;"331121","Y"=&gt;"312113","Z"=&gt;"312311","["=&gt;"332111","\\"=&gt;"314111","]"=&gt;"221411","^"=&gt;"431111","_"=&gt;"111224","NUL"=&gt;"111422","SOH"=&gt;"121124","STX"=&gt;"121421","ETX"=&gt;"141122","EOT"=&gt;"141221","ENQ"=&gt;"112214","ACK"=&gt;"112412","BEL"=&gt;"122114","BS"=&gt;"122411","HT"=&gt;"142112","LF"=&gt;"142211","VT"=&gt;"241211","FF"=&gt;"221114","CR"=&gt;"413111","SO"=&gt;"241112","SI"=&gt;"134111","DLE"=&gt;"111242","DC1"=&gt;"121142","DC2"=&gt;"121241","DC3"=&gt;"114212","DC4"=&gt;"124112","NAK"=&gt;"124211","SYN"=&gt;"411212","ETB"=&gt;"421112","CAN"=&gt;"421211","EM"=&gt;"212141","SUB"=&gt;"214121","ESC"=&gt;"412121","FS"=&gt;"111143","GS"=&gt;"111341","RS"=&gt;"131141","US"=&gt;"114113","FNC 3"=&gt;"114311","FNC 2"=&gt;"411113","SHIFT"=&gt;"411311","CODE C"=&gt;"113141","CODE B"=&gt;"114131","FNC 4"=&gt;"311141","FNC 1"=&gt;"411131","Start A"=&gt;"211412","Start B"=&gt;"211214","Start C"=&gt;"211232","Stop"=&gt;"2331112");
              $code_keys = array_keys($code_array);
              $code_values = array_flip($code_keys);
              for ( $X = 1; $X &lt;= strlen($text); $X++ ) {
                  $activeKey = substr( $text, ($X-1), 1);
                  $code_string .= $code_array[$activeKey];
                  $chksum=($chksum + ($code_values[$activeKey] * $X));
              }
              $code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]];
  
              $code_string = "211412" . $code_string . "2331112";
          } elseif ( strtolower($code_type) == "code39" ) {
              $code_array = array("0"=&gt;"111221211","1"=&gt;"211211112","2"=&gt;"112211112","3"=&gt;"212211111","4"=&gt;"111221112","5"=&gt;"211221111","6"=&gt;"112221111","7"=&gt;"111211212","8"=&gt;"211211211","9"=&gt;"112211211","A"=&gt;"211112112","B"=&gt;"112112112","C"=&gt;"212112111","D"=&gt;"111122112","E"=&gt;"211122111","F"=&gt;"112122111","G"=&gt;"111112212","H"=&gt;"211112211","I"=&gt;"112112211","J"=&gt;"111122211","K"=&gt;"211111122","L"=&gt;"112111122","M"=&gt;"212111121","N"=&gt;"111121122","O"=&gt;"211121121","P"=&gt;"112121121","Q"=&gt;"111111222","R"=&gt;"211111221","S"=&gt;"112111221","T"=&gt;"111121221","U"=&gt;"221111112","V"=&gt;"122111112","W"=&gt;"222111111","X"=&gt;"121121112","Y"=&gt;"221121111","Z"=&gt;"122121111","-"=&gt;"121111212","."=&gt;"221111211"," "=&gt;"122111211","$"=&gt;"121212111","/"=&gt;"121211121","+"=&gt;"121112121","%"=&gt;"111212121","*"=&gt;"121121211");
  
              // Convert to uppercase
              $upper_text = strtoupper($text);
  
              for ( $X = 1; $X&lt;=strlen($upper_text); $X++ ) {
                  $code_string .= $code_array[substr( $upper_text, ($X-1), 1)] . "1";
              }
  
              $code_string = "1211212111" . $code_string . "121121211";
          } elseif ( strtolower($code_type) == "code25" ) {
              $code_array1 = array("1","2","3","4","5","6","7","8","9","0");
              $code_array2 = array("3-1-1-1-3","1-3-1-1-3","3-3-1-1-1","1-1-3-1-3","3-1-3-1-1","1-3-3-1-1","1-1-1-3-3","3-1-1-3-1","1-3-1-3-1","1-1-3-3-1");
  
              for ( $X = 1; $X &lt;= strlen($text); $X++ ) {
                  for ( $Y = 0; $Y &lt; count($code_array1); $Y++ ) {
                      if ( substr($text, ($X-1), 1) == $code_array1[$Y] )
                          $temp[$X] = $code_array2[$Y];
                  }
              }
  
              for ( $X=1; $X&lt;=strlen($text); $X+=2 ) {
                  if ( isset($temp[$X]) &amp;&amp; isset($temp[($X + 1)]) ) {
                      $temp1 = explode( "-", $temp[$X] );
                      $temp2 = explode( "-", $temp[($X + 1)] );
                      for ( $Y = 0; $Y &lt; count($temp1); $Y++ )
                          $code_string .= $temp1[$Y] . $temp2[$Y];
                  }
              }
  
              $code_string = "1111" . $code_string . "311";
          } elseif ( strtolower($code_type) == "codabar" ) {
              $code_array1 = array("1","2","3","4","5","6","7","8","9","0","-","$",":","/",".","+","A","B","C","D");
              $code_array2 = array("1111221","1112112","2211111","1121121","2111121","1211112","1211211","1221111","2112111","1111122","1112211","1122111","2111212","2121112","2121211","1121212","1122121","1212112","1112122","1112221");
  
              // Convert to uppercase
              $upper_text = strtoupper($text);
  
              for ( $X = 1; $X&lt;=strlen($upper_text); $X++ ) {
                  for ( $Y = 0; $Y&lt;count($code_array1); $Y++ ) {
                      if ( substr($upper_text, ($X-1), 1) == $code_array1[$Y] )
                          $code_string .= $code_array2[$Y] . "1";
                  }
              }
              $code_string = "11221211" . $code_string . "1122121";
          }
  
          // Pad the edges of the barcode
          $code_length = 20;
          if ($print) {
              $text_height = 30;
          } else {
              $text_height = 0;
          }
  
          for ( $i=1; $i &lt;= strlen($code_string); $i++ ){
              $code_length = $code_length + (integer)(substr($code_string,($i-1),1));
              }
  
          if ( strtolower($orientation) == "horizontal" ) {
              $img_width = $code_length*$SizeFactor;
              $img_height = $size;
          } else {
              $img_width = $size;
              $img_height = $code_length*$SizeFactor;
          }
  
          $image = imagecreate($img_width, $img_height + $text_height);
          $black = imagecolorallocate ($image, 0, 0, 0);
          $white = imagecolorallocate ($image, 255, 255, 255);
  
          imagefill( $image, 0, 0, $white );
          if ( $print ) {
              imagestring($image, 5, 31, $img_height, $text, $black );
          }
  
          $location = 10;
          for ( $position = 1 ; $position &lt;= strlen($code_string); $position++ ) {
              $cur_size = $location + ( substr($code_string, ($position-1), 1) );
              if ( strtolower($orientation) == "horizontal" )
                  imagefilledrectangle( $image, $location*$SizeFactor, 0, $cur_size*$SizeFactor, $img_height, ($position % 2 == 0 ? $white : $black) );
              else
                  imagefilledrectangle( $image, 0, $location*$SizeFactor, $img_width, $cur_size*$SizeFactor, ($position % 2 == 0 ? $white : $black) );
              $location = $cur_size;
          }
  
          // Draw barcode to the screen or save in a file
          if ( $filepath=="" ) {
              header ('Content-type: image/png');
              imagepng($image);
              imagedestroy($image);
          } else {
              imagepng($image,$filepath);
              imagedestroy($image);       
          }
      }
  }
  
      ```
  </code></pre>