I am generating svg qr codes via php and getting result in php file the following code:
generate-svg.php
<?
include("../qrcode/qrlib.php");
QRcode::svg('Example');
?>
And I see in HTML code in browser:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" xmlns:xlink="http://www.w3.org/1999/xlink" width="111" height="111" viewBox="0 0 111 111">
<desc></desc>
<rect width="111" height="111" fill="#ffffff" cx="0" cy="0" />
<defs>
<rect id="p" width="3" height="3" />
</defs>
<g fill="#000000">
<use x="12" y="12" xlink:href="#p" />
</g>
</svg>
Then I need to paste this svg code as an image into another php file. I do it like this:
svg.php
<?
echo '<object type="image/svg+xml" data="generate-svg.php" class="icon-qr"></object>';
?>
But I need to get the svg code and paste it as a picture. How can I do that?
If I use
echo file_get_contents('generate-svg.php');
I see this HTML code:
<!--?
include("../qrcode/qrlib.php");
QRcode::svg('Example');
?-->
<html><head></head><body></body></html>
2
Answers
One of the ways is to display the SVG by echoing the generated SVG thru the PHP
file_get_contents
commandSo say if the generate-svg.php is like the following (As an example, I generate a red circle):
DEMO
Then use the following to display it (along with other things you want to display):
DEMO
[Additional Point]
If you are rendering the SVG thru a PHP script, then if you use
file_get_contents
on a local file it will faithfully display the PHP script instead of executing it unless you use a URL starting with http:// or https://. You can choose to use something like (assuming that the php script is named as generate-svg2.php) the following :So, the
generate-svg2.php
can be:DEMO
then the script to display it (along with other HTML elements) can be :
DEMO
First something with Output Control Functions:
Then: