1

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>
Vlad
  • 91
  • 5
  • Does this answer your question? [Convert SVG image to PNG with PHP](https://stackoverflow.com/questions/4809194/convert-svg-image-to-png-with-php) – Ken Lee Feb 05 '23 at 04:55
  • @Ken Lee, Thanks, but I don't need to convert svg to png... I need to parse the generated svg code and paste it as an image, not an object. – Vlad Feb 05 '23 at 05:00
  • I see, please see my answer below – Ken Lee Feb 05 '23 at 05:18

2 Answers2

1

One of the ways is to display the SVG by echoing the generated SVG thru the PHP file_get_contents command

So say if the generate-svg.php is like the following (As an example, I generate a red circle):

DEMO

<!DOCTYPE html>
<html>
<body>
<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">

  <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />

</svg>

Then use the following to display it (along with other things you want to display):

DEMO

<?php 
echo "This is a line <br>";
echo file_get_contents("generate-svg.php"); 
echo "<br>This is another line <br>";
?>

[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 :

echo '<img src="generate-svg2.php">';

So, the generate-svg2.php can be:

DEMO

<?php

    include('./phpqrcode-master/qrlib.php');
    
    // outputs image directly into browser, as PNG stream
  echo QRcode::svg('SO is good');

?>

then the script to display it (along with other HTML elements) can be :

DEMO

<?php 
echo "This is a line <br>";
//echo file_get_contents("generate-svg.php"); 

echo '<img src="generate-svg2.php">';

echo "<br>This is another line x<br>";


?>
Ken Lee
  • 6,985
  • 3
  • 10
  • 29
  • @Kee Lee, please see my question again - if I use `echo file_get_contents('generate-svg.php');` I see this HTML code: ` ` – Vlad Feb 05 '23 at 15:50
  • I just know that you will use PHP to render the SVG, in that case please refer to the "Additional Point" part of my revised answer above. – Ken Lee Feb 05 '23 at 16:21
  • @Kee Lee, Thank you, this is code is valid, but I want get tag from first page (generate-svg.php) for insert to second page... This is possible? – Vlad Feb 05 '23 at 16:33
  • If you want to have .... , then you may use `include` (For example, `` ) . However, I don't know whether the qrlib.php will cause any header output when rendering the svg which will in turn cause further HTML elements to become invalid but you can try it by yourself – Ken Lee Feb 05 '23 at 16:51
0

First something with Output Control Functions:

ob_start();

require 'generate-svg.php';

$svg = ob_get_clean();

file_put_contents('svgs/generated-svg.svg', $svg);

Then:

<img src="/svgs/generated-svg.svg" height="200" width="200" alt="QR code">
Jasom Dotnet
  • 1,225
  • 12
  • 17