Last active
May 23, 2019 09:06
-
-
Save cereal-s/cb50659a708caf39affc40efa95bd1f9 to your computer and use it in GitHub Desktop.
Import PNG to SVG with <image> tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Import PNG to SVG with <image> tag | |
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/SVG_Image_Tag | |
*/ | |
$ext = 'png'; | |
$mime = 'image/png'; | |
$files = glob('*.' . $ext); | |
$fname = ''; | |
$fbase = ''; | |
$fdata = null; | |
$f64data = null; | |
$template = <<<'SVG' | |
<!-- filename: %s --> | |
<svg width="100px" height="100px" x="0" y="0" version="1.1" | |
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> | |
<image x="0" y="0" height="100px" width="100px" xlink:href="data:%s;base64,%s" /> | |
</svg> | |
SVG; | |
foreach($files as $f) | |
{ | |
$fname = pathinfo($f)['filename']; | |
$fbase = pathinfo($f)['basename']; | |
print 'file: ' . $fbase . PHP_EOL; | |
$fdata = file_get_contents($f); | |
$f64data = base64_encode($fdata); | |
file_put_contents($fname . '.svg', sprintf($template, $fbase, $mime, $f64data)); | |
} | |
print PHP_EOL . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment