2007-09-28 00:24:43 +02:00
< ? php
2016-02-08 16:42:34 +01:00
/* Copyright ( C ) 2004 - 2016 Laurent Destailleur < eldy @ users . sourceforge . net >
2024-02-29 15:26:53 +01:00
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2012-01-08 01:59:33 +01:00
* Copyright ( C ) 2004 - 2010 Folke Ashberg : Some lines of code were inspired from work
2024-03-18 22:59:11 +01:00
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
2012-01-08 01:59:33 +01:00
* of Folke Ashberg into PHP - Barcode 0.3 pl2 , available as GPL
* source code at http :// www . ashberg . de / bar .
2011-08-03 03:13:30 +02:00
*
2007-09-28 00:24:43 +02:00
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2007-09-28 00:24:43 +02:00
* ( at your option ) any later version .
2011-08-03 03:13:30 +02:00
*
2007-09-28 00:24:43 +02:00
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
2011-08-03 03:13:30 +02:00
*
2007-09-28 00:24:43 +02:00
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2007-09-28 00:24:43 +02:00
*/
2012-01-08 01:59:33 +01:00
/**
2012-04-16 12:01:32 +02:00
* \file htdocs / core / lib / barcode . lib . php
2021-08-17 13:08:03 +02:00
* \brief Set of functions used for barcode generation ( internal lib , also code 'phpbarcode' )
2012-01-08 01:59:33 +01:00
* \ingroup core
*/
2007-09-28 01:57:09 +02:00
2007-09-28 00:24:43 +02:00
/* ******************************************************************** */
/* COLORS */
/* ******************************************************************** */
2020-04-10 10:59:32 +02:00
$bar_color = array ( 0 , 0 , 0 );
$bg_color = array ( 255 , 255 , 255 );
$text_color = array ( 0 , 0 , 0 );
2007-09-28 00:24:43 +02:00
/* ******************************************************************** */
/* FONT FILE */
/* ******************************************************************** */
2021-02-23 22:03:23 +01:00
if ( defined ( 'DOL_DEFAULT_TTF_BOLD' )) {
$font_loc = constant ( 'DOL_DEFAULT_TTF_BOLD' );
}
2011-11-27 15:33:18 +01:00
// Automatic-Detection of Font if running Windows
2017-07-17 20:09:19 +02:00
// @CHANGE LDR
2021-02-23 22:03:23 +01:00
if ( isset ( $_SERVER [ 'WINDIR' ]) && @ file_exists ( $_SERVER [ 'WINDIR' ])) {
$font_loc = $_SERVER [ 'WINDIR' ] . '\Fonts\arialbd.ttf' ;
}
if ( empty ( $font_loc )) {
die ( 'DOL_DEFAULT_TTF_BOLD must de defined with full path to a TTF font.' );
}
2011-08-14 19:17:19 +02:00
2007-09-28 00:24:43 +02:00
/* ******************************************************************** */
/* GENBARCODE */
/* ******************************************************************** */
/* location of 'genbarcode'
* leave blank if you don ' t have them : (
2011-11-27 15:33:18 +01:00
* genbarcode is needed to render encodings other than EAN - 12 / EAN - 13 / ISBN
*/
2011-08-14 19:17:19 +02:00
2021-02-23 22:03:23 +01:00
if ( defined ( 'PHP-BARCODE_PATH_COMMAND' )) {
$genbarcode_loc = constant ( 'PHP-BARCODE_PATH_COMMAND' );
} else {
2021-09-28 12:56:33 +02:00
$genbarcode_loc = '' ;
2023-11-27 11:39:32 +01:00
if ( getDolGlobalString ( 'GENBARCODE_LOCATION' )) {
2024-01-05 04:18:53 +01:00
$genbarcode_loc = getDolGlobalString ( 'GENBARCODE_LOCATION' );
2021-09-25 18:15:44 +02:00
}
2021-02-23 22:03:23 +01:00
}
2007-09-28 00:24:43 +02:00
2011-11-28 23:07:19 +01:00
/**
2012-02-04 14:39:47 +01:00
* Print barcode
2011-11-28 23:07:19 +01:00
*
2016-02-08 16:42:34 +01:00
* @ param string $code Code
2021-08-17 13:08:03 +02:00
* @ param string $encoding Encoding ( 'EAN13' , 'ISBN' , 'C128' , 'UPC' , 'CBR' , 'QRCODE' , 'DATAMATRIX' , 'ANY' ... )
2016-02-08 16:42:34 +01:00
* @ param integer $scale Scale
* @ param string $mode 'png' or 'jpg' ...
* @ return array | string $bars array ( 'encoding' : the encoding which has been used , 'bars' : the bars , 'text' : text - positioning info ) or string with error message
2011-11-28 23:07:19 +01:00
*/
2019-01-27 15:20:16 +01:00
function barcode_print ( $code , $encoding = " ANY " , $scale = 2 , $mode = " png " )
2011-11-28 23:07:19 +01:00
{
2020-10-31 14:32:18 +01:00
dol_syslog ( " barcode.lib.php::barcode_print $code $encoding $scale $mode " );
$bars = barcode_encode ( $code , $encoding );
2021-02-23 22:03:23 +01:00
if ( ! $bars || ! empty ( $bars [ 'error' ])) {
2020-10-31 14:32:18 +01:00
// Return error message instead of array
2021-02-23 22:03:23 +01:00
if ( empty ( $bars [ 'error' ])) {
$error = 'Bad Value ' . $code . ' for encoding ' . $encoding ;
} else {
$error = $bars [ 'error' ];
}
2020-10-31 14:32:18 +01:00
dol_syslog ( 'barcode.lib.php::barcode_print ' . $error , LOG_ERR );
return $error ;
}
2021-02-23 22:03:23 +01:00
if ( ! $mode ) {
$mode = " png " ;
}
2020-10-31 14:32:18 +01:00
//if (preg_match("/^(text|txt|plain)$/i",$mode)) print barcode_outtext($bars['text'],$bars['bars']);
//elseif (preg_match("/^(html|htm)$/i",$mode)) print barcode_outhtml($bars['text'],$bars['bars'], $scale,0, 0);
//else
barcode_outimage ( $bars [ 'text' ], $bars [ 'bars' ], $scale , $mode );
return $bars ;
2011-11-28 23:07:19 +01:00
}
/**
2021-03-12 13:54:27 +01:00
* Encodes $code with $encoding using genbarcode OR built - in encoder if you don ' t have genbarcode only EAN - 13 / ISBN or UPC is possible
2011-11-28 23:07:19 +01:00
*
* You can use the following encodings ( when you have genbarcode ) :
* ANY choose best - fit ( default )
* EAN 8 or 13 EAN - Code
* UPC 12 - digit EAN
* ISBN isbn numbers ( still EAN - 13 )
* 39 code 39
* 128 code 128 ( a , b , c : autoselection )
* 128 C code 128 ( compact form for digits )
* 128 B code 128 , full printable ascii
* I25 interleaved 2 of 5 ( only digits )
* 128 RAW Raw code 128 ( by Leonid A . Broukhis )
* CBR Codabar ( by Leonid A . Broukhis )
* MSI MSI ( by Leonid A . Broukhis )
* PLS Plessey ( by Leonid A . Broukhis )
*
2012-02-04 14:39:47 +01:00
* @ param string $code Code
* @ param string $encoding Encoding
2019-02-13 22:23:14 +01:00
* @ return array | false array ( 'encoding' : the encoding which has been used , 'bars' : the bars , 'text' : text - positioning info )
2011-11-28 23:07:19 +01:00
*/
2019-01-27 15:20:16 +01:00
function barcode_encode ( $code , $encoding )
2011-11-28 23:07:19 +01:00
{
2020-10-31 14:32:18 +01:00
global $genbarcode_loc ;
2021-03-12 13:54:27 +01:00
if (( preg_match ( " /^upc $ /i " , $encoding ))
&& ( preg_match ( " /^[0-9] { 11,12} $ / " , $code ))
) {
/* use built-in UPC-Encoder */
dol_syslog ( " barcode.lib.php::barcode_encode Use barcode_encode_upc " );
$bars = barcode_encode_upc ( $code , $encoding );
} elseif (( preg_match ( " /^ean $ /i " , $encoding ))
2020-10-31 14:32:18 +01:00
|| (( $encoding ) && ( preg_match ( " /^isbn $ /i " , $encoding ))
&& (( strlen ( $code ) == 9 || strlen ( $code ) == 10 ) ||
((( preg_match ( " /^978/ " , $code ) && strlen ( $code ) == 12 ) ||
( strlen ( $code ) == 13 )))))
|| (( ! isset ( $encoding ) || ! $encoding || ( preg_match ( " /^ANY $ /i " , $encoding )))
&& ( preg_match ( " /^[0-9] { 12,13} $ / " , $code )))
2021-02-23 22:03:23 +01:00
) {
2020-10-31 14:32:18 +01:00
/* use built-in EAN-Encoder */
dol_syslog ( " barcode.lib.php::barcode_encode Use barcode_encode_ean " );
$bars = barcode_encode_ean ( $code , $encoding );
2021-02-23 22:03:23 +01:00
} elseif ( file_exists ( $genbarcode_loc )) { // For example C39
2020-10-31 14:32:18 +01:00
/* use genbarcode */
dol_syslog ( " barcode.lib.php::barcode_encode Use genbarcode " . $genbarcode_loc . " code= " . $code . " encoding= " . $encoding );
$bars = barcode_encode_genbarcode ( $code , $encoding );
} else {
2021-08-17 13:08:03 +02:00
print " barcode_encode needs an external program for encodings other then EAN/ISBN (code= " . dol_escape_htmltag ( $code ) . " , encoding= " . dol_escape_htmltag ( $encoding ) . " )<BR> \n " ;
2020-10-31 14:32:18 +01:00
print " <UL> \n " ;
print " <LI>download gnu-barcode from <A href= \" https://www.gnu.org/software/barcode/ \" >www.gnu.org/software/barcode/</A> \n " ;
print " <LI>compile and install them \n " ;
print " <LI>specify path the genbarcode in barcode module setup \n " ;
print " </UL> \n " ;
print " <BR> \n " ;
return false ;
}
return $bars ;
2011-11-28 23:07:19 +01:00
}
2007-09-28 00:24:43 +02:00
2011-11-27 15:33:18 +01:00
/**
2012-02-04 14:39:47 +01:00
* Calculate EAN sum
2011-11-27 15:33:18 +01:00
*
2012-02-04 14:39:47 +01:00
* @ param string $ean EAN to encode
2015-04-06 12:25:30 +02:00
* @ return integer Sum
2011-11-27 15:33:18 +01:00
*/
function barcode_gen_ean_sum ( $ean )
{
2021-03-01 20:37:16 +01:00
$even = true ;
$esum = 0 ;
$osum = 0 ;
2020-10-31 14:32:18 +01:00
$ln = strlen ( $ean ) - 1 ;
2021-02-23 22:03:23 +01:00
for ( $i = $ln ; $i >= 0 ; $i -- ) {
if ( $even ) {
$esum += $ean [ $i ];
} else {
$osum += $ean [ $i ];
}
2020-10-31 14:32:18 +01:00
$even = ! $even ;
}
return ( 10 - (( 3 * $esum + $osum ) % 10 )) % 10 ;
2011-11-27 15:33:18 +01:00
}
2021-03-12 13:11:17 +01:00
/**
* Generate EAN bars
*
* @ param string $ean EAN to encode
* @ return string Encoded EAN
*/
function barcode_gen_ean_bars ( $ean )
{
2024-03-18 22:59:11 +01:00
$digits = array ( '3211' , '2221' , '2122' , '1411' , '1132' , '1231' , '1114' , '1312' , '1213' , '3112' );
2021-03-12 13:11:17 +01:00
$mirror = array ( " 000000 " , " 001011 " , " 001101 " , " 001110 " , " 010011 " , " 011001 " , " 011100 " , " 010101 " , " 010110 " , " 011010 " );
2021-03-12 13:54:27 +01:00
$guards = array ( " 9a1a " , " 1a1a1 " , " a1a7 " );
2021-03-12 13:11:17 +01:00
$line = $guards [ 0 ];
for ( $i = 1 ; $i < 13 ; $i ++ ) {
$str = $digits [ $ean [ $i ]];
if ( $i < 7 && $mirror [ $ean [ 0 ]][ $i - 1 ] == 1 ) {
$line .= strrev ( $str );
} else {
$line .= $str ;
}
if ( $i == 6 ) {
$line .= $guards [ 1 ];
}
}
$line .= $guards [ 2 ];
return $line ;
}
2011-11-27 15:33:18 +01:00
/**
2012-02-04 14:39:47 +01:00
* Encode EAN
2011-11-27 15:33:18 +01:00
*
2012-02-04 14:39:47 +01:00
* @ param string $ean Code
* @ param string $encoding Encoding
2016-02-08 15:49:38 +01:00
* @ return array array ( 'encoding' : the encoding which has been used , 'bars' : the bars , 'text' : text - positioning info , 'error' : error message if error )
2011-11-27 15:33:18 +01:00
*/
function barcode_encode_ean ( $ean , $encoding = " EAN-13 " )
{
2020-10-31 14:32:18 +01:00
$ean = trim ( $ean );
2021-02-23 22:03:23 +01:00
if ( preg_match ( " /[^0-9]/i " , $ean )) {
2024-03-18 22:59:11 +01:00
return array ( " error " => " Invalid encoding/code. encoding= " . $encoding . " code= " . $ean . " (not a numeric) " , " text " => " Invalid encoding/code. encoding= " . $encoding . " code= " . $ean . " (not a numeric) " );
2020-10-31 14:32:18 +01:00
}
$encoding = strtoupper ( $encoding );
2021-02-23 22:03:23 +01:00
if ( $encoding == " ISBN " ) {
if ( ! preg_match ( " /^978/ " , $ean )) {
$ean = " 978 " . $ean ;
}
}
2021-03-12 13:55:39 +01:00
if ( preg_match ( " /^97[89]/ " , $ean )) {
2021-02-23 22:03:23 +01:00
$encoding = " ISBN " ;
2020-10-31 14:32:18 +01:00
}
2021-02-23 22:03:23 +01:00
if ( strlen ( $ean ) < 12 || strlen ( $ean ) > 13 ) {
2024-03-18 22:59:11 +01:00
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) " );
2020-10-31 14:32:18 +01:00
}
$ean = substr ( $ean , 0 , 12 );
$eansum = barcode_gen_ean_sum ( $ean );
$ean .= $eansum ;
2021-03-12 13:11:17 +01:00
$bars = barcode_gen_ean_bars ( $ean );
2020-10-31 14:32:18 +01:00
/* create text */
$pos = 0 ;
$text = " " ;
2021-02-23 22:03:23 +01:00
for ( $a = 0 ; $a < 13 ; $a ++ ) {
if ( $a > 0 ) {
$text .= " " ;
}
2023-05-01 14:33:08 +02:00
$text .= $pos . " :12: " . $ean [ $a ];
2021-02-23 22:03:23 +01:00
if ( $a == 0 ) {
$pos += 12 ;
} elseif ( $a == 6 ) {
$pos += 12 ;
} else {
$pos += 7 ;
}
2020-10-31 14:32:18 +01:00
}
return array (
" error " => '' ,
" encoding " => $encoding ,
2021-03-12 13:11:17 +01:00
" bars " => $bars ,
2011-11-27 15:33:18 +01:00
" text " => $text
2020-10-31 14:32:18 +01:00
);
2011-11-27 15:33:18 +01:00
}
2021-03-12 13:54:27 +01:00
/**
* Encode UPC
*
* @ param string $upc Code
* @ param string $encoding Encoding
* @ return array array ( 'encoding' : the encoding which has been used , 'bars' : the bars , 'text' : text - positioning info , 'error' : error message if error )
*/
function barcode_encode_upc ( $upc , $encoding = " UPC " )
{
$upc = trim ( $upc );
if ( preg_match ( " /[^0-9]/i " , $upc )) {
2024-03-18 22:59:11 +01:00
return array ( " error " => " Invalid encoding/code. encoding= " . $encoding . " code= " . $upc . " (not a numeric) " , " text " => " Invalid encoding/code. encoding= " . $encoding . " code= " . $upc . " (not a numeric) " );
2021-03-12 13:54:27 +01:00
}
$encoding = strtoupper ( $encoding );
if ( strlen ( $upc ) < 11 || strlen ( $upc ) > 12 ) {
2024-03-18 22:59:11 +01:00
return array ( " error " => " Invalid encoding/code. encoding= " . $encoding . " code= " . $upc . " (must have 11/12 numbers) " , " text " => " Invalid encoding/code. encoding= " . $encoding . " code= " . $upc . " (must have 11/12 numbers) " );
2021-03-12 13:54:27 +01:00
}
$upc = substr ( " 0 " . $upc , 0 , 12 );
$eansum = barcode_gen_ean_sum ( $upc );
$upc .= $eansum ;
$bars = barcode_gen_ean_bars ( $upc );
/* create text */
$pos = 0 ;
$text = " " ;
for ( $a = 1 ; $a < 13 ; $a ++ ) {
if ( $a > 1 ) {
$text .= " " ;
}
2023-05-01 15:09:43 +02:00
$text .= $pos . " :12: " . $upc [ $a ];
2021-03-12 13:54:27 +01:00
if ( $a == 1 ) {
2021-03-12 14:44:04 +01:00
$pos += 15 ;
2021-03-12 13:54:27 +01:00
} elseif ( $a == 6 ) {
$pos += 17 ;
2021-03-12 14:44:04 +01:00
} elseif ( $a == 11 ) {
$pos += 15 ;
2021-03-12 13:54:27 +01:00
} else {
$pos += 7 ;
}
}
return array (
" error " => '' ,
" encoding " => $encoding ,
" bars " => $bars ,
" text " => $text
);
}
2011-11-27 15:33:18 +01:00
/**
2012-02-04 14:39:47 +01:00
* Encode result of genbarcode command
2007-09-28 00:24:43 +02:00
*
2012-02-04 14:39:47 +01:00
* @ param string $code Code
* @ param string $encoding Encoding
2019-02-13 22:23:14 +01:00
* @ return array | false array ( 'encoding' : the encoding which has been used , 'bars' : the bars , 'text' : text - positioning info )
2011-11-28 23:07:19 +01:00
*/
2019-01-27 15:20:16 +01:00
function barcode_encode_genbarcode ( $code , $encoding )
2011-11-28 23:07:19 +01:00
{
2023-02-17 17:55:46 +01:00
global $conf , $db , $genbarcode_loc ;
2020-10-31 14:32:18 +01:00
// Clean parameters
2021-02-23 22:03:23 +01:00
if ( preg_match ( " /^ean $ /i " , $encoding ) && strlen ( $code ) == 13 ) {
$code = substr ( $code , 0 , 12 );
}
if ( ! $encoding ) {
$encoding = " ANY " ;
}
2023-02-17 17:55:46 +01:00
$encoding = dol_string_nospecial ( $encoding , '_' );
$code = dol_string_nospecial ( $code , " _ " );
2020-10-31 14:32:18 +01:00
$command = escapeshellarg ( $genbarcode_loc );
$paramclear = " " . escapeshellarg ( $code ) . " " . escapeshellarg ( strtoupper ( $encoding ));
$fullcommandclear = $command . " " . $paramclear . " 2>&1 " ;
//print $fullcommandclear."<br>\n";exit;
dol_syslog ( " Run command " . $fullcommandclear );
2023-02-17 17:55:46 +01:00
$outputfile = $conf -> user -> dir_temp . '/genbarcode.tmp' ; // File used with popen method
// Execute a CLI
include_once DOL_DOCUMENT_ROOT . '/core/class/utils.class.php' ;
$utils = new Utils ( $db );
$result = $utils -> executeCLI ( $fullcommandclear , $outputfile );
if ( ! empty ( $result [ 'output' ])) {
$tmparr = explode ( " \n " , $result [ 'output' ]);
$bars = $tmparr [ 0 ];
$text = $tmparr [ 1 ];
$encoding = $tmparr [ 2 ];
2020-10-31 14:32:18 +01:00
} else {
2023-02-17 17:55:46 +01:00
dol_syslog ( " barcode.lib.php::barcode_encode_genbarcode failed to run " . $fullcommandclear , LOG_ERR );
2020-10-31 14:32:18 +01:00
return false ;
}
2023-02-17 17:55:46 +01:00
2020-10-31 14:32:18 +01:00
//var_dump($bars);
$ret = array (
2011-11-28 23:07:19 +01:00
" bars " => trim ( $bars ),
2013-05-11 15:04:17 +02:00
" text " => trim ( $text ),
" encoding " => trim ( $encoding ),
2020-10-31 14:32:18 +01:00
" error " => " "
);
//var_dump($ret);
2021-02-23 22:03:23 +01:00
if ( preg_match ( '/permission denied/i' , $ret [ 'bars' ])) {
2021-03-01 20:37:16 +01:00
$ret [ 'error' ] = $ret [ 'bars' ];
$ret [ 'bars' ] = '' ;
2020-10-31 14:32:18 +01:00
return $ret ;
}
2021-02-23 22:03:23 +01:00
if ( ! $ret [ 'bars' ]) {
return false ;
}
if ( ! $ret [ 'text' ]) {
return false ;
}
if ( ! $ret [ 'encoding' ]) {
return false ;
}
2020-10-31 14:32:18 +01:00
return $ret ;
2011-11-28 23:07:19 +01:00
}
/**
* Output image onto standard output , or onto disk if global filebarcode is defined
2007-09-28 00:24:43 +02:00
*
2011-11-28 23:07:19 +01:00
* @ param string $text the text - line ( < position >:< font - size >:< character > ... )
* @ param string $bars where to place the bars ( < space - width >< bar - width >< space - width >< bar - width >... )
2012-04-16 12:01:32 +02:00
* @ param int $scale scale factor ( 1 < scale < unlimited ( scale 50 will produce 5400 x300 pixels when using EAN - 13 !!! ))
2011-11-28 23:07:19 +01:00
* @ param string $mode png , gif , jpg ( default = 'png' )
* @ param int $total_y the total height of the image ( default : scale * 60 )
* @ param array $space default : $space [ top ] = 2 * $scale ; $space [ bottom ] = 2 * $scale ; $space [ left ] = 2 * $scale ; $space [ right ] = 2 * $scale ;
2024-02-26 22:34:17 +01:00
* @ return string | void
2007-09-28 00:24:43 +02:00
*/
2024-02-29 15:26:53 +01:00
function barcode_outimage ( $text , $bars , $scale = 1 , $mode = " png " , $total_y = 0 , $space = [])
2011-11-27 15:33:18 +01:00
{
2020-10-31 14:32:18 +01:00
global $bar_color , $bg_color , $text_color ;
global $font_loc , $filebarcode ;
//print "$text, $bars, $scale, $mode, $total_y, $space, $font_loc, $filebarcode<br>";
/* set defaults */
2021-02-23 22:03:23 +01:00
if ( $scale < 1 ) {
$scale = 2 ;
}
2020-10-31 14:32:18 +01:00
$total_y = ( int ) $total_y ;
2021-02-23 22:03:23 +01:00
if ( $total_y < 1 ) {
$total_y = ( int ) $scale * 60 ;
}
if ( ! $space ) {
2024-03-18 22:59:11 +01:00
$space = array ( 'top' => 2 * $scale , 'bottom' => 2 * $scale , 'left' => 2 * $scale , 'right' => 2 * $scale );
2021-02-23 22:03:23 +01:00
}
2020-10-31 14:32:18 +01:00
/* count total width */
$xpos = 0 ;
$width = true ;
$ln = strlen ( $bars );
2021-02-23 22:03:23 +01:00
for ( $i = 0 ; $i < $ln ; $i ++ ) {
2020-10-31 14:32:18 +01:00
$val = strtolower ( $bars [ $i ]);
2021-02-23 22:03:23 +01:00
if ( $width ) {
2020-10-31 14:32:18 +01:00
$xpos += $val * $scale ;
$width = false ;
continue ;
}
2021-02-23 22:03:23 +01:00
if ( preg_match ( " /[a-z]/ " , $val )) {
2020-10-31 14:32:18 +01:00
/* tall bar */
$val = ord ( $val ) - ord ( 'a' ) + 1 ;
}
$xpos += $val * $scale ;
$width = true ;
}
/* allocate the image */
$total_x = ( $xpos ) + $space [ 'right' ] + $space [ 'right' ];
$xpos = $space [ 'left' ];
2021-02-23 22:03:23 +01:00
if ( ! function_exists ( " imagecreate " )) {
2020-10-31 14:32:18 +01:00
print " You don't have the gd2 extension enabled<br> \n " ;
return " " ;
}
$im = imagecreate ( $total_x , $total_y );
/* create two images */
2024-01-12 20:40:34 +01:00
$col_bg = imagecolorallocate ( $im , $bg_color [ 0 ], $bg_color [ 1 ], $bg_color [ 2 ]);
$col_bar = imagecolorallocate ( $im , $bar_color [ 0 ], $bar_color [ 1 ], $bar_color [ 2 ]);
$col_text = imagecolorallocate ( $im , $text_color [ 0 ], $text_color [ 1 ], $text_color [ 2 ]);
2024-03-18 22:59:11 +01:00
$height = ( int ) round ( $total_y - ( $scale * 10 ));
$height2 = ( int ) round ( $total_y - $space [ 'bottom' ]);
2020-10-31 14:32:18 +01:00
/* paint the bars */
$width = true ;
$ln = strlen ( $bars );
2021-02-23 22:03:23 +01:00
for ( $i = 0 ; $i < $ln ; $i ++ ) {
2020-10-31 14:32:18 +01:00
$val = strtolower ( $bars [ $i ]);
2021-02-23 22:03:23 +01:00
if ( $width ) {
2020-10-31 14:32:18 +01:00
$xpos += $val * $scale ;
$width = false ;
continue ;
}
2021-02-23 22:03:23 +01:00
if ( preg_match ( " /[a-z]/ " , $val )) {
2020-10-31 14:32:18 +01:00
/* tall bar */
$val = ord ( $val ) - ord ( 'a' ) + 1 ;
$h = $height2 ;
2021-02-23 22:03:23 +01:00
} else {
$h = $height ;
}
2020-10-31 14:32:18 +01:00
imagefilledrectangle ( $im , $xpos , $space [ 'top' ], $xpos + ( $val * $scale ) - 1 , $h , $col_bar );
$xpos += $val * $scale ;
$width = true ;
}
$chars = explode ( " " , $text );
2020-11-03 21:35:23 +01:00
foreach ( $chars as $v ) {
if ( trim ( $v )) {
2020-10-31 14:32:18 +01:00
$inf = explode ( " : " , $v );
$fontsize = $scale * ( $inf [ 1 ] / 1.8 );
2024-03-18 22:59:11 +01:00
$fontheight = ( int ) round ( $total_y - ( $fontsize / 2.7 ) + 2 );
2020-10-31 14:32:18 +01:00
imagettftext ( $im , $fontsize , 0 , $space [ 'left' ] + ( $scale * $inf [ 0 ]) + 2 , $fontheight , $col_text , $font_loc , $inf [ 2 ]);
}
}
/* output the image */
$mode = strtolower ( $mode );
2020-11-03 21:35:23 +01:00
if ( $mode == 'jpg' || $mode == 'jpeg' ) {
2020-10-31 14:32:18 +01:00
header ( " Content-Type: image/jpeg; name= \" barcode.jpg \" " );
imagejpeg ( $im );
2020-11-03 21:35:23 +01:00
} elseif ( $mode == 'gif' ) {
2020-10-31 14:32:18 +01:00
header ( " Content-Type: image/gif; name= \" barcode.gif \" " );
imagegif ( $im );
2020-11-03 21:35:23 +01:00
} elseif ( ! empty ( $filebarcode )) {
2024-02-26 22:34:17 +01:00
// To write into a file onto disk
2020-10-31 14:32:18 +01:00
imagepng ( $im , $filebarcode );
} else {
header ( " Content-Type: image/png; name= \" barcode.png \" " );
imagepng ( $im );
}
2024-01-09 18:59:12 +01:00
return ;
2007-09-28 00:24:43 +02:00
}
2023-11-14 01:54:04 +01:00
/**
* Check if EAN13 code is valid
*
* @ param string $ean Code
* @ return bool
*/
function isAValidEAN13 ( $ean )
{
$sumEvenIndexes = 0 ;
$sumOddIndexes = 0 ;
$eanAsArray = array_map ( 'intval' , str_split ( $ean ));
if ( ! ( count ( $eanAsArray ) === 13 )) {
return false ;
};
2023-11-19 00:56:46 +01:00
$num = ( count ( $eanAsArray ) - 1 );
for ( $i = 0 ; $i < $num ; $i ++ ) {
2023-11-14 01:54:04 +01:00
if ( $i % 2 === 0 ) {
$sumOddIndexes += $eanAsArray [ $i ];
} else {
$sumEvenIndexes += $eanAsArray [ $i ];
}
}
$rest = ( $sumOddIndexes + ( 3 * $sumEvenIndexes )) % 10 ;
if ( $rest !== 0 ) {
$rest = 10 - $rest ;
}
return $rest === $eanAsArray [ 12 ];
}