barcode.lib.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. /* Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2004-2010 Folke Ashberg: Some lines of code were inspired from work
  4. * of Folke Ashberg into PHP-Barcode 0.3pl2, available as GPL
  5. * source code at http://www.ashberg.de/bar.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/core/lib/barcode.lib.php
  22. * \brief Set of functions used for barcode generation
  23. * \ingroup core
  24. */
  25. /* ******************************************************************** */
  26. /* COLORS */
  27. /* ******************************************************************** */
  28. $bar_color=Array(0,0,0);
  29. $bg_color=Array(255,255,255);
  30. $text_color=Array(0,0,0);
  31. /* ******************************************************************** */
  32. /* FONT FILE */
  33. /* ******************************************************************** */
  34. if (defined('DOL_DEFAULT_TTF_BOLD')) $font_loc=constant('DOL_DEFAULT_TTF_BOLD');
  35. // Automatic-Detection of Font if running Windows
  36. // DOL_CHANGE LDR
  37. if (isset($_SERVER['WINDIR']) && @file_exists($_SERVER['WINDIR'])) $font_loc=$_SERVER['WINDIR'].'\Fonts\arialbd.ttf';
  38. if (empty($font_loc)) die('DOL_DEFAULT_TTF_BOLD must de defined with full path to a TTF font.');
  39. /* ******************************************************************** */
  40. /* GENBARCODE */
  41. /* ******************************************************************** */
  42. /* location of 'genbarcode'
  43. * leave blank if you don't have them :(
  44. * genbarcode is needed to render encodings other than EAN-12/EAN-13/ISBN
  45. */
  46. if (defined('PHP-BARCODE_PATH_COMMAND')) $genbarcode_loc=constant('PHP-BARCODE_PATH_COMMAND');
  47. else $genbarcode_loc = $conf->global->GENBARCODE_LOCATION;
  48. /**
  49. * Print barcode
  50. *
  51. * @param string $code Code
  52. * @param string $encoding Encoding
  53. * @param integer $scale Scale
  54. * @param string $mode 'png' or 'jpg' ...
  55. * @return array|string $bars array('encoding': the encoding which has been used, 'bars': the bars, 'text': text-positioning info) or string with error message
  56. */
  57. function barcode_print($code, $encoding="ANY", $scale = 2 ,$mode = "png")
  58. {
  59. dol_syslog("barcode.lib.php::barcode_print $code $encoding $scale $mode");
  60. $bars=barcode_encode($code,$encoding);
  61. if (! $bars || ! empty($bars['error']))
  62. {
  63. // Return error message instead of array
  64. if (empty($bars['error'])) $error='Bad Value '.$code.' for encoding '.$encoding;
  65. else $error=$bars['error'];
  66. dol_syslog('barcode.lib.php::barcode_print '.$error, LOG_ERR);
  67. return $error;
  68. }
  69. if (! $mode) $mode="png";
  70. //if (preg_match("/^(text|txt|plain)$/i",$mode)) print barcode_outtext($bars['text'],$bars['bars']);
  71. //elseif (preg_match("/^(html|htm)$/i",$mode)) print barcode_outhtml($bars['text'],$bars['bars'], $scale,0, 0);
  72. //else
  73. barcode_outimage($bars['text'], $bars['bars'], $scale, $mode);
  74. return $bars;
  75. }
  76. /**
  77. * Encodes $code with $encoding using genbarcode OR built-in encoder if you don't have genbarcode only EAN-13/ISBN is possible
  78. *
  79. * You can use the following encodings (when you have genbarcode):
  80. * ANY choose best-fit (default)
  81. * EAN 8 or 13 EAN-Code
  82. * UPC 12-digit EAN
  83. * ISBN isbn numbers (still EAN-13)
  84. * 39 code 39
  85. * 128 code 128 (a,b,c: autoselection)
  86. * 128C code 128 (compact form for digits)
  87. * 128B code 128, full printable ascii
  88. * I25 interleaved 2 of 5 (only digits)
  89. * 128RAW Raw code 128 (by Leonid A. Broukhis)
  90. * CBR Codabar (by Leonid A. Broukhis)
  91. * MSI MSI (by Leonid A. Broukhis)
  92. * PLS Plessey (by Leonid A. Broukhis)
  93. *
  94. * @param string $code Code
  95. * @param string $encoding Encoding
  96. * @return array array('encoding': the encoding which has been used, 'bars': the bars, 'text': text-positioning info)
  97. */
  98. function barcode_encode($code,$encoding)
  99. {
  100. global $genbarcode_loc;
  101. if (
  102. (preg_match("/^ean$/i", $encoding))
  103. || (($encoding) && (preg_match("/^isbn$/i", $encoding))
  104. && (( strlen($code)==9 || strlen($code)==10) ||
  105. (((preg_match("/^978/", $code) && strlen($code)==12) ||
  106. (strlen($code)==13)))))
  107. || (( !isset($encoding) || !$encoding || (preg_match("/^ANY$/i", $encoding) ))
  108. && (preg_match("/^[0-9]{12,13}$/", $code)))
  109. )
  110. {
  111. /* use built-in EAN-Encoder */
  112. dol_syslog("barcode.lib.php::barcode_encode Use barcode_encode_ean");
  113. $bars=barcode_encode_ean($code, $encoding);
  114. }
  115. else if (file_exists($genbarcode_loc)) // For example C39
  116. {
  117. /* use genbarcode */
  118. dol_syslog("barcode.lib.php::barcode_encode Use genbarcode ".$genbarcode_loc." code=".$code." encoding=".$encoding);
  119. $bars=barcode_encode_genbarcode($code, $encoding);
  120. }
  121. else
  122. {
  123. print "barcode_encode needs an external programm for encodings other then EAN/ISBN (code=".$code.", encoding=".$encoding.")<BR>\n";
  124. print "<UL>\n";
  125. print "<LI>download gnu-barcode from <A href=\"http://www.gnu.org/software/barcode/\">www.gnu.org/software/barcode/</A>\n";
  126. print "<LI>compile and install them\n";
  127. print "<LI>download genbarcode from <A href=\"http://www.ashberg.de/bar/\">www.ashberg.de/bar/</A>\n";
  128. print "<LI>compile and install them\n";
  129. print "<LI>specify path the genbarcode in barcode module setup\n";
  130. print "</UL>\n";
  131. print "<BR>\n";
  132. return false;
  133. }
  134. return $bars;
  135. }
  136. /**
  137. * Calculate EAN sum
  138. *
  139. * @param string $ean EAN to encode
  140. * @return integer Sum
  141. */
  142. function barcode_gen_ean_sum($ean)
  143. {
  144. $even=true; $esum=0; $osum=0;
  145. $ln=strlen($ean)-1;
  146. for ($i=$ln; $i>=0; $i--)
  147. {
  148. if ($even) $esum+=$ean[$i]; else $osum+=$ean[$i];
  149. $even=!$even;
  150. }
  151. return (10-((3*$esum+$osum)%10))%10;
  152. }
  153. /**
  154. * Encode EAN
  155. *
  156. * @param string $ean Code
  157. * @param string $encoding Encoding
  158. * @return array array('encoding': the encoding which has been used, 'bars': the bars, 'text': text-positioning info, 'error': error message if error)
  159. */
  160. function barcode_encode_ean($ean, $encoding = "EAN-13")
  161. {
  162. $digits=array(3211,2221,2122,1411,1132,1231,1114,1312,1213,3112);
  163. $mirror=array("000000","001011","001101","001110","010011","011001","011100","010101","010110","011010");
  164. $guards=array("9a1a","1a1a1","a1a");
  165. $ean=trim($ean);
  166. if (preg_match("/[^0-9]/i",$ean))
  167. {
  168. return array("error"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (not a numeric)", "text"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (not a numeric)");
  169. }
  170. $encoding=strtoupper($encoding);
  171. if ($encoding=="ISBN")
  172. {
  173. if (!preg_match("/^978/", $ean)) $ean="978".$ean;
  174. }
  175. if (preg_match("/^978/", $ean)) $encoding="ISBN";
  176. if (strlen($ean)<12 || strlen($ean)>13)
  177. {
  178. return array("error"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (must have 12/13 numbers)", "text"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (must have 12/13 numbers)");
  179. }
  180. $ean=substr($ean,0,12);
  181. $eansum=barcode_gen_ean_sum($ean);
  182. $ean.=$eansum;
  183. $line=$guards[0];
  184. for ($i=1;$i<13;$i++)
  185. {
  186. $str=$digits[$ean[$i]];
  187. if ($i<7 && $mirror[$ean[0]][$i-1]==1) $line.=strrev($str); else $line.=$str;
  188. if ($i==6) $line.=$guards[1];
  189. }
  190. $line.=$guards[2];
  191. /* create text */
  192. $pos=0;
  193. $text="";
  194. for ($a=0;$a<13;$a++)
  195. {
  196. if ($a>0) $text.=" ";
  197. $text.="$pos:12:{$ean[$a]}";
  198. if ($a==0) $pos+=12;
  199. else if ($a==6) $pos+=12;
  200. else $pos+=7;
  201. }
  202. return array(
  203. "error" => '',
  204. "encoding" => $encoding,
  205. "bars" => $line,
  206. "text" => $text
  207. );
  208. }
  209. /**
  210. * Encode result of genbarcode command
  211. *
  212. * @param string $code Code
  213. * @param string $encoding Encoding
  214. * @return array array('encoding': the encoding which has been used, 'bars': the bars, 'text': text-positioning info)
  215. */
  216. function barcode_encode_genbarcode($code,$encoding)
  217. {
  218. global $genbarcode_loc;
  219. // Clean parameters
  220. if (preg_match("/^ean$/i", $encoding) && strlen($code)==13) $code=substr($code,0,12);
  221. if (!$encoding) $encoding="ANY";
  222. $encoding=preg_replace("/[\\\|]/", "_", $encoding);
  223. $code=preg_replace("/[\\\|]/", "_", $code);
  224. $command=escapeshellarg($genbarcode_loc);
  225. //$paramclear=" \"".str_replace("\"", "\\\"",$code)."\" \"".str_replace("\"", "\\\"",strtoupper($encoding))."\"";
  226. $paramclear=" ".escapeshellarg($code)." ".escapeshellarg(strtoupper($encoding));
  227. $fullcommandclear=$command." ".$paramclear." 2>&1";
  228. //print $fullcommandclear."<br>\n";exit;
  229. dol_syslog("Run command ".$fullcommandclear);
  230. $fp=popen($fullcommandclear, "r");
  231. if ($fp)
  232. {
  233. $bars=fgets($fp, 1024);
  234. $text=fgets($fp, 1024);
  235. $encoding=fgets($fp, 1024);
  236. pclose($fp);
  237. }
  238. else
  239. {
  240. dol_syslog("barcode.lib.php::barcode_encode_genbarcode failed to run popen ".$fullcommandclear, LOG_ERR);
  241. return false;
  242. }
  243. //var_dump($bars);
  244. $ret=array(
  245. "bars" => trim($bars),
  246. "text" => trim($text),
  247. "encoding" => trim($encoding),
  248. "error" => ""
  249. );
  250. //var_dump($ret);
  251. if (preg_match('/permission denied/i',$ret['bars']))
  252. {
  253. $ret['error']=$ret['bars']; $ret['bars']='';
  254. return $ret;
  255. }
  256. if (!$ret['bars']) return false;
  257. if (!$ret['text']) return false;
  258. if (!$ret['encoding']) return false;
  259. return $ret;
  260. }
  261. /**
  262. * Output image onto standard output, or onto disk if global filebarcode is defined
  263. *
  264. * @param string $text the text-line (<position>:<font-size>:<character> ...)
  265. * @param string $bars where to place the bars (<space-width><bar-width><space-width><bar-width>...)
  266. * @param int $scale scale factor ( 1 < scale < unlimited (scale 50 will produce 5400x300 pixels when using EAN-13!!!))
  267. * @param string $mode png,gif,jpg (default='png')
  268. * @param int $total_y the total height of the image ( default: scale * 60 )
  269. * @param array $space default: $space[top] = 2 * $scale; $space[bottom]= 2 * $scale; $space[left] = 2 * $scale; $space[right] = 2 * $scale;
  270. * @return string|null
  271. */
  272. function barcode_outimage($text, $bars, $scale = 1, $mode = "png", $total_y = 0, $space = '')
  273. {
  274. global $bar_color, $bg_color, $text_color;
  275. global $font_loc, $filebarcode;
  276. //print "$text, $bars, $scale, $mode, $total_y, $space, $font_loc, $filebarcode<br>";
  277. //var_dump($text);
  278. //var_dump($bars);
  279. //var_dump($font_loc);
  280. /* set defaults */
  281. if ($scale<1) $scale=2;
  282. $total_y=(int) $total_y;
  283. if ($total_y<1) $total_y=(int) $scale * 60;
  284. if (!$space)
  285. $space=array('top'=>2*$scale,'bottom'=>2*$scale,'left'=>2*$scale,'right'=>2*$scale);
  286. /* count total width */
  287. $xpos=0;
  288. $width=true;
  289. $ln=strlen($bars);
  290. for ($i=0; $i<$ln; $i++)
  291. {
  292. $val=strtolower($bars[$i]);
  293. if ($width)
  294. {
  295. $xpos+=$val*$scale;
  296. $width=false;
  297. continue;
  298. }
  299. if (preg_match("/[a-z]/", $val))
  300. {
  301. /* tall bar */
  302. $val=ord($val)-ord('a')+1;
  303. }
  304. $xpos+=$val*$scale;
  305. $width=true;
  306. }
  307. /* allocate the image */
  308. $total_x=( $xpos )+$space['right']+$space['right'];
  309. $xpos=$space['left'];
  310. if (! function_exists("imagecreate"))
  311. {
  312. print "You don't have the gd2 extension enabled<br>\n";
  313. return "";
  314. }
  315. $im=imagecreate($total_x, $total_y);
  316. /* create two images */
  317. $col_bg=ImageColorAllocate($im,$bg_color[0],$bg_color[1],$bg_color[2]);
  318. $col_bar=ImageColorAllocate($im,$bar_color[0],$bar_color[1],$bar_color[2]);
  319. $col_text=ImageColorAllocate($im,$text_color[0],$text_color[1],$text_color[2]);
  320. $height=round($total_y-($scale*10));
  321. $height2=round($total_y-$space['bottom']);
  322. /* paint the bars */
  323. $width=true;
  324. $ln=strlen($bars);
  325. for ($i=0; $i<$ln; $i++)
  326. {
  327. $val=strtolower($bars[$i]);
  328. if ($width)
  329. {
  330. $xpos+=$val*$scale;
  331. $width=false;
  332. continue;
  333. }
  334. if (preg_match("/[a-z]/", $val))
  335. {
  336. /* tall bar */
  337. $val=ord($val)-ord('a')+1;
  338. $h=$height2;
  339. } else $h=$height;
  340. imagefilledrectangle($im, $xpos, $space['top'], $xpos+($val*$scale)-1, $h, $col_bar);
  341. $xpos+=$val*$scale;
  342. $width=true;
  343. }
  344. $chars=explode(" ", $text);
  345. reset($chars);
  346. while (list($n, $v)=each($chars))
  347. {
  348. if (trim($v))
  349. {
  350. $inf=explode(":", $v);
  351. $fontsize=$scale*($inf[1]/1.8);
  352. $fontheight=$total_y-($fontsize/2.7)+2;
  353. imagettftext($im, $fontsize, 0, $space['left']+($scale*$inf[0])+2, $fontheight, $col_text, $font_loc, $inf[2]);
  354. }
  355. }
  356. /* output the image */
  357. $mode=strtolower($mode);
  358. if ($mode=='jpg' || $mode=='jpeg')
  359. {
  360. header("Content-Type: image/jpeg; name=\"barcode.jpg\"");
  361. imagejpeg($im);
  362. }
  363. else if ($mode=='gif')
  364. {
  365. header("Content-Type: image/gif; name=\"barcode.gif\"");
  366. imagegif($im);
  367. }
  368. else if (! empty($filebarcode)) // To wxrite into afile onto disk
  369. {
  370. imagepng($im,$filebarcode);
  371. }
  372. else
  373. {
  374. header("Content-Type: image/png; name=\"barcode.png\"");
  375. imagepng($im);
  376. }
  377. }