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

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 by Barand
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?

 

 

Edited by StevenOliver
learned to put PHP code into post with <>
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.