'Is there a way to execute PHP code in a SVG Image?
Even after hours of research I couldn't find any way to get php code executed through a svg image on an apache server running php.
Is it possible to execute php code in a svg image?
If yes, how?
UPDATE: The function processing the file is:
libxml_disable_entity_loader(false);
$doc = new DOMDocument();
$doc -> loadXML(file_get_contents($imageFile), LIBXML_NOENT | LIBXML_DTDLOAD); // $imageFile contains the .svg
$svg = $doc->getElementByTagName('svg');
echo $svg->item(0)->C14N();
Now I would like to execute custom PHP code that I wrote in the SVG and get the result displayed by echo $svg->item(0)->C14N();.
The PHP code must be executed on the back-end server that contains above function.
Solution 1:[1]
You can configure your server to do so but that is not the best practice.
Add the forth line to httpd.conf
AddType application/x-httpd-php .php
AddType application/x-httpd-phps .phps
AddType application/x-httpd-php3 .php3 .phtml
AddType application/x-httpd-php .svg
The downside to this is every request for an .svg will take longer as it runs as a php. And that <?xml will need to be changed on every svg file since by default PHP wants to use <? as for shorthand ... not good.
Alternatively you can use .htaccess within a directory so when you call mysvg.svg it runs mysvg.php
RewriteEngine on
RewriteRule ^(.+)\.svg$ $1.php [L]
But the best practice is to send the svg from a php. Example: How do I load a SVG file that's been generated with PHP? Why hide the fact that the SVG is being dynamically created by php?
Update: If you want to run some php code triggered by an SVG being rendered.
<svg width="200" height="200"
xmlns="http://www.w3.org/2000/svg">
<image href="http://example.com/pixel/" height="1" width="1"/>
</svg>
Note: http://example.com/pixel/index.php script delivers a png pixel and runs some code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
