Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Tło dla generowanego kodu kreskowego
Forum PHP.pl > Forum > Przedszkole
JamalBIG
Witam

W jaki sposób dla poniższej klasy mogę dodać tło w generowanym kodzie kreskowym lub - najlepsze rozwiązanie - całość była generowana na tle innego pliku PNG, GIF lub JPEG?

  1. class barCodeGenrator {
  2. private $file;
  3. private $into;
  4. private $digitArray = array(0=>"00110",1=>"10001",2=>"01001",3=>"11000",4=>"00101",5=>"10100",6=>"01100",7=>"00011",8=>"10010",9=>"01010");
  5. function __construct($value,$into=1, $filename = 'barcode.gif', $width_bar=450, $height_bar=65, $show_codebar=false) {
  6.  
  7. $lower = 1 ; $hight = 50;
  8. $this->into = $into;
  9. $this->file = $filename;
  10. for($count1=9;$count1>=0;$count1--){
  11. for($count2=9;$count2>=0;$count2--){
  12. $count = ($count1 * 10) + $count2 ;
  13. $text = "" ;
  14. for($i=1;$i<6;$i++){
  15. $text .= substr($this->digitArray[$count1],($i-1),1) . substr($this->digitArray[$count2],($i-1),1);
  16. }
  17. $this->digitArray[$count] = $text;
  18. }
  19. }
  20.  
  21.  
  22. $height_bar_max = $height_bar;
  23. $width_bar_max = $width_bar;
  24.  
  25. $img = imagecreate($width_bar_max,$height_bar_max);
  26. if ($show_codebar) {
  27. $height_bar -= 25;
  28. }
  29.  
  30. $cl_black = imagecolorallocate($img, 0, 0, 0);
  31. $cl_white = imagecolorallocate($img, 255, 255, 255);
  32.  
  33. #imagefilledrectangle($img, 0, 0, $lower*95+1000, $hight+300, $cl_white);
  34. imagefilledrectangle($img, 0, 0, $width_bar_max, $height_bar_max, $cl_white);
  35. imagefilledrectangle($img, 5,5,5,$height_bar,$cl_black);
  36. imagefilledrectangle($img, 6,5,6,$height_bar,$cl_white);
  37. imagefilledrectangle($img, 7,5,7,$height_bar,$cl_black);
  38. imagefilledrectangle($img, 8,5,8,$height_bar,$cl_white);
  39. $thin = 1 ;
  40. if(substr_count(strtoupper($_SERVER['SERVER_SOFTWARE']),"WIN32")){
  41. $wide = 3;
  42. } else {
  43. $wide = 2.72;
  44. }
  45. $pos = 9 ;
  46. $text = $value ;
  47. if((strlen($text) % 2) <> 0){
  48. $text = "0" . $text;
  49. }
  50.  
  51.  
  52. while (strlen($text) > 0) {
  53. $i = round($this->JSK_left($text,2));
  54. $text = $this->JSK_right($text,strlen($text)-2);
  55.  
  56. $f = $this->digitArray[$i];
  57.  
  58. for($i=1;$i<11;$i+=2){
  59. if (substr($f,($i-1),1) == "0") {
  60. $f1 = $thin ;
  61. }else{
  62. $f1 = $wide ;
  63. }
  64. imagefilledrectangle($img, $pos,5,$pos-1+$f1,$height_bar,$cl_black) ;
  65. $pos = $pos + $f1 ;
  66.  
  67. if (substr($f,$i,1) == "0") {
  68. $f2 = $thin ;
  69. }else{
  70. $f2 = $wide ;
  71. }
  72. imagefilledrectangle($img, $pos,5,$pos-1+$f2,$height_bar,$cl_white) ;
  73. $pos = $pos + $f2 ;
  74. }
  75. }
  76. imagefilledrectangle($img, $pos,5,$pos-1+$wide,$height_bar,$cl_black);
  77. $pos=$pos+$wide;
  78.  
  79. imagefilledrectangle($img, $pos,5,$pos-1+$thin,$height_bar,$cl_white);
  80. $pos=$pos+$thin;
  81.  
  82.  
  83. imagefilledrectangle($img, $pos,5,$pos-1+$thin,$height_bar,$cl_black);
  84. $pos=$pos+$thin;
  85.  
  86. if ($show_codebar) {
  87. imagestring($img, 5, 0, $height_bar+5, " ".$value, imagecolorallocate($img, 0, 0, 0));
  88. }
  89.  
  90. $this->put_img($img);
  91. }
  92.  
  93. function JSK_left($input,$comp){
  94. return substr($input,0,$comp);
  95. }
  96.  
  97. function JSK_right($input,$comp){
  98. return substr($input,strlen($input)-$comp,$comp);
  99. }
  100. function put_img($image,$file='test.gif'){
  101. if($this->into){
  102. imagegif($image,$this->file);
  103. } else {
  104. header("Content-type: image/gif");
  105. imagegif($image);
  106. }
  107. imagedestroy($image);
  108. }
  109. }


wywołanie wygląda tak:

  1. function generujLiczbe($liczba)
  2. {
  3. $len = strlen($liczba);
  4. $defLen = 12;
  5. $lenDif = $defLen-$len;
  6. $number = false;
  7. for ($i=0; $i < $lenDif; $i++)
  8. {
  9. $number .= 0;
  10. }
  11. return $number.$liczba;
  12. }
  13.  
  14. require_once('barcode.inc.php');
  15. $code_number = generujLiczbe(8);
  16. new barCodeGenrator($code_number, 0, 'barcode.png', 120, 50, true);


Z operacjami na obrazkach za bardzo sobie nie radze dlatego będę wdzięczny za wszelką pomoc
Pozdrawiam
lukash82
Rzuć okiem na to

imagecreatetruecolor
imagecolorallocate
imagefill

Wykorzystuję to u siebie do obróbki obrazków. Możesz wypełnić przestrzeń dowolnym kolorem. Pozdrawiam, Ł.
JamalBIG
A dało by się wstawić tło do wygenerowanego kodu kreskowego?

Rozwiązałem to w ten sposób, że do pliku tła (który jest plikiem 'głównym') dodaje znak wodny którym jest kod kreskowy:

  1. function cardGenrate($barcode)
  2. {
  3. $photo = 'images/watermark.png';
  4.  
  5. $image = imagecreatefrompng($photo);
  6. $watermark = imagecreatefrompng($barcode);
  7.  
  8. $width = imagesx($image);
  9. $height = imagesy($image);
  10. $watermark_width = imagesx($watermark);
  11. $watermark_height = imagesy($watermark);
  12.  
  13. imagecopymerge($image, $watermark, (($width - $watermark_width))-180, (($height - $watermark_height))-155, 0, 0, $watermark_width, $watermark_height, 100);
  14.  
  15. #header('Content-Type: image/jpeg');
  16. imagepng($image, $barcode);
  17. imagedestroy($image);
  18. imagedestroy($watermark);
  19. }


Brawo Ja biggrin.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.