<?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>Eric Hynds &#187; icons</title>
	<atom:link href="http://www.erichynds.com/tag/icons/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.erichynds.com</link>
	<description>Web Developer&#039;s Ramblings on JavaScript, jQuery, ColdFusion, MySQL, and other technologies.</description>
	<lastBuildDate>Mon, 02 Jan 2012 22:55:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>FamFamFam Silk Icon Set in Base64 Encoding</title>
		<link>http://www.erichynds.com/coldfusion/famfamfam-silk-icon-set-in-base64-encoding/</link>
		<comments>http://www.erichynds.com/coldfusion/famfamfam-silk-icon-set-in-base64-encoding/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 15:05:26 +0000</pubDate>
		<dc:creator>Eric Hynds</dc:creator>
				<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[base64]]></category>
		<category><![CDATA[icons]]></category>

		<guid isPermaLink="false">http://www.erichynds.com/?p=348</guid>
		<description><![CDATA[This one can be filed under both &#8220;total impractical&#8221; and &#8220;somewhat useful&#8221;. For those who haven&#8217;t heard of this trick before, base64 encoding enables you to embed binary image data directly into your HTML or CSS source. Therefore, instead of the browser requesting two files from the server &#8211; the CSS file and the image [...]


Related posts:<ol><li><a href='http://www.erichynds.com/coldfusion/test/' rel='bookmark' title='CFIMAGE resize after CFFILE upload'>CFIMAGE resize after CFFILE upload</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.erichynds.com%2Fcoldfusion%2Ffamfamfam-silk-icon-set-in-base64-encoding%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.erichynds.com%2Fcoldfusion%2Ffamfamfam-silk-icon-set-in-base64-encoding%2F&amp;source=erichynds&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This one can be filed under both &#8220;total impractical&#8221; and &#8220;somewhat useful&#8221;.  For those who haven&#8217;t heard of this trick before, base64 encoding enables you to embed binary image data directly into your HTML or CSS source.  Therefore, instead of the browser requesting two files from the server &#8211; the CSS file and the image file &#8211; only the CSS file need be requested.  There are a number of drawbacks/pros/cons to this approach: IE doesn&#8217;t support it, file size limitations, larger CSS files, inability to separately cache CSS vs. images, etc. etc., but if you feel the need to use FamFamFam Silk icon&#8217;s in base64, look no further.</p>
<ul>
<li><a href="/examples/famfamfam/icons.css">The CSS file</a>.  It&#8217;s 2.1mb, so be careful.</li>
<li><a href="/examples/famfamfam/">Examples</a></li>
</ul>
<p>I use icons very liberally on all my projects; they&#8217;re classy and aid in usability.  I typically start with a &#8220;.icon&#8221; class that configures the correct padding/background details for the element, followed by a &#8220;.icon-*&#8221; class, which sets the background-image property to that of the desired icon.  For example, this CSS:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">.<span style="color: #993333;">icon</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background-repeat</span><span style="color: #00AA00;">:</span><span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">3px</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">3px</span> <span style="color: #933;">19px</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.icon-pdf</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">/images/icons/pdf.png</span><span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Applied to this anchor element:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;icon icon-pdf&quot;</span>&gt;</span>Download PDF<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></div></div>

<p>Produces:  &nbsp;<a class="icon pdf" href="#">Download PDF</a></p>
<p>Can you dig it?<span id="more-348"></span>  The same .icon-pdf class in base64 looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.icon-pdf</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>data<span style="color: #00AA00;">:</span>image/png<span style="color: #00AA00;">;</span>base64<span style="color: #00AA00;">,</span>iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHhSURBVDjLjZPLSxtRFIfVZRdWi0oFBf<span style="color: #00AA00;">+</span>BrhRx5dKVYKG4tLhRqlgXPmIVJQiC60JCCZYqFHQh7rrQlUK7aVUUfCBRG5RkJpNkkswrM5NEf73n6gxpHujAB/fOvefjnHM5VQCqCPa1MNoZnU/Qxqhx4woE7ZZlpXO53F0<span style="color: #00AA00;">+</span>n0c52Dl8Pt/nQkmhoJOCdUWBsvQJ2u4ODMOAwvapVAqSJHGJKIrw<span style="color: #00AA00;">+</span>/2uxAmuJgFdMDUVincSxvEBTNOEpmlIp9OIxWJckMlkoOs6AoHAg6RYYNs2kp4RqOvfuIACVFVFPB4vKYn3pFjAykDSOwVta52vqW6nlEQiwTMRBKGygIh9GEDCMwZH6EgoE<span style="color: #00AA00;">+</span>qHLMuVBdbfKwjv3yE6Ogjz/PQ/CZVDPSFRRYE4/RHy1y8wry8RGWGSqyC/nM1meX9IQpQV2JKIUH8vrEgYmeAFwuPDCHa9QehtD26HBhCZnYC8ucGzKSsIL8wgsjiH1PYPxL<span style="color: #00AA00;">+</span>vQvm5B/3sBMLyIm7GhhCe90BaWykV/Gp<span style="color: #00AA00;">+</span>VR9oqPVe9vfBTsruM1HtBKVPmFIUNusBrV3B4ev6bsbyXlPdkbr/u<span style="color: #00AA00;">+</span>StHUkxruBPY<span style="color: #00AA00;">+</span>0KY8f38oWX/byvNAdluHNLeOxDB<span style="color: #00AA00;">+</span>uyQQfPCWZ3NT69BYJWkjxjnB1o9Fv/ASQ5s<span style="color: #00AA00;">+</span>ABz8i2AAAAAElFTkSuQmCC<span style="color: #00AA00;">&#41;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>With this strategy in mind, I took each Silk icon and created a <code>.icon-*</code> class, where <code>*</code> is the name of the icon (underscores replaced with a hyphen).  Each icon has a background-image property of the base64 encoded image.</p>
<h2>The Code</h2>
<p>The code to convert a directory of icons into base64 representation is very straightforward and revolves around ColdFusion&#8217;s fileReadBinary() and toBase64():</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cffunction</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;base64CSS&quot;</span> output<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;false&quot;</span> returntype<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;void&quot;</span><span style="color: #0000FF;">&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfargument</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;imgdir&quot;</span> <span style="color: #0000FF;">required</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;true&quot;</span> <span style="color: #0000FF;">type</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;string&quot;</span> <span style="color: #0000FF;">hint</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;the directory of images to base64 convert&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfargument</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;destination&quot;</span> <span style="color: #0000FF;">required</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;true&quot;</span> <span style="color: #0000FF;">type</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;string&quot;</span> <span style="color: #0000FF;">hint</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;the path and name to dump the css file&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfargument</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;examples&quot;</span> <span style="color: #0000FF;">required</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;false&quot;</span> <span style="color: #0000FF;">type</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;string&quot;</span> <span style="color: #0000FF;">hint</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;the path and name to dump the example html&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> <span style="color: #000000; font-weight: bold;">var</span> images <span style="color: #0000FF;">=</span> <span style="color: #009900;">&quot;&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> <span style="color: #000000; font-weight: bold;">var</span> icon <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">&#123;</span><span style="color: #0000FF;">&#125;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!--- read the image directory ---&gt;</span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfdirectory</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;images&quot;</span> directory<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#arguments.imgdir#&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!--- write our CSS and example files ---&gt;</span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> filewrite<span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">expandpath</span><span style="color: #0000FF;">&#40;</span>arguments.destination<span style="color: #0000FF;">&#41;</span>, <span style="color: #009900;">&quot;&quot;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfif</span> <span style="color: #0000FF;">structkeyexists</span><span style="color: #0000FF;">&#40;</span>arguments, <span style="color: #009900;">&quot;examples&quot;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> filewrite<span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">expandpath</span><span style="color: #0000FF;">&#40;</span>arguments.examples<span style="color: #0000FF;">&#41;</span>, <span style="color: #009900;">&quot;&quot;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfif</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
	<span style="color: #808080; font-style: italic;">&lt;!--- loop through each image ---&gt;</span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfloop</span> <span style="color: #0000FF;">query</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;images&quot;</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--- read the image and convert it to base64 ---&gt;</span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> icon.<span style="color: #0000FF;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">toBase64</span><span style="color: #0000FF;">&#40;</span>fileReadBinary<span style="color: #0000FF;">&#40;</span>directory <span style="color: #0000FF;">&amp;</span><span style="color: #009900;">'/'</span><span style="color: #0000FF;">&amp;</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--- famfamfam icons separate words with an underscore.  hyphens look better in CSS ---&gt;</span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> icon.<span style="color: #0000FF;">name</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">replace</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">replace</span><span style="color: #0000FF;">&#40;</span><span style="color: #0000FF;">name</span>, <span style="color: #009900;">&quot;_&quot;</span>, <span style="color: #009900;">&quot;-&quot;</span>, <span style="color: #009900;">&quot;all&quot;</span><span style="color: #0000FF;">&#41;</span>, <span style="color: #009900;">'.png'</span>, <span style="color: #009900;">''</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--- create the actual CSS and HTML ---&gt;</span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> icon.css <span style="color: #0000FF;">=</span> <span style="color: #009900;">&quot;.icon-&quot;</span> <span style="color: #0000FF;">&amp;</span>icon.<span style="color: #0000FF;">name</span><span style="color: #0000FF;">&amp;</span> <span style="color: #009900;">&quot; { background-image:url(data:image/png;base64,&quot;</span> <span style="color: #0000FF;">&amp;</span>icon.<span style="color: #0000FF;">data</span><span style="color: #0000FF;">&amp;</span> <span style="color: #009900;">&quot;) }&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> icon.html <span style="color: #0000FF;">=</span> <span style="color: #009900;">'&lt;div&gt;&lt;span class=&quot;icon icon-'</span><span style="color: #0000FF;">&amp;</span>icon.<span style="color: #0000FF;">name</span><span style="color: #0000FF;">&amp;</span><span style="color: #009900;">'&quot;&gt;'</span> <span style="color: #0000FF;">&amp;</span> icon.<span style="color: #0000FF;">name</span> <span style="color: #0000FF;">&amp;</span> <span style="color: #009900;">'&lt;/span&gt;'</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--- append to the css file ---&gt;</span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cffile</span> <span style="color: #0000FF;">action</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;append&quot;</span> file<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#expandpath(arguments.destination)#&quot;</span> output<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#icon.css#&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!--- append to the examples file ---&gt;</span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfif</span> <span style="color: #0000FF;">structkeyexists</span><span style="color: #0000FF;">&#40;</span>arguments, <span style="color: #009900;">&quot;examples&quot;</span><span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
			<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cffile</span> <span style="color: #0000FF;">action</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;append&quot;</span> file<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#expandpath(arguments.examples)#&quot;</span> output<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;#icon.html#&quot;</span> <span style="color: #0000FF;">/&gt;</span></span>
		<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfif</span><span style="color: #0000FF;">&gt;</span></span>
	<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfset</span><span style="color: #0000FF;">&gt;</span></span><span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfloop</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cffunction</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> base64CSS<span style="color: #0000FF;">&#40;</span><span style="color: #009900;">&quot;/home/ehynds/Assets/icons/silk/&quot;</span>, <span style="color: #009900;">&quot;icons.css&quot;</span>, <span style="color: #009900;">&quot;examples.htm&quot;</span><span style="color: #0000FF;">&#41;</span> <span style="color: #0000FF;">/&gt;</span></span></pre></div></div>

<p>I started by creating a &#8220;base64CSS&#8221; function which accepts three arguments: a path to a directory of images, the CSS file to generate, and the examples HTML file to generate.  cfdirectory reads the image directory, the two CSS/example files are created, and then I loop through each image in the directory.  Each image is read as binary data and converted to base64, given a name (separated with hyphens instead of underscores), and the CSS and HTML is generated.  Finally, the CSS is appended to the CSS file and the HTML to the examples file.</p>
<p>Simple, right?  Does anyone else use some kind of icon class?  I&#8217;d like to see other approaches!</p>


<p>Related posts:<ol><li><a href='http://www.erichynds.com/coldfusion/test/' rel='bookmark' title='CFIMAGE resize after CFFILE upload'>CFIMAGE resize after CFFILE upload</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.erichynds.com/coldfusion/famfamfam-silk-icon-set-in-base64-encoding/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

