CONTEXT
I have a svg file shaped like following :
<svg width="100px" height="150px">
<g>
<linearGradient id="a" gradientUnits="userSpaceOnUse" x1="2125.9858">
</linearGradient>
</g>
</svg>
Note this is an example, in reality my svg is bigger.
GOAL
I would like to input this as a string in php, and find all float, then finally convert those to whole number. The aim of this script is to round all number to reduce the load for rendering in client screen.
EXPECTED OUTPUT
<svg width="100px" height="150px">
<g>
<linearGradient id="a" gradientUnits="userSpaceOnUse" x1="2125">
</linearGradient>
</g>
</svg>
QUESTION
Can I use str_replace to do the job ? I thought of something that would be near this command :
$svg = "...";
$svg = str_replace("/[0-9.]/", floor(the_occurence_found), $svg);
Is that possible ? Or am I going right on the wall ?
Related
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
I am using the same SVG multiple times on a webpage. The SVGs are inserted on the server side with PHP like this:
<?php echo file_get_contents("myimage.svg"); ?>
The SVG contains a gradient, which should have different colors on different instances of the SVG.
An HTML document delivered by the server may look like the following snippet. The same SVG has been inserted twice:
#image1 .stop1 { stop-color: #FDF39C }
#image1 .stop2 { stop-color: #FE8A77 }
#image2 .stop1 { stop-color: #64E8EA }
#image2 .stop2 { stop-color: #A79CFC }
<div id="image1">
<svg width="256" height="256" viewBox="0 0 256 256">
<defs>
<linearGradient id="gradient1">
<stop class="stop1" offset="0%"/>
<stop class="stop2" offset="100%"/>
</linearGradient>
</defs>
<circle fill="url(#gradient1)" cx="128" cy="128" r="100" />
</svg>
</div>
<div id="image2">
<svg width="256" height="256" viewBox="0 0 256 256">
<defs>
<linearGradient id="gradient1">
<stop class="stop1" offset="0%"/>
<stop class="stop2" offset="100%"/>
</linearGradient>
</defs>
<circle fill="url(#gradient1)" cx="128" cy="128" r="100" />
</svg>
</div>
The problem is that both instances of the SVG display the gradient defined in the first SVG. Here is a comparison of the actual vs the intended result:
The reason for this is that the gradients of both inline SVGs have the same ID gradient1, because the same SVG which contains that ID is inserted twice on the server. The circle elements' fill is set to url(#gradient1) in both cases, which the browser simply links to the first occurence of that ID, which is the gradient definition in the first SVG. This behavior is correct, but for my case this is a problem.
The question is: How do I avoid duplicate ids? I worked with SVG injection using SVGInject, and this solved the problem by making the IDs unique. SVGInject simply adds a random string to the end of IDs, for example changing gradient1 to gradient1-h4w7xo82.
However, I do not see a solution when inserting the SVGs with PHP. Can you help?
Convert the svg file into php so you can pass the gradientId parameter into it.
<?php
header('Content-Type: image/svg+xml');
echo '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="256" height="256" viewBox="0 0 256 256">
<defs>
<linearGradient id="' . $gradientId . '">
<stop class="stop1" offset="0%"/>
<stop class="stop2" offset="100%"/>
</linearGradient>
</defs>
<circle fill="url(#' . $gradientId . ')" cx="128" cy="128" r="100" />
</svg>';
?>
CSS Based Solution
I was having the same problem.
The solution to the above problem is, change the css class, so that different css class is targeting different object.
Change -
CSS Class of 2nd element, I have change it to #image2 .stop1-df, appended additional "df"
Linear Gradient ID - of second element - gradient1-df
Circle fill url - #gradient1-df
#image1 .stop1 { stop-color: #FDF39C }
#image1 .stop2 { stop-color: #FE8A77 }
#image2 .stop1-df { stop-color: #64E8EA }
#image2 .stop2-df { stop-color: #A79CFC }
<div id="image1">
<svg width="256" height="256" viewBox="0 0 256 256">
<defs>
<linearGradient id="gradient1">
<stop class="stop1" offset="0%"/>
<stop class="stop2" offset="100%"/>
</linearGradient>
</defs>
<circle fill="url(#gradient1)" cx="128" cy="128" r="100" />
</svg>
</div>
<div id="image2">
<svg width="256" height="256" viewBox="0 0 256 256">
<defs>
<linearGradient id="gradient1-df">
<stop class="stop1-df" offset="0%"/>
<stop class="stop2-df" offset="100%"/>
</linearGradient>
</defs>
<circle fill="url(#gradient1-df)" cx="128" cy="128" r="100" />
</svg>
</div>
If you use https://jakearchibald.github.io/svgomg/ it will convert all styles to URL-parameters, like this: id="a" and url("a"). If you want multiple SVGs on one page, they interfere with eachother.
I needed to have several SVGs on the same page, so I made a small util for my self. It will replace those a/b/c urls with a parameter you can insert for your self, so you can have 1 SVG with one_a, one_b etc and the second SVG with second_a, second_b etc. It uses regex, obviously:
# replace id="a" with id="test_a"
removeStr = "id=\"#{strChar}\""
regex = new RegExp(removeStr,'g')
strInput = strInput.replace(regex,"id=\"#{strName}_#{strChar}\"")
You can find it here:
https://codepen.io/pwiegers/pen/YzreGGo
It saved me a ton of time; it might save you some :-)
I want to convert SVG images to PNG keeping transparency. In the case of solid color shapes works fine, but when the shape is filled with a gradient, then the output image renders an unexpected solid white background.
I want some solution using Imagick library, but some code to preproccess svg code is also acceptable.
Here a minimal example of the issue:
<?php
$svg = <<<SVG
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="100" height="100">
<ellipse style="fill:red" cx="50" cy="50" rx="50" ry="50" />
</svg>
SVG;
$img = new \Imagick();
$img->readImageBlob($svg);
$img->setImageBackgroundColor('transparent');
//IMAGE WITH NICE TRANSPARENT BACKGROUND
$img->writeImage(__DIR__ . '/circle_plain.png');
$svg = <<<SVG
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="100" height="100">
<defs>
<linearGradient
id="linearGradient1">
<stop
style="stop-color:#ff0000;stop-opacity:1;"
offset="0"/>
<stop
style="stop-color:#00ff00;stop-opacity:1;"
offset="1"/>
</linearGradient>
<linearGradient
xlink:href="#linearGradient1"
id="linearGradient2"
x1="0"
y1="50"
x2="100"
y2="50"
gradientUnits="userSpaceOnUse" />
</defs>
<ellipse style="fill:url(#linearGradient2)" cx="50" cy="50" rx="50" ry="50" />
</svg>
SVG;
$img = new \Imagick();
$img->readImageBlob($svg);
$img->setImageBackgroundColor('transparent');
//IMAGE WITH WRONG WHITE BACKGROUND!!
$img->writeImage(__DIR__ . '/circle_gradient.png');
My Ubuntu 14.04 laptop has GraphicsMagick and the PHP extension (GMagick) installed. When I run the following from the command line,
gm convert -font /usr/share/fonts/truetype/google-fonts/RockSalt.ttf svg_file.svg jpeg_file.jpg
renders the JPEG with the font as desired. However, this PHP code
$gm = new Gmagick();
$draw = new GmagickDraw();
$draw->setfont("/usr/share/fonts/truetype/google-fonts/RockSalt.ttf");
$gm->readImageBlob($svg);
$gm->setImageFormat("jpeg");
$gm->drawimage($draw);
$gm->writeImage($jpgFile);
$gm->clear();
Fails to do the same and creates a JPEG with a default font. What could be the issue here?
This is the SVG:
<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="737" height="521"><defs/> <g class="main"> <title>Main</title> <rect fill="#ffffff" stroke-width="0" x="27.24188" y="429.44421" width="682.51626" height="68.18413" id="svg_2" class="element" opacity="0.75" stroke="#000000"/> <g text-anchor="start" font-family="Rock Salt" fill="#000" font-size="20" class="element textarea" id="svg_3"> <rect opacity="0" stroke-width="0" x="27.24188" y="429.44421" width="682.51626" height="68.18413" id="svg_4"/> <g class="textGroup"> <text class="eol" y="470.98331" x="44.46314" xml:space="preserve">Your memories will last forever</text> </g> </g> </g></svg>
You need to edit GraphicMagick's type.mkg file so the font will load.
https://sourceforge.net/p/graphicsmagick/discussion/250738/thread/5c00e815/
Here is an example of the required format:
https://github.com/CliffsDover/graphicsmagick/blob/master/config/type-windows.mgk.in
<?xml version="1.0"?>
<typemap>
<type
name="Arial"
fullname="Arial"
family="Arial"
weight="400"
style="normal"
stretch="normal"
glyphs="#windows_font_dir#arial.ttf"
/>
<type
name="Arial-Black"
fullname="Arial Black"
family="Arial"
weight="900"
style="normal"
stretch="normal"
glyphs="#windows_font_dir#ariblk.ttf"
/>
<type
name="Arial-Bold"
fullname="Arial Bold"
family="Arial"
weight="700"
style="normal"
stretch="normal"
glyphs="#windows_font_dir#arialbd.ttf"
/>
I would like to get SVG tag content with PHP.
test.svg:
<?xml version="1.0" encoding="utf-8"?>
<!-- comment -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<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="202.5px" height="226.084px" viewBox="0 0 202.5 226.084" enable-background="new 0 0 202.5 226.084" xml:space="preserve">
<g>
<path d="M0,13.628c47.7940,13.628z"/>
<polygon points="108.48,207.874 145.506,196.948 145.506,204.536 108.48,214.854 "/>
<path fill="none" stroke="#000000" d="M114.55,223.959"/>
</g>
<g>
<path fill="none" stroke="#000000" d="M114.55,223.959"/>
</g>
<anythingElse>
<path fill="none" stroke="#000000" d="M114.55,223.959"/>
<anythingElse>
</svg>
php:
$svg = new SimpleXMLElement( file_get_contents( 'test.svg' ) );
$svg->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');
$svg->registerXPathNamespace('xlink', 'http://www.w3.org/1999/xlink');
Now, I would like to get the svg tag content as a string.
Desired result (hardcoded example):
$content = '<g>
<path d="M0,13.628c47.7940,13.628z"/>
<polygon points="108.48,207.874 145.506,196.948 145.506,204.536 108.48,214.854 "/>
<path fill="none" stroke="#000000" d="M114.55,223.959"/>
</g>
<g>
<path fill="none" stroke="#000000" d="M114.55,223.959"/>
</g>
<anythingElse>
<path fill="none" stroke="#000000" d="M114.55,223.959"/>
<anythingElse> ';
//EDIT:
I don't want to look for g tag: '/svg:svg/svg:g'.
As there is no guarantee that inside the svg there will be a g tag. There could be more then one g tag or some other tags.
I want to get everything between the opening and closing svg tags.
You have already seen right (more likely: copy and pasted from an example code that has been given to you in a previous answer w/o understanding it further) that you need to register the XML namespace here for your xpath because there is no prefix for it in the XML:
<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="202.5px" height="226.084px" viewBox="0 0 202.5 226.084" enable-background="new 0 0 202.5 226.084" xml:space="preserve">
It is just the URI http://www.w3.org/2000/svg, no prefix given. It is the namespace of that <svg> root element.
Therefore you need to register the prefix (the other already prefixed namespace should be already registered automatically):
$svg->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');
Which is what you do so far.
To now use your registered prefix in the xpath query, prefix the tagname with it:
/svg:svg/svg:g[1]
Should give you the first <g> element in that SVG-Namespace. Let me know if this still needs further explanation / clarification.
See the online demo.
This is also exactly as the reason outline to your previous question:
Loop through SVG elements with PHP
I think to get onle the g tag you don;t need namespace you could try this:
$result = $svg->xpath('svg/g');
foreach ($result as $gtag) {
echo $gtag . "\n";
}
I can't test it though:)