Jump to content

Creating a PNG file with a someone else's class


niche

Recommended Posts

I'd like to output a PNG file from the script in the code box.Documentation for this script is at : http://www.barcodephp.com/1d/userguide.phpIf you're interested, the script can be downloaded at: http://www.barcodephp.com/download.phpI inserted a filename as the documentation suggested (find the text that begins with "The first argument is the filename").Any suggestions on how to get this script to create a *.png file?

<?phprequire('class/BCGFont.php');require('class/BCGColor.php');require('class/BCGDrawing.php');require('class/BCGcode39.barcode.php');$font = new BCGFont('./class/font/Arial.ttf', 18);$color_black = new BCGColor(0, 0, 0);$color_white = new BCGColor(255, 255, 255);$code = new BCGcode39();$code->setScale(2);$code->setThickness(30);$code->setForegroundColor($color_black);$code->setBackgroundColor($color_white);$code->setFont($font);$code->setChecksum(false);$code->parse('A123');// Drawing Part$drawing = new BCGDrawing('', $color_white);$drawing->setBarcode($code);$drawing->draw();header('Content-Type: image/png');$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);?> 

Link to comment
Share on other sites

If you want to save it as a PNG file you need to give it a filename. Regardless, you're not creating the image object, it looks like you can do that with either the init or set_im methods of the BCGDrawing class.

Link to comment
Share on other sites

The doc said the first argument was for a filename, but a filename there produces an error message. What do you think?

$drawing = new BCGDrawing('', $color_white);

Link to comment
Share on other sites

According to the code of the BCGDrawing class, the first argument to the constructor is the filename to save the file as. If that produces an error, the solution would be to fix that error instead of leaving out the filename.

Link to comment
Share on other sites

My OOP is weak, but I'm willing to try. I've found the file BCGDrawing.php. What do you recommend as the next couple of steps?

Link to comment
Share on other sites

The error is pretty vague: The image "http://localhost/temp99.php" cannot be displayed because it contains errors. I need to study on PHP Objects before proceeding.An alternative approach would be to simply position the barcode that's being successfully produced on screen, but I don't know how to use HTML and CSS the output of this script:How do you do that please?

<?phprequire('class/BCGFont.php');require('class/BCGColor.php');require('class/BCGDrawing.php');require('class/BCGcode39.barcode.php');$font = new BCGFont('./class/font/Arial.ttf', 18);$color_black = new BCGColor(0, 0, 0);$color_white = new BCGColor(255, 255, 255);$code = new BCGcode39();$code->setScale(2);$code->setThickness(30);$code->setForegroundColor($color_black);$code->setBackgroundColor($color_white);$code->setFont($font);$code->setChecksum(false);$code->parse('A123');// Drawing Part$drawing = new BCGDrawing('', $color_white);$drawing->setBarcode($code);$drawing->draw();header('Content-Type: image/png');$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);?> 

Link to comment
Share on other sites

You either save it as a file, or send it to the browser. You have the header function there sending a PNG header, so if you do that and send it to the browser, which happens when the filename is empty, then you can't also save it to disk. If you want to save the file to disk, then give it a filename. If you want to both save it as a file, and send it to the browser, then save it as a file first, send the content type header, then use file_get_contents to get the contents of the file and send those to the browser. The reason you're seeing the browser tell you that the image can't be displayed is because there is no image data, it's being saved to disk instead of sent to the browser.

Link to comment
Share on other sites

Turn on error reporting

ini_set('display_errors',1);error_reporting(E_ALL|E_STRICT);

Running your code, I got:PHP Fatal error: Call to undefined function imagecreate() in /var/www/html/test/barcode/bc/class/BCGFont.php on line 44Might be an installation or configuration issue.http://www.php.net/manual/en/image.installation.php

Link to comment
Share on other sites

Do you think the script is broken in that it apparently doesn't let me save the output to file, or am I just missing something (like where it's saving the file)?

Link to comment
Share on other sites

OK, I found the file. It didn't occur to me to look for it because of the error message. Thank-you very much for sticking with me.I pretty fried. Could you just show me the script that you had in mind when you said: " If you want to both save it as a file, and send it to the browser, then save it as a file first, send the content type header, then use file_get_contents to get the contents of the file and send those to the browser."?

Link to comment
Share on other sites

OK, I found the file. It didn't occur to me to look for it because of the error message.
The error message was from the browser, not PHP. The browser said it couldn't display an image because it was given an image content-type but no image data.If you're already saving it as a file, then that's fine (I'm curious though as to why you need to save every barcode on the server). You have the filename in the script, so you can use file_get_contents to get the data in that file and then use echo to send the data to the browser. If you're already sending the content-type header to tell the browser the image is a PNG then all you need to do is get the PNG data and send it to the browser also. If you have the filename then that's the only thing you need.
Link to comment
Share on other sites

I need the barcode saved as a file because I know how to position an image. I don't know how to position this on the browser:

<?phpecho file_get_contents("12345");?> 

12345 is a png file that easy to position with html and css, but produces a mess with file_get_contents.I would like to know how to use it in this situation.How would you do it?

Link to comment
Share on other sites

It's exactly the same, the browser doesn't care where an image comes from. An image is an image. You can display dynamic images perfectly fine on a page in exactly the same way you would position a static image. In your img tag, you set the PHP script as the src attribute. In the PHP script, you send a content-type header followed by the image data. You style the img tag the same way you style any other img tag, it doesn't matter that the src attribute is a PHP script instead of a static image. So it sounds like you don't need to save the file on the server, the only reason you would possibly need to is if you had to save the barcode together with an invoice or something and didn't want to re-generate the same image every time you viewed the invoice, for example. If you're just displaying it once on the screen there's no reason to save it (and doing so would require that you also have a garbage-collection routine to make sure the server doesn't get filled up with barcode images). If you don't need to save the file, then just skip giving the object a filename and have it output straight to the browser like it was.

Link to comment
Share on other sites

This doesn't work, but I think it in the right direction you are talking about:

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

Would you correct it please?

Link to comment
Share on other sites

Try putting the image tag in the page, not in the PHP file and requesting the PHP script.<img src="barcodemaker.php" />Also - you may run into caching issues, in which case, adding a timestamp to the request would help.

Link to comment
Share on other sites

Thanks for the tip on styling the PHP with HTML and CSS. Now I need to understand you when you said: " In the PHP script, you send a content-type header followed by the image data." I Don't know what that would look like. Can you show me using the PHP we've been using?

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

Thanks

Link to comment
Share on other sites

<?php	// File starts here	$drawing = new BCGDrawing('', $color_white);	$drawing->setBarcode($code); // you will have to get the code from the querystring or similar	$drawing->draw();	header('Content-Type: image/png');	echo $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);	// File ends here?>

Basically, you can't have two different document types in one script - you need to separate the different file types out.

Link to comment
Share on other sites

I'm lost then. justsomeguy said to style my PHP script as the image source. wirehopper clarified that the script is the entire file and viola.Now I'm working to use "a content-type header followed by the image data (also per justsomeguy)" and wasn't expecting Synook's comment that "you will have to get the code from the querystring or similar". I don't know what that means.Can you provide clarification please? If it helps, here's the whole script:

<?phprequire('class/BCGFont.php');require('class/BCGColor.php');require('class/BCGDrawing.php');require('class/BCGcode39.barcode.php');$font = new BCGFont('./class/font/Arial.ttf', 18);$color_black = new BCGColor(0, 0, 0);$color_white = new BCGColor(255, 255, 255);$code = new BCGcode39();$code->setScale(2);$code->setThickness(30);$code->setForegroundColor($color_black);$code->setBackgroundColor($color_white);$code->setFont($font);$code->setChecksum(false);$code->parse('A123');// Drawing Part$drawing = new BCGDrawing('', $color_white);$drawing->setBarcode($code);$drawing->draw();header('Content-Type: image/png');$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);

Link to comment
Share on other sites

The code you just posted is already sending the image data. That's what that class does when you use the finish method. The documentation for the class explains all of this.

We now change the header to say to the browser that we will output an image. If you output it to a file, you don't have to put this line.
To finish, call the finish method with the argument BCGDrawing::IMG_FORMAT_PNG or BCGDrawing::IMG_FORMAT_JPEG to have a png or a jpg file. If you have specified a filename before, the image will be saved into this file, otherwise it will be displayed.
Link to comment
Share on other sites

Obviously, I was being a little thick. I get it now!Thanks to everyone: justsomeguy, wirehopper and Synook.Niche

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...