Vlad
Vlad

Reputation: 101

How to properly show generated SVG from PHP

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>

Upvotes: 2

Views: 1790

Answers (2)

Jasom Dotnet
Jasom Dotnet

Reputation: 1307

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

Upvotes: 0

Ken Lee
Ken Lee

Reputation: 8063

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


?>

Upvotes: 1

Related Questions