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>
3
  • Does this answer your question? Convert SVG image to PNG with PHP
    – Ken Lee
    Feb 5 at 4: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 5 at 5:00
  • I see, please see my answer below
    – Ken Lee
    Feb 5 at 5:18

2 Answers 2

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>";


?>
4
  • @Kee Lee, please see my question again - 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
    Feb 5 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 5 at 16:21
  • @Kee Lee, Thank you, this is code is valid, but I want get tag <svg></svg> from first page (generate-svg.php) for insert to second page... This is possible?
    – Vlad
    Feb 5 at 16:33
  • If you want to have <svg>....</svg> , then you may use include (For example, <?php include "generate-svg2.php";?> ) . 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 5 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">
2
  • 1
    but I have first file generate-svg.php.... not generate-svg.svg :(
    – Vlad
    Feb 5 at 15:48
  • @Vlad Do it works for you now? Feb 6 at 9:34

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.