Skip to content

Instantly share code, notes, and snippets.

@cereal-s
Last active May 23, 2019 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cereal-s/cb50659a708caf39affc40efa95bd1f9 to your computer and use it in GitHub Desktop.
Save cereal-s/cb50659a708caf39affc40efa95bd1f9 to your computer and use it in GitHub Desktop.
Import PNG to SVG with <image> tag
<?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