<?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>Brett Terpstrabase64 page  - Brett Terpstra</title>
	<atom:link href="http://brettterpstra.com/tag/base64/feed/" rel="self" type="application/rss+xml" />
	<link>http://brettterpstra.com</link>
	<description>Elegant solutions to complex problems.</description>
	<lastBuildDate>Tue, 22 May 2012 02:49:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>TextMate drag command for Base64 encoding images</title>
		<link>http://brettterpstra.com/textmate-drag-command-for-base64-encoding-images/</link>
		<comments>http://brettterpstra.com/textmate-drag-command-for-base64-encoding-images/#comments</comments>
		<pubDate>Sat, 19 Jun 2010 14:00:29 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[base64]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[commands]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[drag command]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[textmate]]></category>

		<guid isPermaLink="false">http://brettterpstra.com/?p=659</guid>
		<description><![CDATA[<p>Just a quick hit on this one… when hacking away at the styles of things one probably shouldn’t be hacking away at, embedding images right in the CSS is a handy trick. It’s done by Base64 encoding the image, removing line breaks from the resulting string, and using it to set the background property for the CSS rule. It looks&#8230;</p><p>Originally posted on <a href="http://brettterpstra.com" title="BrettTerpstra.com">BrettTerpstra.com</a> at <a href="http://brettterpstra.com/textmate-drag-command-for-base64-encoding-images/">TextMate drag command for Base64 encoding images</a></p>]]></description>
			<content:encoded><![CDATA[<p>Just a quick hit on this one… when hacking away at the styles of things one probably shouldn’t be hacking away at, embedding images right in the CSS is a handy trick. It’s done by <a href="http://en.wikipedia.org/wiki/Base64">Base64</a> encoding the image, removing line breaks from the resulting string, and using it to set the background property for the CSS rule.</p>

<p>It looks something like this (truncated):</p>

<p><code>background:url(data:image/png;base64,iVBORw0KG...AAAAASUVORK5CYII=);</code></p>

<p>The image/png changes depending on the filetype that’s encoded, becoming image/jpg or image/gif, etc. Anyway, I got tired of shelling out to encode the images, so here’s a very simple command I set up in TextMate which allows me to drag any JPEG, PNG or GIF file into a CSS rule and get the background property with encoded image all at once. Don’t try it with <em>big</em> images, but for the kind of thing you’d actually put into a CSS file, it works great.</p>

<p>The command itself is this (it’s a one-liner):</p>

<div markdown=0><pre><code>
# Drag command set to handle: jpg,png,gif
# scoped to: text.html.basic source.css.embedded.html, source.css meta.scope.property-list.css

openssl base64 -in "$TM_DROPPED_FILE" | awk -v ext="${TM_DROPPED_FILE#*.}" '{ str1=str1 $0 }END{ print "background:url(data:image/"ext";base64,"str1");" }'
</code></pre></div>

<p>It uses the <code>openssl</code> command that’s on a Mac by default. It encodes the file and outputs it to STDOUT, which is passed to awk. The extension of the dropped file is grabbed via Bash parameter substitution (<code>${TM_DROPPED_FILE#*.}</code>) and passed to awk where it’s subbed in and all of the line breaks are removed. Presto.</p>

<p>Here’s the actual TextMate command if you want to try it out and aren’t sure how to create a new one. Assuming you’re running TextMate (kind of a prerequisite for a TextMate command), just unzip and double click the file.</p>

<div class="download_desc"><p class="download-icon"><a href="http://brettterpstra.com/downloads/InsertBase64image.tmDragCommand.zip?9d7bd4" title="Download Base64 Image Encoder TextMate Drag Command (398)"><img src="http://cdn2.brettterpstra.com/wp-content/uploads/downloads/thumbnails/2010/06/TextMateIcon.png?9d7bd4" alt="download image for Base64 Image Encoder TextMate Drag Command" width="64" /></a><br /><a href="http://brettterpstra.com/downloads/InsertBase64image.tmDragCommand.zip?9d7bd4" title="Download Base64 Image Encoder TextMate Drag Command (398)" class="download-button">Download</a></p><p class="desc"><a href="http://brettterpstra.com/downloads/InsertBase64image.tmDragCommand.zip?9d7bd4" title="Download Base64 Image Encoder TextMate Drag Command (398)">Base64 Image Encoder TextMate Drag Command</a> — Encodes a dropped image and inserts a CSS background property with the result in TextMate. Works in embedded CSS and in CSS files. <a href="http://brettterpstra.com/2010/06/19/textmate-drag-command-for-base64-encoding-images/">More Info</a></p></div>

<hr style="clear:both;visibility:hidden" />
<p>Related posts:<ol>
<li><a href='http://brettterpstra.com/a-textmate-command-for-fast-abbr-tags/' rel='bookmark' title='A TextMate Command for fast abbr tags'>A TextMate Command for fast abbr tags</a></li>
<li><a href='http://brettterpstra.com/textmate-as-writeroom-for-free/' rel='bookmark' title='TextMate as WriteRoom, for free'>TextMate as WriteRoom, for free</a></li>
<li><a href='http://brettterpstra.com/placeholder-image-textmate-snippet/' rel='bookmark' title='Placeholder image TextMate Snippet'>Placeholder image TextMate Snippet</a></li>
</ol></p><p>Originally posted on <a href="http://brettterpstra.com" title="BrettTerpstra.com">BrettTerpstra.com</a> at <a href="http://brettterpstra.com/textmate-drag-command-for-base64-encoding-images/">TextMate drag command for Base64 encoding images</a></p>]]></content:encoded>
			<wfw:commentRss>http://brettterpstra.com/textmate-drag-command-for-base64-encoding-images/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic (Feed is rejected)
Page Caching using disk: enhanced
Database Caching 9/26 queries in 0.045 seconds using xcache
Object Caching 838/847 objects using xcache
Content Delivery Network via cdn2.brettterpstra.com

Served from: brettterpstra.com @ 2012-05-23 02:16:13 -->
