1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
<?php
// ############## GENERER UN CODE BARRE CODE 39 AVEC PHP/GD ################# //
// Date de création : 11/11/2007 //
// Auteur : Thierry Godin //
// http://thierry-godin.developpez.com/ //
// //
// Remerciements à Guillaume Rossolini pour les corrections et les conseils //
// http://g-rossolini.developpez.com/ //
// ########################################################################## //
//Vérification des paramètres passés par l'URL =================================
if(isset($_GET['string']))
{
$TheString = preg_replace('¤[^0-9A-Z. $/+%*-]¤', '', strtoupper($_GET['string']));
}
else
{
$TheString = 'ERREUR';
}
if(isset($_GET['control']) and $_GET['control'] == "1")
{
$AddControl = TRUE;
}
else{
$AddControl = FALSE;
}
//------------------------------------------------------------------------------
//création de l'image temporaire ===============================================
$thumb = imagecreatetruecolor(500, 25);
$fond = imagecolorallocate($thumb, 255, 0, 255); // fond
imagefill($thumb, 0, 0, $fond);
//------------------------------------------------------------------------------
// tableau Char -> Code ========================================================
$TabCode = array(
'0' => '101000111011101',
'1' => '111010001010111',
'2' => '101110001010111',
'3' => '111011100010101',
'4' => '101000111010111',
'5' => '111010001110101',
'6' => '101110001110101',
'7' => '101000101110111',
'8' => '111010001011101',
'9' => '101110001011101',
'A' => '111010100010111',
'B' => '101110100010111',
'C' => '111011101000101',
'D' => '101011100010111',
'E' => '111010111000101',
'F' => '101110111000101',
'G' => '101010001110111',
'H' => '111010100011101',
'I' => '101110100011101',
'J' => '101011100011101',
'K' => '111010101000111',
'L' => '101110101000111',
'M' => '111011101010001',
'N' => '101011101000111',
'O' => '111010111010001',
'P' => '101110111010001',
'Q' => '101010111000111',
'R' => '111010101110001',
'S' => '101110101110001',
'T' => '101011101110001',
'U' => '111000101010111',
'V' => '100011101010111',
'W' => '111000111010101',
'X' => '100010111010111',
'Y' => '111000101110101',
'Z' => '100011101110101',
'-' => '100010101110111',
'.' => '111000101011101',
' ' => '100011101011101',
'$' => '100010001000101',
'/' => '100010001010001',
'+' => '100010100010001',
'%' => '101000100010001',
'*' => '100010111011101'
);
//------------------------------------------------------------------------------
// convertir la chaine en code =================================================
$CodeBar = '';
// tableau des caractères seuls
$TabKeys = array_keys($TabCode);
$TotalChar = 0;
for($i = 0; $i < strlen($TheString); $i++)
{
$CodeBar .= "0" . $TabCode[$TheString[$i]] . "<br>";
//récupération de l'index du caractère + calcul de la somme des indexes
foreach($TabKeys as $key => $value) {
if($value === $TheString[$i]){ $TotalChar += $key;}
}
}
//------------------------------------------------------------------------------
//calculer le caractère de controle ============================================
$IndexControl = bcmod($TotalChar, 43); // retourne le reste de la division
//------------------------------------------------------------------------------
//ajouter le caratère de controle ==============================================
if($AddControl)
{
$CodeBar.= "0" . $TabCode[$TabKeys[$IndexControl]];
}
//------------------------------------------------------------------------------
//on rajoute * en début et en fin de code ======================================
$xCodeBar = $TabCode["*"] . $CodeBar . "0" . $TabCode["*"];
//------------------------------------------------------------------------------
// dessiner le code barre ======================================================
$c_w = imagecolorallocate($thumb, 255, 255, 255); // blanc
$c_b = imagecolorallocate($thumb, 0, 0, 0); // noir
$x_ref = 0;
for($x=0; $x < strlen($xCodeBar); $x++)
{
if($xCodeBar[$x] == "1")
{
imageline($thumb, $x_ref, 0, $x_ref, 25, $c_b);
}
else
{
imageline($thumb, $x_ref, 0, $x_ref, 25, $c_w);
}
$x_ref++;
}
//------------------------------------------------------------------------------
// Création de l'image définitive ==============================================
$IMG = imagecreatetruecolor($x_ref, 25);
imagefill($IMG, 0, 0, $fond);
imagecopymerge ($IMG, $thumb, 0, 0, 0, 0, 500, 25, 100 );
//------------------------------------------------------------------------------
header("Content-type: image/png");
imagepng($IMG);
?> |
Partager