3

I was wondering if I could save bar code in database. I am using "Code 39" type of barcode. Barcode will be generated from variable. I wanted to save it to database if possible. I have downloaded library from "http://www.barcodebakery.com". Can anyone give me suggestion how to do so?

Code works fine it displays the image, later on I will use bar code scanner to look for products, each of them will contain bar code.

<?php
    require_once('class/BCGFontFile.php');
    require_once('class/BCGColor.php');
    require_once('class/BCGDrawing.php');
    require('class/BCGcode39.barcode.php');

    $font = new BCGFontFile('font/Arial.ttf', 18);
    $color_black = new BCGColor(0, 0, 0);
    $color_white = new BCGColor(255, 255, 255);

    $barcode="3RC402A00";

    // Barcode Part
    $code = new BCGcode39();
    $code->setScale(2);
    $code->setThickness(30);
    $code->setForegroundColor($color_black);
    $code->setBackgroundColor($color_white);
    $code->setFont($font);
    $code->parse($barcode);

    // Drawing Part
    $drawing = new BCGDrawing('', $color_white);
    $drawing->setBarcode($code);
    $drawing->draw();
    header('Content-Type: image/png');
    $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
?>

Any suggestion is appreciated. Thanks

jsgoupil
  • 3,788
  • 3
  • 38
  • 53
user3651819
  • 497
  • 6
  • 22
  • save image as blob in database or save just the $barcode in the database and have a genbarcode.php page that you include in your img src="" bit that generates the barcode no the fly (this is how I do it in my system) – Dave Oct 22 '14 at 12:39
  • 2
    I don't get it, do you want to save the barcode as an image? what's wrong with saving just the code? way easier and lighter – martskins Oct 22 '14 at 12:40

2 Answers2

2

You can

  1. Save the actual code (string) in the database, and recreate the generation code on demand. This is the preferred method.
  2. You can save the image to your system and link to the image file with a location (path) in your database.
  3. You can save the image as a BLOB field in your database.
  4. You might be able to serialize the $drawing object, and recreate this afterwards. Don't do this.

So bottomline: save your string to the database, but if you really can't recreate the code on demand: save the image and link to that.

Nanne
  • 64,065
  • 16
  • 119
  • 163
  • Thanks for fast reply. I want to store an image so later I can print the image after each product. Beside that, I will make a script to identify the products via bar code that is the reason why I need string and image. – user3651819 Oct 22 '14 at 12:54
  • The string and the image are the same information. If you have the string, you can create the image. Hence there is no need to actually save the image, is there? If you save the string, and you want to print the image, call above code, create the image, and do with it what you want (e.g. print). If that is not possible as you cannot run the code (that would be strange, so you might be fixing the wrong problem there), but if this is the case, you can with option 2 or 3. – Nanne Oct 22 '14 at 13:16
0
$drawing = new BCGDrawing('yourfilename.png', $color_white);

That would save the image to a file.

To save a file into database, refer to this stackoverflow post.

How can I store and retrieve images from a MySQL database using PHP?

Community
  • 1
  • 1
Veeru
  • 4,936
  • 2
  • 43
  • 61
  • Thanks for fast reply. I want to store an image so later I can print the image after each product. Beside that, I will make a script to identify the products via bar code that is the reason why I need string and image. – user3651819 Oct 22 '14 at 12:52
  • Yeah, got your question. You have to use a BLOB type field in your database (assuming mysql). And save the image data there. Refer here: http://stackoverflow.com/questions/17717506/how-to-upload-images-into-mysql-database-using-php-code – Veeru Oct 22 '14 at 12:53