user3231308
user3231308

Reputation: 83

How to save generated barcode into folder as an img?

I'm a beginner. I'm trying to generate barcode and save into a folder 'uploads' as an image. Here is my code, but it doesn't work. What is the problem? Any help will be greatly appreciated.

    // Including all required classes
    require_once('class/BCGFontFile.php');
    require_once('class/BCGColor.php');
    require_once('class/BCGDrawing.php');

    // Including the barcode technology
    require_once('class/BCGcode128.barcode.php');

    $font = new BCGFontFile('./font/Arial.ttf', 18);

    // The arguments are R, G, B for color.
    $color_black = new BCGColor(0, 0, 0);
    $color_white = new BCGColor(255, 255, 255);


        $code = new BCGcode128();
        $code->setScale(2); // Resolution
        $code->setThickness(30); // Thickness
        $code->setForegroundColor($color_black); // Color of bars
        $code->setBackgroundColor($color_white); // Color of spaces
        $code->setFont($font); // Font (or 0)
        $code->parse('hi'); // Text


    $drawing = new BCGDrawing('../uploads/',$color_white);
    $drawing->setBarcode($code);
    $drawing->draw();


    // Draw (or save) the image into PNG format.
    $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);

Upvotes: 3

Views: 3717

Answers (3)

donald123
donald123

Reputation: 5749

When you have take a look into the manual for ->finish() you find this

 Writes the image in the specified file. The file has been specified when you constructed the class, if you didn't, the image will be displayed.

Your solution can be find under http://www.barcodephp.com/en/manual/drawing#__construct

Upvotes: 0

Ivan Yonkov
Ivan Yonkov

Reputation: 7034

You uploads folder should be writtable by the webserver. Also the documentation states you need a filename

construct">http://www.barcodephp.com/en/manual/drawing#_construct

Think about using setFilename()

Upvotes: 0

Zvonimir Burić
Zvonimir Burić

Reputation: 548

You have to set the proper filename. Like this:

$drawing = new BCGDrawing('../uploads/filename.png', $color_white);

or this:

$drawing->setFilename('../uploads/filename.png');

Upvotes: 2

Related Questions