Load SVG file on PHP server side - php

I want to load the svg file based on a parameter.
e.g., if Parameter = A then load A.svg, if parameter = B then load B.svg, and so on..
For this case, I'm using Codeigniter, so on Model I load the svg file like this:
Model.php
$svgTag = '<div id="svg-content" >'
. '<input type="radio" name="test"/>'
. file_get_contents( APPPATH. 'views/svg/'.$param.'.svg', TRUE)
. '</div>'
After that, I load the $svgTag on view.
View.php
<html>
<body>
<?php echo $svgTag; ?>
</body>
</html>
My expectation is the svg file will be loaded in the div#svg-content, but the actual result is that svg is loaded after tag <body> like this:RESULT
<html>
<body>
<svg>........</svg> <!-- Loaded SVG File -->
<div id="svg-content" >
<input type="radio" name="test"/>
</div>
</body>
</html>
I wanna add the event on radio button, IF it's clicked, then change the color of SVG image.
IF I use <object> I cannot apply this style.
NOTE: This is the content of file that I try to load:
<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 1280 800" enable-background="new 0 0 1280 800" xml:space="preserve">
<g>
<polygon points="358.3,524.2 328.3,524.2 328.3,665 415,665 415,640 358.3,640 "/>
</g>
</svg>
Is there any way to get the result like this..?
<html>
<body>
<div id="svg-content" >
<input type="radio" name="test"/>
<svg>........</svg> <!-- Loaded SVG File -->
</div>
</body>
</html>

Use the <img> tag to embed it in page, this will give you full controle of placement in html
$svgTag = '<div id="svg-content" >'
. '<input type="radio" name="test"/>'
. '<img src="'.APPPATH. 'views/svg/'.$param.'.svg'.'" alt="">'
. '</div>'
Here is awesome answer on which to use <img>, <object>, or <embed> for SVG files?

Finally I found the answer.
It should be use fread() function to load the SVG file.So, it should be like this:
$fh = fopen(APPPATH. 'views/svg/'.$param.'.svg', 'r');
$svg_file = fread($fh, 25000);
$svgTag = '<div id="svg-content" >'
. '<input type="radio" name="test"/>'
. $svg_file
. '</div>'

Related

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

problems using php and dompdf to output svg

I'm trying to display some SVG as a PDF with Dompdf, but the PDF output is blank. Can someone tell me what I need to change?
Here is some example code which produces the error.
<?php
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$statement = '
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.40.1 (20161225.0304)
-->
<!-- Title: boxes_and_circles Pages: 1 -->
<svg width="422pt" height="448pt"
viewBox="0.00 0.00 421.50 448.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 444)">
<title>boxes_and_circles</title>
<polygon fill="#ffffff" stroke="transparent" points="-4,4 -4,-444 417.5,-444 417.5,4 -4,4"/>
<!-- A -->
<g id="node1" class="node">
<title>A</title>
<polygon fill="none" stroke="#000000" points="136,-353.5 82,-353.5 82,-317.5 136,-317.5 136,-353.5"/>
<text text-anchor="middle" x="109" y="-331.3" font-family="Helvetica,sans-Serif" font-size="14.00" fill="#000000">A</text>
</g>
<!-- 1 -->
<g id="node7" class="node">
<title>1</title>
<ellipse fill="none" stroke="#000000" cx="102" cy="-234.5" rx="32.5" ry="32.5"/>
<text text-anchor="middle" x="102" y="-230.3" font-family="Helvetica,sans-Serif" font-size="14.00" fill="#000000">1</text>
</g>
<!-- A->1 -->
<g id="edge1" class="edge">
<title>A->1</title>
<path fill="none" stroke="#000000" d="M107.7485,-317.4432C106.9701,-306.2112 105.9343,-291.266 104.9565,-277.1578"/>
<polygon fill="#000000" stroke="#000000" points="108.4424,-276.8319 104.2593,-267.0979 101.4591,-277.316 108.4424,-276.8319"/>
</g>
</svg>
';
$dompdf->loadHtml($statement);
// Render the HTML as PDF
$dompdf->render();
//echo($statement);
// Output the generated PDF to Browser
$dompdf->stream();
?>
If I run echo($statement), the browser outputs this :
The PDF output by Dompdf is completely blank.
If I instead use $statement = "hello, world!", Dompdf correctly outputs hello, world! in the PDF.
What do I need to do to output this SVG image as PDF with Dompdf?
I got it to work by deleting the first line <?xml version="1.0" encoding="UTF-8" standalone="no"?>
Also, in the real code, I had to delete all links that were embedded in the svg.
Furthermore, I had to put the svg into an img tag, and add this base64_encode part as follows :
$html = '<img src="data:image/svg+xml base64,'.base64_encode($statement).'" width="100" height="100" />';
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream();
This was from trial and error, and through reading another post Dompdf not generating pdf properly from SVG

Regular expression replacment for gettyimages

I have editors giving me the embed code from gettyimages in this format:
<div class="getty embed image" style="background-color:#fff;display:inline-block;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;color:#a7a7a7;font-size:11px;width:100%;max-width:594px;">
<div style="padding:0;margin:0;text-align:left;">Embed from Getty Images</div>
<div style="overflow:hidden;position:relative;height:0;padding:65.656566% 0 0 0;width:100%;">
<iframe src="//embed.gettyimages.com/embed/473144498?et=jFJ38un7Qy1YOLsguPNmmA&viewMoreLink=on&sig=YGqYtdBCwZUYgO864KJJ6ulXuBuS1glNjjOGOHCJ28M=&caption=true" width="594" height="390" scrolling="no" frameborder="0" style="display:inline-block;position:absolute;top:0;left:0;width:100%;height:100%;margin:0;"></iframe>
</div><p style="margin:0;"></p></div>
I can't manipulate the image size with all those extra div tags from Gettyimages and tried to replace them to this format:
<iframe src="//embed.gettyimages.com/embed/473144498?et=jFJ38un7Qy1YOLsguPNmmA&viewMoreLink=on&sig=YGqYtdBCwZUYgO864KJJ6ulXuBuS1glNjjOGOHCJ28M=&caption=true" width="594" height="390"></iframe>
But I've had no luck so far. Can anyone help me?
I'd use the domdocument parser for this, not a regex.
<?php
$string = '<div class="getty embed image" style="background-color:#fff;display:inline-block;font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif;color:#a7a7a7;font-size:11px;width:100%;max-width:594px;">
<div style="padding:0;margin:0;text-align:left;">Embed from Getty Images</div>
<div style="overflow:hidden;position:relative;height:0;padding:65.656566% 0 0 0;width:100%;">
<iframe src="//embed.gettyimages.com/embed/473144498?et=jFJ38un7Qy1YOLsguPNmmA&viewMoreLink=on&sig=YGqYtdBCwZUYgO864KJJ6ulXuBuS1glNjjOGOHCJ28M=&caption=true" width="594" height="390" scrolling="no" frameborder="0" style="display:inline-block;position:absolute;top:0;left:0;width:100%;height:100%;margin:0;"></iframe>
</div><p style="margin:0;"></p></div>';
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($string);
libxml_clear_errors();
$doc->getElementsByTagName('iframe')->item(0)->removeAttribute('style');
echo $doc->saveHTML();
Output:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><div class="getty embed image" style="background-color:#fff;display:inline-block;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;color:#a7a7a7;font-size:11px;width:100%;max-width:594px;">
<div style="padding:0;margin:0;text-align:left;">Embed from Getty Images</div>
<div style="overflow:hidden;position:relative;height:0;padding:65.656566% 0 0 0;width:100%;">
<iframe src="//embed.gettyimages.com/embed/473144498?et=jFJ38un7Qy1YOLsguPNmmA&viewMoreLink=on&sig=YGqYtdBCwZUYgO864KJJ6ulXuBuS1glNjjOGOHCJ28M=&caption=true" width="594" height="390" scrolling="no" frameborder="0"></iframe>
</div><p style="margin:0;"></p></div></body></html>
This assumes you only have 1 iframe. If you have multiples assign
$doc->getElementsByTagName('iframe')
to a variable and then iterate through it.
If you only want the height, width, and src attributes it is probably better to select those then build the element. Otherwise you will have to remove every attribute the user could add..
So modifying the above approach:
$iframes = $doc->getElementsByTagName('iframe');
foreach($iframes as $iframe) {
$src = $iframe->getAttribute('src');
$height = $iframe->getAttribute('height');
$width = $iframe->getAttribute('width');
echo "<iframe src='$src' height='$height' width='$width'></iframe>";
}
This would give:
<iframe src='//embed.gettyimages.com/embed/473144498?et=jFJ38un7Qy1YOLsguPNmmA&viewMoreLink=on&sig=YGqYtdBCwZUYgO864KJJ6ulXuBuS1glNjjOGOHCJ28M=&caption=true' height='390' width='594'></iframe>
You also could use the " for the attribute encapsulation you'll just need to escape them, or concatenate the variables and use single quotes for encapsulation.
Too get them all #(( [a-z]+)="([^"])+")+# via perg_match_all

SVG - positioning won't work

I am new to SVG. I think there are two ways for me to insert SVG icons in my wordpress template.
1.: with "use"
<svg viewBox="0 0 100 100">
<use xlink:href="<?php bloginfo('template_directory'); ?>/icons/download.svg#Layer_1"></use>
</svg>
2.: as PHP file
get_template_part("icons/download")
("get_template_part" in this case refers to a "icon.php" file with the XML code that draws the SVG:
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="40px" height="33.999px" viewBox="0 0 40 33.999" style="enable-background:new 0 0 40 33.999;" xml:space="preserve">
<path style="fill-rule:evenodd;clip-rule:evenodd;" d="M39,14h-1v15.999c0,2.209-1.791,4-4,4H6c-2.209,0-4-1.791-4-4V14H1
c-0.552,0-1-0.448-1-1c0-0.552,0.448-1,1-1h1v-0.001h2v18c0,1.104,0.895,2,2,2h28c1.104,0,2-0.896,2-2v-18h2V12h1
c0.552,0,1,0.448,1,1C40,13.552,39.552,14,39,14z M20.708,13.706c-0.001,0.001-0.002,0.002-0.003,0.002l-0.009,0.009l0,0l0,0
c-0.087,0.087-0.189,0.147-0.294,0.196c-0.005,0.002-0.009,0.007-0.014,0.009c-0.369,0.163-0.814,0.098-1.117-0.205l-4.989-4.994
c-0.394-0.394-0.394-1.033,0-1.427c0.394-0.394,1.032-0.394,1.426,0L19,10.591V1c0-0.552,0.448-1,1-1c0.552,0,1,0.448,1,1v9.56
l3.261-3.264c0.393-0.394,1.031-0.394,1.425,0s0.394,1.033,0,1.427L20.708,13.706z"/>
</svg>
The first option doesn't work in IE (even 11!) without javascript.
The second option works in IE9 and above, that's cool! but i am not able to position this SVG. i have put it in a wrapper (".svg-wrapper") and tried to position it with CSS but the SVG appears outside of the wrapper in DOM :-(
Am i doing something wrong here?
echo '<li class="alignmiddle">' . '<div class="svgwrapper">' . get_template_part("icons/download") . '</div>' . $title . '.' . $path_info['extension'] . '<span class="filesize"> (' . $filesize . ')</span>' . '</li>';
Or is there a better solution at all?
I use the following code which allows you to define a fallback for older browsers. This method allows you to apply CSS in the same way you would a normal image. As SVG's are vector based you must assign a width and height.
<img src="<?php bloginfo('template_directory'); ?>/img/example.svg" onerror="this.onerror=null; this.src='<?php bloginfo('template_directory'); ?>/img/example.png'" alt="" />

SVG in PHP Error: Opening and ending tag mismatch: meta line 0 and head

I have a problem when i try to create an SVG in PHP. I manage to create SVG when i set the string manually (like: $string = "Hello";), but when i try to get a string from another page using sessions i get the error "error on line 3 at column 8: Opening and ending tag mismatch: meta line 0 and head".
<?php
//$string = "HEEEIA"; <--- It works when i manually insert the string text
$string = $_SESSION['text-from-last-page'];
header('Content-type: image/svg+xml');
echo '<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red"/>
<text x="80" y="90" style="font:size: 8">' . $string . '</text>
</svg>';
?>

Categories