ce source n'est pas de moi, mais j'ai mis tellement de temps à le trouver que je vous en fait cadeau
ce script permet de générer des code barre EAN 13 en fabriquant chacune des barre, ça ne se base pas sur une police True Type (TTF)
Le site où je l'ai trouvée (NE FONCTIONNE PLUS):
http://www.neokraft.net/articles/codes-barres-php/
voilà un autre site
http://www.alaide.com/outils_generateur_ean13.php mais c'est du javascript
et en plus un site pour créer les images des codes barres (vient de GRenard :
http://www.phpcs.com/codes/GENERATEUR-CODE-BARRE-BARCODE_23927.aspx):
http://www.barcodephp.com/1d/demo/html/ean13.php
j'ai créé aussi une source pour fabriquer un code barre code 39:
http://www.phpcs.com/code.aspx?ID=31659
Source / Exemple :
<?php
// ce fichier s'appelle par exemple EAN13.php
// dans votre programme il suffit d'appeler l'image comme suit:
// <img src="EAN13.php?numero=3149025043092&dimension=5">
// et c'est tout
$form_numero='0123456789012'; //le code EAN13 à 13 chiffres
$form_dimension=4.5; // multiplicateur de la taille de l'image initiale (120 pixel x 70 pixel)
// ce qui donne avec la valeur de 4.5 une image en 300 dpi de 4.57 cm x 2.57 cm (540 pixel x 315 pixel)
import_request_variables("gP", "form_");
/* ***** BEGIN LICENSE BLOCK *****
*
- The contents of this file are subject to the Mozilla Public License Version
- 1.1 (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.mozilla.org/MPL/
*
- Software distributed under the License is distributed on an "AS IS" basis,
- WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- for the specific language governing rights and limitations under the
- License.
*
- The Original Code is : Debora, un générateur de codes barre.
*
- The Initial Developer of the Original Code is
- Olivier Meunier.
- Portions created by the Initial Developer are Copyright (C) 2003
- the Initial Developer. All Rights Reserved.
*
- Contributor(s):
- Rémi Chéno (ajout des séparateurs gauche, centre et droite)
*
- ***** END LICENSE BLOCK ***** */
class debora
{
/**
- Déclaration des propriétés
- /
var $arryGroup = array('A' => array(
0 => "0001101", 1 => "0011001",
2 => "0010011", 3 => "0111101",
4 => "0100011", 5 => "0110001",
6 => "0101111", 7 => "0111011",
8 => "0110111", 9 => "0001011"
),
'B' => array(
0 => "0100111", 1 => "0110011",
2 => "0011011", 3 => "0100001",
4 => "0011101", 5 => "0111001",
6 => "0000101", 7 => "0010001",
8 => "0001001", 9 => "0010111"
),
'C' => array(
0 => "1110010", 1 => "1100110",
2 => "1101100", 3 => "1000010",
4 => "1011100", 5 => "1001110",
6 => "1010000", 7 => "1000100",
8 => "1001000", 9 => "1110100"
)
);
var $arryFamily = array(
0 => array('A','A','A','A','A','A'),
1 => array('A','A','B','A','B','B'),
2 => array('A','A','B','B','A','B'),
3 => array('A','A','B','B','B','A'),
4 => array('A','B','A','A','B','B'),
5 => array('A','B','B','A','A','B'),
6 => array('A','B','B','B','A','A'),
7 => array('A','B','A','B','A','B'),
8 => array('A','B','A','B','B','A'),
9 => array('A','B','B','A','B','A')
);
/**
*
*
*
function debora($EAN13)
{
settype($EAN13,'string');
//Transformation de la chaine EAN en tableau
for($i=0;$i<13;$i++)
{
$this->EAN13[$i] = substr($EAN13,$i,1);
}
$this->strCode = $this->makeCode();
}
/**
*
- Crée une chaine contenant des 0 ou des 1 pour indiquer les espace blancs ou noir
*
- return string Chaine résultante
- /
function makeCode()
{
//On récupère la classe de codage de la partie qauche
$arryLeftClass = $this->arryFamily[$this->EAN13[0]];
//Premier séparateur (101)
$strCode = '101';
//Codage partie gauche
for ($i=1; $i<7; $i++)
{
$strCode .= $this->arryGroup[$arryLeftClass[$i-1]][$this->EAN13[$i]];
}
//Séparateur central (01010)
$strCode .= '01010';
//Codage partie droite (tous de classe C)
for ($i=7; $i<13; $i++)
{
$strCode .= $this->arryGroup['C'][$this->EAN13[$i]];
}
//Dernier séparateur (101)
$strCode .= '101';
return $strCode;
}
/**
*
- Crée une image GIF ou PNG du code généré par giveCode
*
function makeImage($imageType="png")
{
global $form_dimension;
//Initialisation de l'image
//$img=imagecreate(120, 70);
$width=120;
$height=70;
$img=imagecreate($width, $height);
$color[0] = ImageColorAllocate($img, 255,255,255);
$color[1] = ImageColorAllocate($img, 0,0,0);
$coords[0] = 15;
$coords[1] = 10;
$coords[2] = 1;
$coords[3] = 40;
imagefilledrectangle($img, 0, 0, 95, 80, $color[0]);
for($i=0;$i<strlen($this->strCode);$i++)
{
$posX = $coords[0];
$posY = $coords[1];
$intL = $coords[2];
$intH = $coords[3];
$fill_color = substr($this->strCode,$i,1);
# Allongement des 3 bandes latérales et centrales
# sur une idée de Rémi Chéno
if ($i < 3 || ($i >= 46 && $i < 49) || $i >= 92) {
$intH = $intH + 8;
}
imagefilledrectangle($img, $posX, $posY, $posX, ($posY+$intH), $color[$fill_color]);
//Deplacement du pointeur
$coords[0] = $coords[0] + $coords[2];
}
# Affichage du code (Rémi Chéno)
imagestring($img, 3, 5, 50, $this->EAN13[0], $color[1]);
imagestring($img, 3, 19, 50, implode('', array_slice($this->EAN13,1, 6)), $color[1]);
imagestring($img, 3, 65, 50, implode('', array_slice($this->EAN13,7)), $color[1]);
// Calcul des nouvelles dimensions
$newwidth = $width* $form_dimension;
$newheight = $height * $form_dimension;
// Chargement
$thumb = imagecreatetruecolor($newwidth, $newheight);
// Redimensionnement
imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
Header( "Content-type: image/".$imageType);
$func_name = 'image'.$imageType;
$func_name($thumb);
imagedestroy($img);
imagedestroy($thumb);
}
}//Fin de la classe
$ean13 = new debora($form_numero);
$ean13-> makeImage();
?>
Conclusion :
Si vous avez des améliorations ou des commentaire et même des critiques, tout est bienvenue
16 juil. 2015 à 12:49
28 sept. 2011 à 11:27
Mais je me suis quand même cassé la tête dessus jusqu'à lire le message de ghetto95.
Car moi aussi en copier/collant le code avec le clic droit de la souris sous Firefox, rien ne fonctionnait...
Il m'a fallu passer par IE et cliquer sur "copier dans le presse papier" pour que ça marche.
C'est très énervant ces soucis de copier/coller quand même...
5 avril 2011 à 13:19
votre formulaire crée l'image une fois que vous l'avez validé.
comment voulez-vous qu'il crée une image dont le code n'a pas été saisi ?
d'ailleurs pourquoi mettez-vous un formulaire si c'est une page à imprimer ?
je ne comprends pas votre raisonnement ?
5 avril 2011 à 12:07
<html>
<head>
<title>Code barre EAN13</title>
<!--<link rel="stylesheet" type="text/css" media="print" href="print.css" />-->
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/jquery.tablesorter.js" type="text/javascript"></script>
<script src="js/jquery.jPrintArea.js" type="text/javascript"></script>
<script src="js/codebarre.js" type="text/javascript"></script>
<script type="text/javascript">
function getCookieVal(offset) {
var endstr=document.cookie.indexOf (";", offset);
if (endstr==-1)
endstr=document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg=name+"=";
var alen=arg.length;
var clen=document.cookie.length;
var i=0;
while (i<clen) {
var j=i+alen;
if (document.cookie.substring(i, j)==arg)
return getCookieVal (j);
i=document.cookie.indexOf(" ",i)+1;
if (i==0) break;}
return null;
}
function DeleteCookie (name) {
var exp=new Date();
exp.setTime (exp.getTime() - 100000);
var cval=GetCookie (name);
document.cookie=name+"="+cval+"; expires="+exp.toGMTString();
}
function EAN13() {
//var form = document.getElementById('form1');
var url="index.php";
cb = document.form2.codeBarre.value;
if (isNaN(cb)||cb.length!=12) {
alert("Un nombre de 12 chiffres seulement");
document.location.href=url;
return false;
}else{
url="index.php?valid="+1;
document.location.href=url;
}
}
</script>
</head>
<?php
$valid="";
if(isset($_GET['valid'])){
$valid=$_GET['valid'];
}
$codebar="";
if(isset($_POST['codeBarre'])){
if(!empty($_POST['codeBarre'])){
$codebar=$_POST['codeBarre'];
$form_dimension=4.5;
class debora
{
/**
* Déclaration des propriétés
*/ var $arryGroup array('A'> array(
0 => "0001101", 1 => "0011001",
2 => "0010011", 3 => "0111101",
4 => "0100011", 5 => "0110001",
6 => "0101111", 7 => "0111011",
8 => "0110111", 9 => "0001011"
),
'B' => array(
0 => "0100111", 1 => "0110011",
2 => "0011011", 3 => "0100001",
4 => "0011101", 5 => "0111001",
6 => "0000101", 7 => "0010001",
8 => "0001001", 9 => "0010111"
),
'C' => array(
0 => "1110010", 1 => "1100110",
2 => "1101100", 3 => "1000010",
4 => "1011100", 5 => "1001110",
6 => "1010000", 7 => "1000100",
8 => "1001000", 9 => "1110100"
)
);
var $arryFamily = array(
0 => array('A','A','A','A','A','A'),
1 => array('A','A','B','A','B','B'),
2 => array('A','A','B','B','A','B'),
3 => array('A','A','B','B','B','A'),
4 => array('A','B','A','A','B','B'),
5 => array('A','B','B','A','A','B'),
6 => array('A','B','B','B','A','A'),
7 => array('A','B','A','B','A','B'),
8 => array('A','B','A','B','B','A'),
9 => array('A','B','B','A','B','A')
);
/**
* Constructeur
*
* Initialise la classe
*
* @EAN13 string code EAN13
*
* return void
*/
function GetCheckDigit($EAN13)
{
//Calcule le chiffre de contrle
$sum=0;
for($i=1;$i<=11;$i+=2)
$sum+=3*$EAN13{$i};
for($i=0;$i<=10;$i+=2)
$sum+=$EAN13{$i};
$r=$sum%10;
if($r>0)
$r=10-$r;
return $r;
}
function debora($EAN13)
{
$EAN13=$EAN13.$this->GetCheckDigit($EAN13);
settype($EAN13,'string');
//Transformation de la chaine EAN en tableau
for($i=0;$i<13;$i++)
{
$this->EAN13[$i] = substr($EAN13,$i,1);
}
$this->strCode = $this->makeCode();
}
/**
* Création du code binaire
*
* Crée une chaine contenant des 0 ou des 1 pour indiquer les espace blancs ou noir
*
* return string Chaine résultante
*/
function makeCode()
{
//On récupère la classe de codage de la partie qauche
$arryLeftClass = $this->arryFamily[$this->EAN13[0]];
//Premier séparateur (101)
$strCode = '101';
//Codage partie gauche
for ($i=1; $i<7; $i++)
{
$strCode .= $this->arryGroup[$arryLeftClass[$i-1]][$this->EAN13[$i]];
}
//Séparateur central (01010)
$strCode .= '01010';
//Codage partie droite (tous de classe C)
for ($i=7; $i<13; $i++)
{
$strCode .= $this->arryGroup['C'][$this->EAN13[$i]];
}
//Dernier séparateur (101)
$strCode .= '101';
return $strCode;
}
/**
* Création de l'image
*
* Crée une image GIF ou PNG du code généré par giveCode
*
* return void
*/
function makeImage($imageType="jpeg")
{
global $form_dimension;
//Initialisation de l'image
//$img=imagecreate(120, 70);
//unlink("image/monimage.jpeg");
$width=120;
$height=70;
$img=imagecreate($width, $height);
$color[0] = ImageColorAllocate($img, 255,255,255);
$color[1] = ImageColorAllocate($img, 0,0,0);
$coords[0] = 15;
$coords[1] = 10;
$coords[2] = 1;
$coords[3] = 40;
imagefilledrectangle($img, 0, 0, 95, 80, $color[0]);
for($i=0;$i<strlen($this->strCode);$i++)
{
$posX = $coords[0];
$posY = $coords[1];
$intL = $coords[2];
$intH = $coords[3];
$fill_color = substr($this->strCode,$i,1);
# Allongement des 3 bandes latérales et centrales
# sur une idée de Rémi Chéno
if ($i < 3 || ($i >= 46 && $i < 49) || $i >= 92) {
$intH = $intH + 8;
}
imagefilledrectangle($img, $posX, $posY, $posX, ($posY+$intH), $color[$fill_color]);
//Deplacement du pointeur
$coords[0] = $coords[0] + $coords[2];
}
# Affichage du code (Rémi Chéno)
imagestring($img, 3, 5, 50, $this->EAN13[0], $color[1]);
imagestring($img, 3, 19, 50, implode('', array_slice($this->EAN13,1, 6)), $color[1]);
imagestring($img, 3, 65, 50, implode('', array_slice($this->EAN13,7)), $color[1]);
// Calcul des nouvelles dimensions
$newwidth = $width* $form_dimension;
$newheight = $height * $form_dimension;
// Chargement
$thumb = imagecreatetruecolor($newwidth, $newheight);
// Redimensionnement
imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//Transfert du fichier dans le répertoire image
imagejpeg($img,"image/monimage.jpeg");
chmod ("image/monimage.jpeg", 0777);
imagedestroy($img);
imagedestroy($thumb);
}
}
unlink("image/monimage.jpeg");
$ean13 = new debora($codebar);
$ean13->makeImage();
}
}
?>
<form name="form2" id="form2" method="POST" >
Code EAN13 (12 premier chiffres):
</form>
<?php if(!empty($codebar)){
$image='';
?>
<form name="form1" id="form1" >
<fieldset style="margin-top:100px;width:7.5cm;height:1.1cm;border: 1px solid #000000; text-decoration: none;
-moz-border-radius: 10px 10px 10px 10px;
-webkit-border-radius: 10px 10px 10px 10px;
border-radius: 10px 10px 10px 10px;
-webkit-border-top-left-radius: 10px; /* pour Chrome */
-webkit-border-top-right-radius: 10px; "> <?php echo $image;?>
<label>Poids </label><?php echo "4.41 grs"; ?>,
----
<?php echo "S12MN1K"; ?>,
<?php echo "Puste"; ?>,
<?php echo "21.90 Eur"; ?>
</fieldset>
</form>
<?php $codebar="";}?>
</html>
Il copie l'image dans le répertoire image
Il cherche d'abord l'élément div EAN13CODE
L'impression marche mais le problème c'est qu'il imprime l'image qu'on a saisie avant et non actuelle. Je pense que l'image précédente n'est pas supprimée
Avez-vous une solution?
5 avril 2011 à 08:52
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.