<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>feDropShadow Archives - Alexandros Georgiou</title>
	<atom:link href="https://www.alexgeorgiou.gr/tag/fedropshadow/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.alexgeorgiou.gr/tag/fedropshadow/</link>
	<description>Balancing brackets for a living</description>
	<lastBuildDate>Mon, 22 Jul 2019 18:20:50 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://www.alexgeorgiou.gr/wp-content/uploads/2021/07/cropped-alexgeorgiou-icon-32x32.png</url>
	<title>feDropShadow Archives - Alexandros Georgiou</title>
	<link>https://www.alexgeorgiou.gr/tag/fedropshadow/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>❏ How to render an SVG with a feDropShadow filter to PNG raster</title>
		<link>https://www.alexgeorgiou.gr/render-svg-fedropshadow-filter/</link>
					<comments>https://www.alexgeorgiou.gr/render-svg-fedropshadow-filter/#respond</comments>
		
		<dc:creator><![CDATA[alexg]]></dc:creator>
		<pubDate>Thu, 30 Aug 2018 22:54:11 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[button]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[drop shadow]]></category>
		<category><![CDATA[feDropShadow]]></category>
		<category><![CDATA[icon]]></category>
		<category><![CDATA[ImageMagick]]></category>
		<category><![CDATA[inkscape]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[raster]]></category>
		<category><![CDATA[SVG]]></category>
		<category><![CDATA[vector graphics]]></category>
		<guid isPermaLink="false">https://www.alexgeorgiou.gr/?p=271</guid>

					<description><![CDATA[<p>Render an SVG that uses the feDropShadow filter to a raster file such as PNG, using a PHP script.</p>
<p>The post <a href="https://www.alexgeorgiou.gr/render-svg-fedropshadow-filter/">❏ How to render an SVG with a feDropShadow filter to PNG raster</a> appeared first on <a href="https://www.alexgeorgiou.gr">Alexandros Georgiou</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In one of my most recent geeky escapades, I was attempting to automate generation of some icons / buttons. Turns out all is fun and games, until someone uses <code>feDropShadow</code>.</p>
<h2>There&#8217;s more than one ways to raster an SVG</h2>
<p>There&#8217;s tons of reasons why you wouldn&#8217;t want to generate a bunch of icons with a clumsy UI, such as Adobe Illustrator or Inkscape. You can set up a script that dumps your icons out as SVG, then add something that auto-converts to PNG. Think of the possibilities:</p>
<ul>
<li><strong>Parametrize your pipeline:</strong> Flip one parameter, run again, voila! A new batch of icons, with a different color or stroke width or font family, all freshly baked!</li>
<li><strong>Add a time dimension:</strong> Run multiple times with some variation, then join the results to&nbsp;<a href="https://askubuntu.com/questions/648244/how-to-create-a-gif-from-the-command-line" target="_blank" rel="noopener noreferrer">create animated GIFs</a>.</li>
<li><strong>Expand the range of glyphs:</strong> With a small change in your code, you can render any range of unicode glyphs. Think currency symbols, tool buttons, numbers, etc.</li>
<li><strong>Tile all the buttons into a sprite:</strong> Then, use the CSS <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/background-position" target="_blank" rel="noopener noreferrer"><code>background-position</code></a> rule to display the right button at the right place. The browser will only do one HTTP request to download all the buttons.</li>
</ul>
<p>Why would you do all that, you say?</p>
<p>Well, for one, you could try to sell icon collections on places like <a href="https://www.vectorstock.com" target="_blank" rel="noopener noreferrer">vectorstock.com</a>. Once you have a pipeline ready, you can flood the market! Or so I theorize&#8230;</p>
<p>Also, code is beautiful!</p>
<h3>Way 1: Native PHP</h3>
<p>You could use the <a href="http://php.net/manual/en/book.imagick.php" target="_blank" rel="noopener noreferrer">PHP ImageMagick library</a> to generate your PNG file. In the following example, I will write out some SVG for the Bitcoin icon, using the <a href="https://www.fileformat.info/info/unicode/char/20bf/index.htm" target="_blank" rel="noopener noreferrer">Unicode character</a> and some <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform" target="_blank" rel="noopener noreferrer">rotation</a>.</p>
<pre>&lt;?php

ob_start();

$text = '&amp;#x20bf;';

?&gt;&lt;?xml version="1.0"?&gt;
&lt;svg
	xmlns="http://www.w3.org/2000/svg"
	xmlns:xlink="http://www.w3.org/1999/xlink"
	viewBox="0 0 64 64"&gt;

    &lt;circle
		fill="orange"
		stroke="transparent"
		cx="32"
		cy="32"
		r="31"
		stroke-width="0"&gt;
	&lt;/circle&gt;


    &lt;text
		transform="rotate(10 32 32)"
		x="50%"
		y="50%"
		text-anchor="middle"
		fill="white"
		font-size="36pt"
		stroke="0"
		dy=".3em"&gt;
		
		&lt;?php echo $text; ?&gt;
	&lt;/text&gt;
&lt;/svg&gt;
&lt;?php

$svg = ob_get_clean();

// write out the SVG to file
file_put_contents( 'bitcoin-icon.svg', $svg );

// now use Imagick to render the button to raster
$im = new Imagick();
$im-&gt;setBackgroundColor( new ImagickPixel( 'transparent' ) );
$im-&gt;readImageBlob( $svg );
$im-&gt;setImageFormat( "png24" );
$im-&gt;writeImage( 'bitcoin-icon.png' );
$im-&gt;clear();
$im-&gt;destroy();</pre>
<p>Not too shabby! You can now use the full power of PHP templating to wrap this code into loops and generate all kinds of icons. Parametrise colors, sizes, stroke widths, etc.</p>
<div id="attachment_272" style="width: 71px" class="wp-caption aligncenter"><img decoding="async" aria-describedby="caption-attachment-272" class="wp-image-272 size-full" src="https://www.alexgeorgiou.gr/wp-content/uploads/2018/08/bitcoin-icon.png" alt="Bitcoin icon generated using some custom PHP" width="61" height="61"><p id="caption-attachment-272" class="wp-caption-text">bitcoin-icon.png</p></div>
<h3>Way 2: Convert</h3>
<p><a href="https://www.imagemagick.org/" target="_blank" rel="noopener noreferrer">ImageMagick</a> can be invoked on Linux systems with the <code>convert</code> command.</p>
<pre>convert bitcoin-icon.svg bitcoin-icon.png</pre>
<h3>Way 3: Inkscape CLI</h3>
<p>Inkscape can be used from the command line to convert an SVG to PNG. Here&#8217;s how:</p>
<pre>inkscape -z bitcoin-icon.svg -e bitcoin-icon.png</pre>
<p>Depending on various details of your SVG file, and on the current phase of the moon, this way might give you a better result compared to the other two.</p>
<h2>Keep your face to the PNG and you cannot see a shadow</h2>
<p>If you are a novice user of Scalable Vector Graphics, you might think that you can add a drop shadow with this well-known incantation:</p>
<pre>&lt;svg ... &lt;!-- header goes here as before --&gt;

	&lt;defs&gt;
		&lt;filter id="shadow"&gt;
			&lt;feDropShadow dx="4" dy="8" stdDeviation="4"/&gt;
		&lt;/filter&gt;
	&lt;/defs&gt;

	&lt;g filter="url(#shadow)"&gt;
		&lt;!-- rest of SVG shapes go here as before --&gt;
	&lt;/g&gt;
&lt;/svg&gt;</pre>
<p>You could, but only web browsers would render the output with a shadow. None of the three methods above render the shadow correctly. In fact, even the preview renderer in <a href="https://en.wikipedia.org/wiki/GNOME_Files" target="_blank" rel="noopener noreferrer">Nautilus</a> screws up when it sees this filter.</p>
<h2>When small scripts begin to cast big shadows</h2>
<p>What to do now? Simple. A quick look at the <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Element/feDropShadow" target="_blank" rel="noopener noreferrer">MDN docs</a> reveals that <code>feDropShadow</code> is a <em>filter primitive</em>, and it is equivalent to a bunch of simpler filters, <a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Element/feMerge" target="_blank" rel="noopener noreferrer">merged</a> together. Let&#8217;s try the whole thing again, this time spelling the shadow out a bit more verbosely:</p>
<p><script src="https://gist.github.com/alex-georgiou/fa311a92184859dd33f5055a93adbca2.js"></script></p>
<p>It seems that the <code>feDropShadow</code> filter is not yet supported in ImageMagick. At least not in the version I&#8217;m using. But the primitives it depends on, are!</p>
<p>&nbsp;</p>
<div id="attachment_273" style="width: 131px" class="wp-caption aligncenter"><img decoding="async" aria-describedby="caption-attachment-273" class="size-full wp-image-273" src="https://www.alexgeorgiou.gr/wp-content/uploads/2018/08/bitcoin-icon-shadow.png" alt="Bitcoin icon with shadow, generated using some custom PHP" width="121" height="121"><p id="caption-attachment-273" class="wp-caption-text">bitcoin-icon.png</p></div>
<p>The post <a href="https://www.alexgeorgiou.gr/render-svg-fedropshadow-filter/">❏ How to render an SVG with a feDropShadow filter to PNG raster</a> appeared first on <a href="https://www.alexgeorgiou.gr">Alexandros Georgiou</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.alexgeorgiou.gr/render-svg-fedropshadow-filter/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
