Jump to content

PHP-generated image WITH html?


StevenOliver

Recommended Posts

Always had trouble echoing php-generated image with HTML. For example, a QR code generator converts text to a QR code "png" image (the QR image then appears on a page of its own with nothing else.) However, I want the image embedded in my html page like this:

echo 'hello this is my image'. QRcode::png("some words here");

I can write the image to a file, then have my html page read the file... but there should be a more direct way of echoing the image in html without the extra step of having to create files to be created stored and then read.

Link to comment
Share on other sites

Okay, I understand. the "qr.php" page becomes the image itself! Very nice! Thank you!

Question: Using your example, are the parameters supposed to be sent via  "GET" like this:

$text_to_be_encoded = "hello here is some text to turn into a QR code";
echo '<img src="QR.php?parameters='.$text_to_be_encoded.'">';

If that is the correct way, do I have to worry about "special characters" should I need something like this:

// urlencode it (preserves code)
// base64 encode it (in case there are spaces in the text)
$text_to_be_encoded = base64_encode(urlencode('hello here is some text'));
echo '<img src="QR.php?parameters='.$text_to_be_encoded.'">';

What do you think?

 

 

Link to comment
Share on other sites

For small images, another alternative would be to generate a data: url using the image data.  For example, assuming QRCode::png returns the binary data for your image:

$imageData = QRCode::png('Text to encode');

echo 'Here is your QR Code: <img src="data:image/png;base64,' . base64_encode($imageData) . '">';

 

Link to comment
Share on other sites

58 minutes ago, kicken said:

For small images, another alternative would be to generate a data: url using the image data.  For example, assuming QRCode::png returns the binary data for your image:


$imageData = QRCode::png('Text to encode');

echo 'Here is your QR Code: <img src="data:image/png;base64,' . base64_encode($imageData) . '">';

 

Oh wow, I wish this would have worked! I tried it just now and I remember trying something similar a while ago... but it doesn't work.

The first part "$imageData = QRcode::png('Text to encode')" yields a bunch of weird characters in my browser, and then the <img src= part yields a blank image placeholder.

I'll have to go through the qr coding scripts to see if QRCode::png returns binary data. Thank you anyway, though!

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.