StevenOliver Posted October 28, 2018 Share Posted October 28, 2018 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 28, 2018 Share Posted October 28, 2018 (edited) If, say, QR.php is the file that creates the image and renders it to the screen QR.php <?php $im = imagecreate(50,50); // draw image here header("Content-type: image/png"); imagepng($im); imagedestroy($im); ?> then in your html page <img src="QR.php"> Edited October 28, 2018 by Barand Quote Link to comment Share on other sites More sharing options...
StevenOliver Posted October 28, 2018 Author Share Posted October 28, 2018 (edited) 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? Edited October 28, 2018 by StevenOliver learned to put PHP code into post with <> Quote Link to comment Share on other sites More sharing options...
Barand Posted October 28, 2018 Share Posted October 28, 2018 urlencode() should suffice $txt = urlencode("Text to be encoded"); echo "<img src=\"QR.php?parameter=$txt\">"; Quote Link to comment Share on other sites More sharing options...
StevenOliver Posted October 28, 2018 Author Share Posted October 28, 2018 Thank you! I understand. URLencode takes care of spaces! Thank you. Newbies (like me) seem to want to over-complicate things.You have the gift (and know-how and experience) of being able to make things simple and clean -- the way PHP is supposed to be! Quote Link to comment Share on other sites More sharing options...
kicken Posted October 30, 2018 Share Posted October 30, 2018 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) . '">'; Quote Link to comment Share on other sites More sharing options...
StevenOliver Posted October 30, 2018 Author Share Posted October 30, 2018 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.