<?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>Birk</title>
	<atom:link href="http://birk-jensen.dk/feed/" rel="self" type="application/rss+xml" />
	<link>http://birk-jensen.dk</link>
	<description>dot dot dot</description>
	<lastBuildDate>Wed, 06 Jul 2011 10:56:09 +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>Cowon Emulator status</title>
		<link>http://birk-jensen.dk/2011/07/cowon-emulator-status/</link>
		<comments>http://birk-jensen.dk/2011/07/cowon-emulator-status/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 10:55:11 +0000</pubDate>
		<dc:creator>Philip Birk-Jensen</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[cowon]]></category>
		<category><![CDATA[emulator]]></category>

		<guid isPermaLink="false">http://birk-jensen.dk/?p=398</guid>
		<description><![CDATA[This post is mainly for my self, if you don&#8217;t have the basic understanding of how to make UCI&#8217;s for the Cowon this post wont make much sense. I&#8217;ve been busy at work lately, so I haven&#8217;t really gotten as far as I wanted on my Cowon J3 &#038; S9 emulator. But I&#8217;ll write a [...]]]></description>
			<content:encoded><![CDATA[<p>This post is mainly for my self, if you don&#8217;t have the basic understanding of how to make UCI&#8217;s for the Cowon this post wont make much sense.<br />
I&#8217;ve been busy at work lately, so I haven&#8217;t really gotten as far as I wanted on my Cowon J3 &#038; S9 emulator. But I&#8217;ll write a quick status update here and which problems I&#8217;ve encountered.</p>
<p>As it turns out, my understanding of how the device worked was very limited, and some of what I knew turned out to be wrong. My main fault was not understanding how the <em>launcher.swf</em> worked. It turns out that I only need to implement the ext_fscommand2 functions and not the functions starting with &#8220;gn_&#8221; (I found references to this function in some decompiled UCI&#8217;s). The &#8220;gn_&#8221; functions are in the launcher.swf which I simply load from the emulator (I had implemented about 25% of the launcher.swf in my emulator before I realized this :().</p>
<p>I&#8217;ve also figured out how I will make the file listing, or I&#8217;m down to 2 ideas. Both ideas include an AIR application that reads the device folders (which is going to be fun because I&#8217;ve never made an AIR application before).<br />
The first solution (the one I&#8217;ll probably end up using), will have the application save an XML file with the folder and file structure. This XML is then loaded by the emulator and used to mimic the file system, requiring a manual update of the filesystem every time a file is changed (but like on the actual device the files don&#8217;t change that often).<br />
<span id="more-398"></span><br />
After I get the filesystem up and running, it should be rather easy to emulate the actual playing of an MP3. But then again I thought a lot of the things that caused trouble in this project would&#8217;ve been easy.</p>
<p>Another thing I&#8217;ll give it a week or to and see if I get started up on the emulator again, if I don&#8217;t I&#8217;ll just release it in it&#8217;s current form, so you can see how it works for yourself. The current form however is a not to well documented and still in progress, set of classes with no actual UI everything I change I do code time.</p>
]]></content:encoded>
			<wfw:commentRss>http://birk-jensen.dk/2011/07/cowon-emulator-status/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cowon J3 (Cow&#8217;n&#039;Emu)</title>
		<link>http://birk-jensen.dk/2011/04/cowon-j3-cownemu/</link>
		<comments>http://birk-jensen.dk/2011/04/cowon-j3-cownemu/#comments</comments>
		<pubDate>Sat, 16 Apr 2011 14:53:08 +0000</pubDate>
		<dc:creator>Philip Birk-Jensen</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[cowon]]></category>
		<category><![CDATA[emulator]]></category>

		<guid isPermaLink="false">http://birk-jensen.dk/?p=384</guid>
		<description><![CDATA[So I&#8217;ve recently bought the Cowon J3 32gb mp3 player. It&#8217;s a small upgrade from my Cowon S9, which was the 16gb version. Both players has an excellent sound quality, the best I&#8217;ve ever had in an mp3 player (I&#8217;ve owned a couple of different ones, Rio, Creative, iPod, Samsung and something that starts with [...]]]></description>
			<content:encoded><![CDATA[<p><center><a href="http://birk-jensen.dk/2011/04/cowon-j3-cownemu/cowon_j3_4801/" rel="attachment wp-att-387"><img src="http://birk-jensen.dk/wp-content/uploads/cowon_j3_4801.jpg" alt="" title="cowon_j3_4801" width="480" height="395" class="aligncenter size-full wp-image-387 head-image" /></a></center>So I&#8217;ve recently bought the <a href="http://www.cowonglobal.com/product_wide/COWONJ3/product_page_1.php">Cowon J3</a> 32gb mp3 player. It&#8217;s a small upgrade from my Cowon S9, which was the 16gb version.</p>
<p>Both players has an excellent sound quality, the best I&#8217;ve ever had in an mp3 player (I&#8217;ve owned a couple of different ones, Rio, Creative, iPod, Samsung and something that starts with F that I can&#8217;t remember the name of anymore).<br />
My only problem with the 2 Cowons is the touch-screen user interface, which has gotten a lot better with the J3. The problem is, even though the touch UI is improved, the new button(buttons on the actual device) layout is not as good as it was with the S9 (my own opinion).</p>
<p>But to the point of it all. Both the S9 and J3 runs on a Adobe Flash UI, and Cowon have opened up for their API, so you can make your own UI with flash and Actionscript 2 (unfortunatly the UI runs on a AVM 1 in Flash Player 7, so you can&#8217;t use AS3).<br />
So I started out making a simple SWF to test out what the J3 was capable of, and how to use the Cowon API. And everything was really straight forward, to call their API you use the ext_fscommand2 function, and passed along some arguments letting the device know what you want to do.<br />
The main problem is the testing phase. You need to plugin the player, transfer your SWF to it, plug it out, turn it on and then test it. The first couple of times it wasn&#8217;t an issue, but the 5th time you do it, it gets annoying, and the 10th time I did it, I stopped. It was to much of a hassle. So I searched the net for an emulator so I could test my project right away.<br />
Now the only emulator I could find had no download link, and no one seemed to update it. So I decided to start working on my own.</p>
<p>So far I&#8217;ve implemented the ext_fscommand2 function and have it working as intended. But I haven&#8217;t made any actual functionality that simulates the device yet. One of the things I&#8217;m still having a hard time with is the Key events. I need to catch all the events from the UserSWF (the SWF you wish to load onto the device), and handle them in my own emulated environment instead of having Flash doing it for me.</p>
<p>Anyway, that was a long post just to tell you that I had started work on a Cowon S9+J3 emulator. Hopefully I&#8217;ll have a running alpha version next time I post about the subject.</p>
]]></content:encoded>
			<wfw:commentRss>http://birk-jensen.dk/2011/04/cowon-j3-cownemu/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>CSSParser release</title>
		<link>http://birk-jensen.dk/2011/04/cssparser-release/</link>
		<comments>http://birk-jensen.dk/2011/04/cssparser-release/#comments</comments>
		<pubDate>Sun, 10 Apr 2011 19:46:23 +0000</pubDate>
		<dc:creator>Philip Birk-Jensen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[css-class]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://birk-jensen.dk/?p=367</guid>
		<description><![CDATA[So&#8230; I haven&#8217;t really been doing any work at all on the CSSParser(my own CSS Tidy project for reading and compressing CSS) I started on a couple of months ago. What happened is what always happens with my own projects. After I&#8217;ve implemented the initial idea, I start to loose interest. But I&#8217;ve made it [...]]]></description>
			<content:encoded><![CDATA[<p>So&#8230; I haven&#8217;t really been doing any work at all on the CSSParser(my own <a href="http://csstidy.sourceforge.net/">CSS Tidy</a> project for reading and compressing CSS) I started on a <a href="http://birk-jensen.dk/2010/10/starting-out-my-css-minify-project/">couple of months ago</a>. What happened is what always happens with my own projects. After I&#8217;ve implemented the initial idea, I start to loose interest.<br />
But I&#8217;ve made it further with this CSSParser than I usually do, so I&#8217;ve decided to release the source code for the project. Not every function will be well documented but I&#8217;ve tried to comment as much as possible.<br />
<center><strong><a href="http://dload.birk-jensen.dk/CSSParser/CSSParser.zip">Download it here.</a></strong></center></p>
<p>I&#8217;ll do a quick explanation of how the process, just to give an idea of how it all works.</p>
<ol>
<li>The <span class="inLine">CSSParser</span> class reads a given CSS, using a single <span class="inLine">preg_match_all</span> call.</li>
<li>Now then class will start to build up an array of <span class="inLine">CSSSelector</span> classes. This class will hold all the properties and identifiers of a CSS selector from the given CSS.</li>
<li>Each property of the CSSSelector will be held in a <span class="inLine">CSSProperty</span> class or a class extended the CSSProperty like <span class="inLine">CSSBorder</span> and <span class="inLine">CSSBox</span>. This way it&#8217;s easy to add more specific output on properties, simply by extending the CSSProperty class and making your own property class.</li>
<li>Now all the data is structured in the CSSParser class. And by calling the <span class="inLine">printCSS()</span> function with a <span class="inLine">CSSOut</span> class as the argument, we can get the CSS out the way we want.</li>
</ol>
<p>When printing the CSS a very primitive templating system is used, where the template simply is PHP that has access to the list of selectors after they&#8217;ve been run through a preProcess function optimizing the data for output.<br />
An example can be the <span class="inLine">template1.php</span> file (found in the downloadable zip):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$selector</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$selector</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">identifier</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; {<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$selector</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">properties</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$property</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$property</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$settings</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #006699; font-weight: bold;">$key</span>: <span style="color: #006699; font-weight: bold;">$value</span>;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;}<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><span id="more-367"></span><br />
In the bottom of the <span class="inLine">CSSParser.php</span> file, there&#8217;s a small test which outputs a CSS file using 2 different templates. This little example shows you how you can easily use the parser:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$css</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CSSParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$css</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">readCSS</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/data/test.css&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$settings</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CSSOut<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Readability:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$settings</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">templateFile</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;data/template1.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$css</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">printCSS</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$settings</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Smallest:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$settings</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">templateFile</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;data/template2.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$css</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">printCSS</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$settings</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This project is far from done (or well tested), so even though it can read and parse the simple test.css file included in the zip, it&#8217;s not ready for an actual live deployment. It&#8217;s simply a piece of code that shows how I would&#8217;ve implemented a CSS Parser if I had the time to finish it all.</p>
<p>So if you decide to download the project, here&#8217;s a little guide with places of interest (code pieces in the project I feel is vital for this to work or had most fun writing).</p>
<ul>
<li><span class="inLine">CSSParser->readCSS($cssString)</span> This function is the heart of it all. It processes the CSS and sets up the selectors with the correct properties. I feel I&#8217;ve documented this function well, and since I don&#8217;t do regular expressions very often I&#8217;m particularly proud of the 2 patterns just at the start of this function!</li>
<li><span class="inLine">CSSSelector->getHandler($property)</span> The function that makes sure the correct CSSProperty class is used for a property. It&#8217;s actually a bit confusing, and I had to read it a couple of times again before realising what I was trying to achieve when I wrote it. It&#8217;s important to note that it&#8217;s connected with the <span class="inLine">$propertyList</span> variable at the top of the <span class="inLine">CSSSelector.php</span> file.</li>
<li><span class="inLine">CSSBorder</span> Yep the entire class. This was my first property class, then I wrote the CSSProperty class, so the CSSBorder could fit on it, and then wrote the <span class="inLine">CSSBox</span> in between these to classes. The <span class="inLine">getBorderArray()</span> function in this class, is also responsible for my <a href="http://birk-jensen.dk/2010/12/checking-for-a-value/">Checking for a value</a> speed test</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://birk-jensen.dk/2011/04/cssparser-release/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Checking for a value</title>
		<link>http://birk-jensen.dk/2010/12/checking-for-a-value/</link>
		<comments>http://birk-jensen.dk/2010/12/checking-for-a-value/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 08:39:46 +0000</pubDate>
		<dc:creator>Philip Birk-Jensen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[bench-class]]></category>
		<category><![CDATA[css-class]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://birk-jensen.dk/?p=322</guid>
		<description><![CDATA[Reasoning behind the bench I&#8217;ve been working a bit on my CSSParser (which I will rename to CSSReader og CSSHandler when I get around to it), and one thing I found out was, when reading shorthand values (like: border: 1px solid #00ffff;) you can&#8217;t assume they always come in the same order. If we take [...]]]></description>
			<content:encoded><![CDATA[<h4>Reasoning behind the bench</h4>
<p>I&#8217;ve been working a bit on my <a href="http://birk-jensen.dk/tag/css-class/">CSSParser</a> (which I will rename to CSSReader og CSSHandler when I get around to it), and one thing I found out was, when reading shorthand values (like: <span class="inLine">border: 1px solid #00ffff;</span>) you can&#8217;t assume they always come in the same order.<br />
If we take a <span class="inLine">border</span> property as an example:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.something</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">border-right</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">dashed</span> <span style="color: #cc00cc;">#00ff00</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-left</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">dashed</span> <span style="color: #933;">1px</span> <span style="color: #993333;">rgb</span><span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">border-top</span> <span style="color: #000000; font-weight: bold;">blue</span> <span style="color: #993333;">solid</span> <span style="color: #933;">1px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>This shows that the <span class="inLine">border</span> shorthand value can have a different order of subvalues, and the actual value data can change alot (notice the 3 different ways of defining a color, the <span class="inLine">rgb()</span> still cause me a bit of trouble). So to set an array (mapped with <span class="inLine">['width']</span>, <span class="inLine">['style']</span> and <span class="inLine">['color']</span>) I can&#8217;t just split the string and set it, I need to split the string and then try to guess every value. To guess the values I&#8217;ll be running through the following process:<span id="more-322"></span><br />
<a href="http://birk-jensen.dk/2010/12/checking-for-a-value/check_process/" rel="attachment wp-att-359"><img src="http://birk-jensen.dk/wp-content/uploads/check_process.png" alt="" title="check_process" width="620" height="230" class="aligncenter size-full wp-image-359 head-image" /></a><br />
I&#8217;ve tried to put the <em>checks</em> in the order I would assume most common, so I can <span class="inLine">continue</span> the loop quickly after each <em>check</em> and skip the following ones.</p>
<h4>The test</h4>
<p><center><a href="http://bench.birk-jensen.dk/runner.php?test=look_for_value">Link to test</a></center><br />
Now the thing is I need to check if one value exists in a predefined list of values (this case it&#8217;s particularly the styles, but colors and width as well has some constant names they can use to define the numeric value). I figured there&#8217;s 3 ways of doing this. Actually I started with 5 ways, but as I <a href="http://birk-jensen.dk/2010/11/benchmark-class-release-first-test/">learned previously</a> I have to keep these tests down in size for it to work properly (the 2 ways I cut will be grayed in the list below):</p>
<ol>
<li>Having an array with all the predefined values, and checking it with <span class="inLine"><a href="http://php.net/manual/en/function.in-array.php">in_array</a></span>.</li>
<li>Having all the predefined values in a string separated by spaces, then using <span class="inLine"><a href="http://php.net/manual/en/function.strpos.php">strpos</a></span> to look for our value.</li>
<li>Flipping (<span class="inLine"><a href="http://php.net/manual/en/function.array-flip.php">array_flip</a></span>) our array from earlier, and instead of <span class="inLine">in_array</span> we use <span class="inLine"><a href="http://php.net/manual/en/function.isset.php">isset</a>($array['our_value'])</span>.</li>
<li style="color: #999999">Again using <span class="inLine">strpos</span> to check, but instead of a string separated by spaces I tried a constant.</li>
<li style="color: #999999">Tried a <span class="inLine">preg_match</span> on a string of values.</li>
</ol>
<p>I&#8217;ve run the test for the same dataset, but testing for values on different positions in the set.</p>
<ul>
<li>first value, is testing for the value which is placed as the first value in the dataset.</li>
<li>mid value, is a value there&#8217;s somewhere the middle of the set.</li>
<li>last value, is last value of the set.</li>
<li>no hit, here I test for a value that doesn&#8217;t exist in the set.</li>
</ul>
<p><a href="http://birk-jensen.dk/2010/12/checking-for-a-value/values/" rel="attachment wp-att-348"><img src="http://birk-jensen.dk/wp-content/uploads/values.png" alt="" title="values" width="620" height="217" class="aligncenter size-full wp-image-348 head-image" /></a><br />
The obvious winner is the <span class="inLine">isset</span> method which is almost 5 times faster than checking for the value using the <span class="inLine">in_array</span> (which was what I would have chosen to start with). To avoid using the <span class="inLine">array_flip</span> you can always just write the array up with keys and then equal some value, like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hidden&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;dotted&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;dashed&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>It&#8217;s just a hassle to write larger sets of data that way, so using the array_flip function really helps out here, which is also the method I&#8217;ve chosen for my CSS project.</p>
]]></content:encoded>
			<wfw:commentRss>http://birk-jensen.dk/2010/12/checking-for-a-value/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Benchmark class, release (&amp; first test)</title>
		<link>http://birk-jensen.dk/2010/11/benchmark-class-release-first-test/</link>
		<comments>http://birk-jensen.dk/2010/11/benchmark-class-release-first-test/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 10:09:50 +0000</pubDate>
		<dc:creator>Philip Birk-Jensen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[bench-class]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://birk-jensen.dk/?p=290</guid>
		<description><![CDATA[So as promised I&#8217;ve cleaned up and commented a bit on my Bench class. And let me get to it, you can Download it here. Bench suite I&#8217;ve also made my first test in my new (and not to pretty, but working) bench suite. The bench suite is a site where I can gather my [...]]]></description>
			<content:encoded><![CDATA[<p>So as promised I&#8217;ve cleaned up and commented a bit on my Bench class. And let me get to it, you can<br />
<center><strong><a href="http://dload.birk-jensen.dk/BenchClass/Bench_v1.zip">Download it here.</a></strong></center></p>
<h4>Bench suite</h4>
<p>I&#8217;ve also made my first test in my new (and not to pretty, but working) <a href="http://bench.birk-jensen.dk/">bench suite</a>. The bench suite is a site where I can gather my different tests, and be able to easily access them later on for references. So far it&#8217;s a very simple setup, where I have:</p>
<ul>
<li>A list of the tests. This goes through a directory where each test has it&#8217;s own test file, and use some of the data in the file to make an informative list.</li>
<li>A runner. Which will run each test, and output the results in an easy to read table, along with the actual source code of the test.</li>
</ul>
<p>Down the road I would like to improve a bit on the interface which is lacking to say the least. I&#8217;ve also been planning on saving the test data so I can get an average result of all the tests run.</p>
<h4>Empty loops test</h4>
<p>So the very first test I&#8217;ve done is a simple <a href="http://bench.birk-jensen.dk/runner.php?test=empty_loops">empty loop</a>(it will run the test every time you, so the result wont be identical to what I&#8217;ve displayed below) test. And it also works as a quick how-to for the class it self.<br />
What I wanted to test with this bench, was the difference between the <span class="inLine">for</span>, <span class="inLine">while</span> and <span class="inLine">do..while</span> loops, and as an added bonus I also tested a <a href="http://dk.php.net/manual/en/control-structures.goto.php">goto</a> loop (more for the fun of things). I&#8217;ve also tested everything with a pre and post increment. Anyway the result of the test looks as following<span id="more-290"></span> after it&#8217;s been through google docs:<br />
<a href="http://birk-jensen.dk/2010/11/benchmark-class-release-first-test/empty_loop/" rel="attachment wp-att-311"><img src="http://birk-jensen.dk/wp-content/uploads/empty_loop.png" alt="" title="Empty loop" width="620" height="270" class="aligncenter size-full wp-image-311 head-image" /></a><br />
To reach my conclusion I&#8217;ve run the test multiple times (the above chart is just from a single run), and it seems that the <span class="inLine">do..while</span> is the fastest loop most of the time, followed by the <span class="inLine">while</span> loop. A thing that I noticed was the <span class="inLine">for</span> loop runs faster with pre incrementing(<span class="inLine">++$i</span>) instead of post(<span class="inLine">$i++</span>). But the other loops tend to be faster with a post increment, don&#8217;t ask me why it&#8217;s just what I&#8217;ve observed.<br />
Even though the <span class="inLine">do..while</span> is notably faster (almost every time) I think I&#8217;ll stick to using the <span class="inLine">for</span> loop for these kind of loops, unless I really need the speed. In case I needed the speed I would probably do a test with each loop to see what gain I actually would get in that specific case.</p>
<p>All in all a good first test, I think I&#8217;ll need to test fewer things in my next tests if possible. I could have skipped pre and post incrementing to make the chart a more readable, something I might keep in mind for my next bench.</p>
]]></content:encoded>
			<wfw:commentRss>http://birk-jensen.dk/2010/11/benchmark-class-release-first-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Benchmark class v1</title>
		<link>http://birk-jensen.dk/2010/10/benchmark-class-v1/</link>
		<comments>http://birk-jensen.dk/2010/10/benchmark-class-v1/#comments</comments>
		<pubDate>Thu, 28 Oct 2010 11:27:40 +0000</pubDate>
		<dc:creator>Philip Birk-Jensen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[bench-class]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://birk-jensen.dk/?p=257</guid>
		<description><![CDATA[So I&#8217;ve made a first version of the benchmarking class I&#8217;ve been talking about previously. While working on the CSSParser I encountered one of those micro-optimization situations, so I made a quick first draft of the Bench class (I&#8217;ll post the actual benchmark in another post). The outline of the Bench class is as the [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve made a first version of the benchmarking class I&#8217;ve been talking about previously. While working on the <a href="http://birk-jensen.dk/tag/css-class/">CSSParser</a> I encountered one of those micro-optimization situations, so I made a quick first draft of the Bench class (I&#8217;ll post the actual benchmark in another post).<br />
The outline of the Bench class is as the following:</p>
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="30%"><strong>Function</strong></td>
<td width="70%"><strong>Description</strong></td>
</tr>
<tr>
<td width="30%">start($test, $subTest = &#8220;&#8221;)</td>
<td width="70%" style="font-size: 90%">Start a test, with a test title (which is also the identifier for the test). It&#8217;s possible to supply additional sub tests to the test, this is helpful when doing the same code test but with different inputs.</td>
</tr>
<tr>
<td width="30%">end($test, $subTest = &#8220;&#8221;)</td>
<td width="70%" style="font-size: 90%">Ends the test with the test and subTest identifier.</td>
</tr>
<tr>
<td width="30%">tick($test, $subTest = &#8220;&#8221;)</td>
<td width="70%" style="font-size: 90%">Set a mid point in a test, this way we can easily make more fix points if needed.</td>
</tr>
<tr>
<td width="30%">getResult($dec = 6)</td>
<td width="70%" style="font-size: 90%">Returns all the tests in a neatly packed table, the $dec is the amount of decimals on the time measurement.</td>
</tr>
</table>
<p>Let me show a simple example where we test 2 empty for and while loops and a recursive function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> recursive<span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> recursive<span style="color: #009900;">&#40;</span><span style="color: #339933;">--</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bench<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">start</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;for loop&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;1000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;for loop&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;1000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">start</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;for loop&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;10000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">10000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;for loop&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;10000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">start</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;while&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;1000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;while&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;1000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">start</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;while&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;10000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;while&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;10000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">start</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;recursive&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;1000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
recursive<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;recursive&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;1000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">start</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;recursive&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;10000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
recursive<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;recursive&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;10000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResult</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<div class="phpOutput"><span class="outputTitle">simple bench output</span></p>
<pre>for loop 	0.000097	0.000810	0.000454	100.00%
while    	0.000074	0.000629	0.000351	77.48%
recursive	0.001844	0.016954	0.009399	2072.14%</pre>
</div>
<p>The first column is the name of the test.<br />
The following 2 columns is each sub test, so the first number is the time of our &#8220;1000&#8243; iterations and the second number is &#8220;10000&#8243; iterations.<br />
The fourth column represents the <span id="more-257"></span>average of all the sub tests (it&#8217;s not always this is informative, so there will be an option to remove this in future versions).<br />
The last column is the difference between all the tests average. It will set the first test to 100%, the rest of the tests will be calculated in relation to this number.<br />
With the output formatted this way, you can easily copy and paste it into a <a href="https://docs.google.com/">spreadsheet</a> and display it in what ever chart that fits your purpose. Just remember not to use the percentage in your data set:<br />
<a href="http://birk-jensen.dk/2010/10/benchmark-class-v1/benchchart/" rel="attachment wp-att-271"><img src="http://birk-jensen.dk/wp-content/uploads/benchchart.png" alt="An example of the data in a Google Docs spreadsheet" title="Chart" width="620" height="184" class="aligncenter size-full wp-image-271 head-image" /></a></p>
<p>I still need to clean up and comment the class some more, before I&#8217;m comfortable with putting it up online. But it&#8217;s rather small so if I get some time this weekend I&#8217;ll run through it, and it will be up next week.</p>
]]></content:encoded>
			<wfw:commentRss>http://birk-jensen.dk/2010/10/benchmark-class-v1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Starting up my CSS Minify project</title>
		<link>http://birk-jensen.dk/2010/10/starting-out-my-css-minify-project/</link>
		<comments>http://birk-jensen.dk/2010/10/starting-out-my-css-minify-project/#comments</comments>
		<pubDate>Tue, 19 Oct 2010 09:48:55 +0000</pubDate>
		<dc:creator>Philip Birk-Jensen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[css-class]]></category>
		<category><![CDATA[minify]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://birk-jensen.dk/?p=246</guid>
		<description><![CDATA[This is just a status update on my CSS Minify project, I will make more updates as I progress through the project. It&#8217;s basically a journal of my thoughts and design process, so if anything strikes you please do comment with some feedback. So I started working on a way to minify my CSS files. [...]]]></description>
			<content:encoded><![CDATA[<p><em>This is just a status update on my CSS Minify project, I will make more updates as I progress through the project. It&#8217;s basically a journal of my thoughts and design process, so if anything strikes you please do comment with some feedback.</em></p>
<p>So I started working on a way to minify my CSS files. I know (or well I found out, just after I started working on my own project) that there&#8217;s a well written and very functional <a href="http://csstidy.sourceforge.net/">CSSTidy</a> which does exactly what I want to do. I will anyhow still continue my own project, mostly for the learning process, but also because it&#8217;s been a long time since I&#8217;ve had a project of my own and I miss that.<br />
My initial thought was to parse the CSS using regular expressions, but the <a href="http://en.wikipedia.org/wiki/Lexical_analysis">lexical</a> approach CSSTidy uses caught my eye.<br />
I&#8217;ve decided to start out parsing with the regular expressions. I will eventually try to copycat the CSSTidy way, just to get a better understanding of lexical analysis, and having CSSTidy as a result sheet.</p>
<p>So far I&#8217;ve made the parser, and it seems to work on proper CSS I will need to test it up against some messy CSS.<br />
I had my CSSParser class, with a $selectors array holding all the selectors (my CSSSelector classes). The CSSSelector class had a similar $properties array, holding all the properties (CSSProperties class) for the selector.<br />
This seemed a bit overkill, the arrays were both using a key being the selector/property name and a value being the class, the class it self just being a class with a value property. So I will remove the 2(selector and property) classes, and just use a simpler array structure.<br />
I do however still like the idea of having the selector and property classes, it fits nicely into the whole CSS principal. I&#8217;ve been planing to use them when it comes to outputting the CSS again, but in that case why not just use them to start with &#8230; I&#8217;m torn.</p>
]]></content:encoded>
			<wfw:commentRss>http://birk-jensen.dk/2010/10/starting-out-my-css-minify-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XML-RPC</title>
		<link>http://birk-jensen.dk/2010/09/xml-rpc/</link>
		<comments>http://birk-jensen.dk/2010/09/xml-rpc/#comments</comments>
		<pubDate>Wed, 29 Sep 2010 08:26:31 +0000</pubDate>
		<dc:creator>Philip Birk-Jensen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[article]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[webservice]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xmlrpc]]></category>

		<guid isPermaLink="false">http://birk-jensen.dk/?p=221</guid>
		<description><![CDATA[This is simply a copy of my old XML-RPC article (2005). I&#8217;ve done a bit of content editing when converting the article to this blog format, but still most of the content is unchanged. 1. Introduction So you&#8217;ve decided to learn XML-RPC[1], or did the name just sound &#8220;freaky&#8221; and raised a little interest? If [...]]]></description>
			<content:encoded><![CDATA[<p><em>This is simply a copy of my old <a title="XML-RPC" href="http://birk-jensen.dk/articles/php/xmlrpc/xmlrpc.php?view_old">XML-RPC article (2005)</a>. I&#8217;ve done a bit of content editing when converting the article to this blog format, but still most of the content is unchanged.</em></p>
<h3>1. Introduction</h3>
<p>So you&#8217;ve decided to learn XML-RPC<sup><a href="http://www.xmlrpc.com" target="_blank">[1]</a></sup>, or did the name just sound &#8220;freaky&#8221; and raised a little interest? If it did I suggest you read on, if you already know what XML-RPC is, then just skip the next intro part</p>
<h4>1.1 What is XML-RPC</h4>
<p>XML-RPC is a way to make web services, it packs the requests and responses in XML both the client and the server can understand.<br />
For those who don&#8217;t know a web service is a function of some sort placed on the net (it could be a function to convert a string into all upper case letters, which we are going to make in the examples below). The smart thing is that, a web service can be programmed in any kind of language, and any other language can use it. Many big sites have a web service, for example Amazon has a web service that allows you to search books and information.<br />
Another smart thing about XML-RPC (and to my knowledge all other web services), is that they can be run from a web server. Not only is this smart, because you don&#8217;t need to install some high-end server dedicated to the purpose. But you don&#8217;t have to worry about firewalls.</p>
<h4>1.2 Installing XML-RPC</h4>
<p>If you&#8217;ve installed an extension before, this should be no problem at all, just install the XML-RPC extension. But if this is your first time, the following links will help a bit:<br />
Windows users: <a href="http://www.php.net/manual/en/install.windows.extensions.php">http://www.php.net/manual/en/install.windows.extensions.php</a><br />
Unix users: <a href="http://www.php.net/manual/en/install.unix.php">http://www.php.net/manual/en/install.unix.php</a><br />
OS X users: <a href="http://www.php.net/manual/en/install.macosx.php">http://www.php.net/manual/en/install.macosx.php</a><br />
For you guys(and gals) who compile PHP: <em>&#8211;with-xmlrpc</em>.<span id="more-221"></span></p>
<h3>2. The Server</h3>
<p>For obviusly reasons we start making the server, which contains the function our client will be calling later on.<br />
I&#8217;ve decided to comment the code, this way I don&#8217;t have to explain it to you after you&#8217;ve read it. And you can by remember what it all does later on:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*****
 * We start witht the functions we wan't to be web services.
 * Each of them must have 3 arguments:
 * 1st: This is the method name, which the client requests.
 * 2nd: This one is the arguments, which the client sends with the request.
 * 3rd: This data is specified by the server it self.
 *****/</span>
<span style="color: #000000; font-weight: bold;">function</span> xmlEcho<span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #339933;">,</span> <span style="color: #000088;">$appData</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'Come again: '</span><span style="color: #339933;">.</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Just for the fun of it, let's make 2 function:</span>
<span style="color: #000000; font-weight: bold;">function</span> xmlAllToUpper<span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #339933;">,</span> <span style="color: #000088;">$appData</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strToUpper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$args</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">/*****
 * Next up we'll assign the XML-RPC server handle to a variable.
 * I've chosen this &quot;sneaky&quot; name: $xmlServer * But you can think of this
 * as any other handle, for example, when you're working with a database, 
 * you have the connection handle, it's the same thing.
 *****/</span>
<span style="color: #000088;">$xmlServer</span> <span style="color: #339933;">=</span> <span style="color: #990000;">xmlrpc_server_create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">/*****
 * Now that's done it's time to tell the server which functions the
 * client can request. This is done with the xmlrpc_server_register_method
 * function (why can't they come up with some short names!). This function
 * requires 3 arguments:
 * 1st: The server handle, we just specified above.
 * 2nd: The function name, which the client calls.
 * 3rd: The function name, to run.
 *
 * This means that your function dosn't have to be named the same as the
 * function the client calls.
 *****/</span>
<span style="color: #990000;">xmlrpc_server_register_method</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$server</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'echo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'xmlEcho'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">xmlrpc_server_register_method</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$server</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'goLarge'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'xmlAllToUpper'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">/*****
 * We need to get what ever request the client send us.
 * This is done with the $HTTP_RAW_POST_DATA variable. Nothing more
 * to say about that, so I'll just give you yet another dumb ass
 * comment ... That was it.
 *****/</span>
<span style="color: #000088;">$xmlRequest</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$HTTP_RAW_POST_DATA</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">/*****
 * We have all we need now, for the server to call the function, the
 * client requested.
 * To call this function, we use this (again rediculesly long) function:
 * xmlrpc_server_call_method, which as always take 3 arguments:
 * 1st: The server handle.
 * 2nd: The client request we just got above.
 * 3rd: What ever data you (as the server) want to send, this
 *      will be passed into the $appData variable.
 *****/</span>
<span style="color: #000088;">$xmlResponse</span> <span style="color: #339933;">=</span> <span style="color: #990000;">xmlrpc_server_call_method</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xmlServer</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xmlRequest</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Prepare the header contents so it sends XML</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: text/xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xmlResponse</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">xmlrpc_server_destroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xmlServer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>All done with the server part. If you haven&#8217;t figured out exactly how the server works yet, then let me give you a little insight. In short terms, it&#8217;s just a php file, that is called by the client, through a normal URL. It then processes the contents which the client send, and return a result.</p>
<h3>3. The Client</h3>
<p>Well you made it so far, so why not make the client to test your server as well. Even though we made the server first, I must admit you&#8217;ll probably be using the client part a &#8220;wee&#8221; bit more. Of course it&#8217;s fun to mess around with the server, but chances are you&#8217;ll be using other webservices most of the time?<br />
Anyway, as you might notice, the client code contains a little socket<sup><a href="http://dk.php.net/manual/en/function.fsockopen.php" target="_blank">[2]</a></sup> programming. If you&#8217;ve never worked with sockets before don&#8217;t think to much of it, it&#8217;s not that hard, and you&#8217;ll propably pick up the on the code anyway.<br />
<em>When I wrote this article I had no idea of the <a href="http://birk-jensen.dk/2010/09/a-quick-look-at-the-curl-library/">cURL library</a>, which I would propably be using for an actual project instead of my own request function.</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*****
 * First off we'll be making a function, to send our request to the
 * server.
 * This is where the feared socket programming makes it appearence,
 * take a notice at the default value for port, some people have
 * (for reasons only god may know).
 *****/</span>
<span style="color: #000000; font-weight: bold;">function</span> sendRequest<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$request</span><span style="color: #339933;">,</span> <span style="color: #000088;">$port</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">80</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// First off we open a connection to the server.</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$sock</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fsockopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$port</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errNo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errString</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Error: '</span><span style="color: #339933;">.</span> <span style="color: #000088;">$errNo</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' - '</span><span style="color: #339933;">.</span> <span style="color: #000088;">$errString</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/*****
     * We prepare the HTTP header, in our request.
     * Notice we set the Content-Type to text/xml (just notice...).
     *****/</span>
    <span style="color: #000088;">$httpQuery</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;POST &quot;</span><span style="color: #339933;">.</span> <span style="color: #000088;">$url</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; HTTP/1.0<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$httpQuery</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;User-Agent: xmlrpc<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$httpQuery</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Host: &quot;</span><span style="color: #339933;">.</span> <span style="color: #000088;">$host</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$httpQuery</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Type: text/xml<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$httpQuery</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Content-Length: &quot;</span><span style="color: #339933;">.</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$httpQuery</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$request</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Here we send the request to the server</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sock</span><span style="color: #339933;">,</span> <span style="color: #000088;">$httpQuery</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$httpQuery</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Error while trying to send request'</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// We get the response from the server</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sock</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$xmlResponse</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">fgets</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sock</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Closing the connection</span>
    <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sock</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// We strip the response from it's HTTP header</span>
    <span style="color: #000088;">$xmlResponse</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xmlResponse</span><span style="color: #339933;">,</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xmlResponse</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/*****
     * To decode the XML into PHP, we use the (finaly a short function)
     * xmlrpc_decode function. And that should've done the trick.
     * We now have what ever the server function made in our $xmlRespnse
     * variable.
     *****/</span>
    <span style="color: #000088;">$xmlResponse</span> <span style="color: #339933;">=</span> <span style="color: #990000;">xmlrpc_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xmlResponse</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Returns the result.</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$response</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*****
 * The first thing we do, is encoding our request into an XML-RPC
 * string. This is done with the xmlrpc_encode_request function, which 
 * accepts 2 arguments:
 * 1st: Name of the function we wish to call from the server.
 * 2nd: The argument we want to parse into this function. If we
 *      wish to parse more than 1 argument, we put them into an array.
 *****/</span>
<span style="color: #000088;">$xmlRequest</span> <span style="color: #339933;">=</span> <span style="color: #990000;">xmlrpc_encode_request</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'echo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'All hail Philip the almighty... Obey!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*****
 * Now we call the sendRequest function, with our XML request. Remember
 * to change the host if you're not on a local computer or what ever you use.
 * And remember to change the path, if you've not saved the server directly in
 * the root (which I suspect you havent). If you hav it in the &quot;articles/php&quot;
 * subdirectory, you use &quot;/articles/php/xml_server.php&quot;
 *****/</span>
<span style="color: #000088;">$xmlResponse</span> <span style="color: #339933;">=</span> sendRequest<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/xmlrpc_server.php'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xmlRequest</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// And at last, we print out the response!</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xmlResponse</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<div class="phpOutput"><span class="outputTitle">The response from the server</span>string(49) &#8220;Come again: All hail Philip the almighty&#8230; Obey!&#8221;</div>
<p>Hey, now wasn&#8217;t that funny! Ya I bet it was &#8230; Anyway let&#8217;s take one last example just to try out all of our functions. This time we parse more than one argument with the request, just to get the whole picture:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$xmlRequest</span> <span style="color: #339933;">=</span> <span style="color: #990000;">xmlrpc_encode_request</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'goLarge'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1234</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'little'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1 small step'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$xmlResponse</span> <span style="color: #339933;">=</span> sendRequest<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/xmlrpc_server.php'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xmlRequest</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xmlResponse</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<div class="phpOutput"><span class="outputTitle">goLarge output</span>array(3) {<br />
  [0]=><br />
  string(4) &#8220;1234&#8243;<br />
  [1]=><br />
  string(6) &#8220;LITTLE&#8221;<br />
  [2]=><br />
  string(12) &#8220;1 SMALL STEP&#8221;<br />
}</div>
<p>Well I don&#8217;t realy know how to explain this any better, so I guess all the practical stuff ends here. </p>
<h3>4. Theory</h3>
<p>I&#8217;ve chosen to place this section at the end of the article, because it&#8217;s just a little extra on how XML-RPC works underneath. And let me just tell you, that I quickly go through this, if you want more detailed information try the XML-RPC Specification<sup><a href="http://www.xmlrpc.com/spec" target="_blank">[3]</a></sup>, which will go more in depth.</p>
<h4>4.1 Request</h4>
<p>I&#8217;ll start by pasting in our own request to the server (the echo request we made in the example above), leaving out the headers:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;iso-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;methodCall<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;methodName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>echo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/methodName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;params<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>All hail Philip the almighty... Obey!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/params<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/methodCall<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>It&#8217;s pretty straight forward, the first thing we do is state that we would like to call a function <span class="inLine">&lt;methodCall&gt;</span>.<br />
We then tell the server which function we&#8217;re calling <span class="inLine">&lt;methodName&gt;</span>, this is followed by out list of parameters.<br />
A <span class="inLine">&lt;params&gt;</span> can have any number of <span class="inLine">&lt;param&gt;</span>, which each holds a <span class="inLine">&lt;value&gt;</span>. In our case this value is a string (<span class="inLine">&lt;string&gt;</span>), but it can be any one of XML-RPCs 5 supported data types:</p>
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr style="background-color: #dddddd">
<td width="150"><strong>Tag</strong></td>
<td width="290"><strong>Type</strong></td>
<td><strong>Example</strong></td>
</tr>
<tr>
<td>&lt;i4&gt; or &lt;int&gt;</td>
<td>four-byte signed integer</td>
<td>-12</td>
</tr>
<tr style="background-color: #eeeeee">
<td>&lt;boolean&gt;</td>
<td>0(false) or 1(true)</td>
<td>1</td>
</tr>
<tr>
<td>&lt;string&gt;</td>
<td>string</td>
<td>hello world</td>
</tr>
<tr style="background-color: #eeeeee">
<td>&lt;double&gt;</td>
<td>double-precision signed floating point number</td>
<td>-12.214</td>
</tr>
<tr>
<td>&lt;dateTime.iso8601&gt;</td>
<td>date/time</td>
<td>19980717T14:08:55</td>
</tr>
<tr style="background-color: #eeeeee">
<td>&lt;base64&gt;</td>
<td>base64-encoded binary</td>
<td>eW91IGNhbid0IHJlYWQgdGhpcyE=</td>
</tr>
</table>
<p>XML-RPC also supports <em>arrays</em> and <em>structures</em>, but you&#8217;ll have to read about them in the XML specs.</p>
<h4>4.2 Response</h4>
<p>Again I&#8217;ve stripped the HTML header from the server response:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;iso-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;methodResponse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;params<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Come again: All hail Philip the almighty... Obey!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/params<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/methodResponse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Like with the request, this is rather understandable, so I wont add anything to this.<br />
Sometimes an error happens, you might have supplied to many arguments, or you might misspelled a function so the server can&#8217;t find it. When this happen the server will return a fault code. I&#8217;ve (besides stolen the idea of showing this fault code here from the XML-RPC Specs) chosen to give you an example on a fault code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;iso-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;methodResponse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fault<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;struct<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;member<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>faultString<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>server error. method not found.<span style="color: #ddbb00;">&amp;#10;&amp;#10;</span>echos<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/member<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;member<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>faultCode<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;int<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-32601<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/int<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/member<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/struct<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fault<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/methodResponse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Again, it&#8217;s easy to understand, but this shows the <span class="inLine">&lt;struct&gt;</span> and how it works.</p>
<h3>Conclusion</h3>
<p>To be honest I don&#8217;t really know what to make of all this, except that it works like a charm. If you compare the XML-RPC protocol to some of the other web service protocols you can find &#8220;out there&#8221;, you&#8217;ll find that the difference is small. Some are a little bit more advanced, some are more compact, others are exactly the same and some are useless.<br />
Finding the right web service, is all about what you like to work with, my personal favorit is XML-RPC (hence this article). So my suggestion is try some of the other big web services, and find the one you&#8217;re most comfortable with, and then stick with it!<br />
<em>Honestly I haven&#8217;t used XML-RPC since I wrote this article. And if I should make a service available, I would first search through any new alternatives, and find a solution that would fit my problem.</em></p>
<h3>a. sources</h3>
<p>[1] <a href="http://www.xmlrpc.com">http://www.xmlrpc.com</a><br />
[2] <a href="http://dk.php.net/manual/en/function.fsockopen.php">http://dk.php.net/manual/en/function.fsockopen.php</a><br />
[3] <a href="http://www.xmlrpc.com/spec">http://www.xmlrpc.com/spec</a></p>
]]></content:encoded>
			<wfw:commentRss>http://birk-jensen.dk/2010/09/xml-rpc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pic2HTML</title>
		<link>http://birk-jensen.dk/2010/09/pic2html/</link>
		<comments>http://birk-jensen.dk/2010/09/pic2html/#comments</comments>
		<pubDate>Thu, 23 Sep 2010 12:04:39 +0000</pubDate>
		<dc:creator>Philip Birk-Jensen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[article]]></category>
		<category><![CDATA[gd]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://birk-jensen.dk/?p=138</guid>
		<description><![CDATA[This is simply a copy of my old pic2html article (2005). I&#8217;ve done some minor content editing, mostly spelling but there still might be a few lines that doesn&#8217;t make any sense. 1. Introduction A long long time ago, I was a little trip by http://romanm.ch (no longer the same site), and well I must [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://birk-jensen.dk/2010/09/pic2html/beehead/" rel="attachment wp-att-194"><img src="http://birk-jensen.dk/wp-content/uploads/beehead.jpg" alt="Bee" title="Pic2HTML" width="620" height="147" class="aligncenter size-full wp-image-194 head-image" /></a><em>This is simply a copy of my old <a title="PHP Ping" href="http://birk-jensen.dk/articles/php/pic2html/pic2html.php?view_old">pic2html article (2005)</a>. I&#8217;ve done some minor content editing, mostly spelling but there still might be a few lines that doesn&#8217;t make any sense.</em></p>
<h3>1. Introduction</h3>
<p>A long long time ago, I was a little trip by <a href="http://romanm.ch" target="_blank">http://romanm.ch <em>(no longer the same site)</em></a>, and well I must admit pretty impressing, try to take a look your self, especially the movies are well made.<br />
Anyway, this gave me the idea of making an image to pure html code. My theory was that it would be simple to make a picture look good even with nothing but html (and as you&#8217;ll see later this was the truth, it&#8217;s possible to make 100% replicas), even though it&#8217;ll be size heavy.<br />
Before we get going, remember to enable the <em>GD library</em><sup><a href="http://www.php.net/manual/en/ref.image.php" target="_blank">[1]</a></sup>, for PHP. If you don&#8217;t know how, consult the PHP manual. I wont be going into details about the different GD lib functions, so you&#8217;ll need to read up on the GD functions you&#8217;re not familiar with in the manual as well.</p>
<h3>2. rgb2html</h3>
<p>First off we need a function to change the RGB color code to HTML color code (or simple from decimal to hexadecimal<sup><a href="http://en.wikipedia.org/wiki/Numeral_system" target="_blank">[2]</a></sup>). Lucky for us PHP is filled with a lot of helper functions, that sometimes turns out to be, well useful. This time it&#8217;s the great comeback for the <em>dechex</em> function, which convert a decimal number to hexadecimal. I&#8217;ve packed this down in a little function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> rgb2html<span style="color: #009900;">&#40;</span><span style="color: #000088;">$clrR</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clrG</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clrB</span><span style="color: #339933;">,</span> <span style="color: #000088;">$intReturn</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$clrR</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;0&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">dechex</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$clrR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$clrG</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;0&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">dechex</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$clrG</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$clrB</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;0&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">dechex</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$clrB</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$intReturn</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'red'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$clrR</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'green'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$clrG</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'blue'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$clrB</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$intReturn</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$clrR</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$clrG</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$clrB</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>In our code, all we need is the function which spits out the color code as a string, but I got carried away and made a little extra (return as an array). And you can like any other of my functions in any of my articles, (ab)use them like a mad man, if that&#8217;s you desire, just as long as you don&#8217;t say you&#8217;ve written then your self. <span id="more-138"></span>And just for the fun of it, lets try the code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// #ff9600</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'#'</span><span style="color: #339933;">.</span> rgb2html<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">150</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<div class="phpOutput"><span class="outputTitle">output</span>#ff9600</div>
<p>And well it seems fine.</p>
<h3>3. pic2html</h3>
<p>And now this is where all the magic happens. In this function, we&#8217;ll be reading a picture pixel by pixel, and printing it out as HTML. I&#8217;ve made a couple of considerations about the HTML before I started:<br />
I&#8217;ve used the <em>&lt;pre&lt;</em>, this way we save some valuable space, byt not having to use the evil <em>&lt;br /&gt;</em> code. And i&#8217;ve chosen to use the <em>&lt;font color=&#8221;#[COLOR]&#8220;&gt;char&lt;/font&gt;</em> instead of the (now a days) &#8220;correct&#8221; <em>&lt;span&gt;</em>, again to save some Kb. The stylesheet i&#8217;ve chosen is the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span><span style="color: #00AA00;">;</span></pre></td></tr></table></div>

<p>For the less fortunate (<em>this including myself. It took me several tries to find this obvious css syntax</em>), this sheet will give it so that every character fills up 1 single pixel.<br />
Anyway here you have it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> pic2html<span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <span style="color: #000088;">$char</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$imgX</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$imgY</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$lastIndx</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$clrRGB</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorsforindex</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lastIndx</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;pre style=&quot;font-size: 1px; line-height: 1px;&quot;&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;font color=&quot;#'</span> <span style="color: #339933;">.</span> rgb2html<span style="color: #009900;">&#40;</span><span style="color: #000088;">$clrRGB</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'red'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clrRGB</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'green'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clrRGB</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'blue'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$y</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$imgY</span><span style="color: #339933;">;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$imgX</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$clrIndx</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$lastIndx</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$clrIndx</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/font&gt;;'</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$clrRGB</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorsforindex</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clrIndx</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;font color=&quot;#'</span> <span style="color: #339933;">.</span> rgb2html<span style="color: #009900;">&#40;</span><span style="color: #000088;">$clrRGB</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'red'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
                <span style="color: #000088;">$clrRGB</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'green'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clrRGB</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'blue'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$lastIndx</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$clrIndx</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$char</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;&quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/pre&gt;&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// You don't need to . concatenate the string, but my WP-plugin brakes if I don't.</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>It shouldn&#8217;t be that hard to understand, but I&#8217;ll run through the code in briefs below:<br />
First of we find the size of the image, so we know how many times to run our loops.<br />
Next we find the very first color. This is done outside our loops, this way we can optimize our code, so if the same color appears 2 times after each other, we will keep the same <font>. This will in pictures with few colors make the size of the file dramatically less.<br />
Now we print the selected character with the right color. Running from the top left corner till the lower right when it&#8217;s done.</p>
<p>When you call the pic2html function, remember to do it with an image handle, and not just with a path:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
pic2html<span style="color: #009900;">&#40;</span><span style="color: #990000;">imagecreatefromjpeg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'images/bigbee.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<div class="phpOutput"><span class="outputTitle">output</span>&lt;pre style=&#8221;font-size: 1px; line-height: 1px;&#8221;&gt;&lt;font color=&#8221;#0c0b09&#8243;&gt;#&lt;/font&gt;&lt;fo<br />
nt color=&#8221;#0f0e0c&#8221;&gt;#&lt;/font&gt;&lt;font color=&#8221;#100f0d&#8221;&gt;##&lt;/font&gt;&lt;font color=&#8221;#0f0e0c&#8221;&gt; [..........]</div>
<h3>4. Conclusion</h3>
<p>Actually I must conclude that &#8211; for once &#8211; everything was as I&#8217;ve expected. It&#8217;s possible to make a replica of an image, but it&#8217;s heavy. Another problem with a large picture is that it doesn&#8217;t go all the way through the image, it makes it till one point, and then it just stops. i don&#8217;t know if it&#8217;s my PHP script timeout, or if it&#8217;s my system that lags the resources. But if you notice the beautiful picture below (captured by yours truly, during a massive hangover), you&#8217;ll see the script stops:<br />
<a href="http://birk-jensen.dk/2010/09/pic2html/bee/" rel="attachment wp-att-152"><img src="http://birk-jensen.dk/wp-content/uploads/bee.jpg" alt="" title="Bee" width="575" height="197" class="size-full wp-image-152" /></a></p>
<h4>4.1 Useful?</h4>
<p>So far I haven&#8217;t found any useful purpose for this. But if anyone can come up with something please do throw a comment and let me know.</p>
<h4>- Added -</h4>
<p><em>While converting the article to my new blog, I&#8217;ve read through it quickly, changing a few obvious spelling mistakes and altered a couple of lines (and removed a few sentences). But what strike me now is, that we can actually decrease the size of the end HTML, if we used &lt;span&gt; with a class. The class names would be c[ITERATED_NUMBER] and define a color, this way we could reuse a small class name many times instead of replicating a full color=&#8221;#000000&#8243; (15 chars) class=&#8221;c1&#8243; (10 chars, allowing up to class=&#8221;c999999&#8243; different colors, that could be reused.<br />
Anyway I still really can&#8217;t seem to figure out where this could be used, if you got an idea please do tell&#8230;</em></p>
<h4>a. sources</h4>
<p>[1] <a href="http://www.php.net/manual/en/ref.image.php">http://www.php.net/manual/en/ref.image.php</a><br />
[2] <a href="http://en.wikipedia.org/wiki/Numeral_system">http://en.wikipedia.org/wiki/Numeral_system</a></p>
]]></content:encoded>
			<wfw:commentRss>http://birk-jensen.dk/2010/09/pic2html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Benchmark class, what do I need?</title>
		<link>http://birk-jensen.dk/2010/09/benchmark-class-what-do-i-need/</link>
		<comments>http://birk-jensen.dk/2010/09/benchmark-class-what-do-i-need/#comments</comments>
		<pubDate>Mon, 20 Sep 2010 09:35:58 +0000</pubDate>
		<dc:creator>Philip Birk-Jensen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[bench-class]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://birk-jensen.dk/?p=114</guid>
		<description><![CDATA[Although micro-optimizing don&#8217;t give the biggest performance improvement, I often find my self wondering if using function X() instead of Y() is faster, and if my own function Z() is better. Most of the time I end up writing a small tiny benchmark test on the two or three functions, which then gives me a [...]]]></description>
			<content:encoded><![CDATA[<p>Although micro-optimizing don&#8217;t give the biggest performance improvement, I often find my self wondering if using function <span class="inLine">X()</span> instead of <span class="inLine">Y()</span> is faster, and if my own function <span class="inLine">Z()</span> is better. Most of the time I end up writing a <del datetime="2010-09-08T12:42:16+00:00">small</del> tiny benchmark test on the two or three functions, which then gives me a quick overview of the functions speed.<br />
So I&#8217;ve decided to make a small and simple to use micro-benchmarking class.</p>
<h3>Goal</h3>
<p>Basically this class is all about being easy to use, so you don&#8217;t have to spend a long time setting up a test.</p>
<ul>
<li><strong>Easy to set up.</strong> The class should be used for those small tests, where you&#8217;re in doubt of which piece of code is fastest. And if one is to test a piece of code, for a tiny performance increase (maybe more out of curiosity than an actual application improvement), it should only take a couple of minutes to set the test up. If it takes 15 minutes to create a test every time, you wont take the time to write the test if you&#8217;re in the middle of something else.</li>
<li><strong>Readable output.</strong> For the same reasons it should be easy to set up, the output needs to be readable when the test is done, without any post-test-processing. It would be good if the output could be chosen, so you could either get the easy to read, or a more in-depth detailed result (maybe formatted for post-processing in another environment).</li>
</ul>
<h3>Nice to have</h3>
<p>Again with the intentions of making this easier to work with, it would be great to have a feature, where you would just write the piece of code you wanted to test, and then have the class handle the rest.<span id="more-114"></span> I&#8217;ve been brainstorming a few ways to do this.</p>
<ul>
<li><strong>Functions.</strong> Assign a function, the class can call when doing the test. Perhaps it should be possible to assign a set of functions, so you had an init, bench and close function. This would make the testing process easy, write the functions, and then get the result, and even though calling a function will have an impact it should be the same for every test case.</li>
<li><strong><a href="http://dk.php.net/manual/en/function.eval.php">Eval</a> the code.</strong> This crossed my mind, but I think it&#8217;s generally bad practice to use the eval function if you can avoid it. And the code written has to be a string, which will mess up the syntax highlighting. And I don&#8217;t really know the overhead of using eval.</li>
<li><strong>External files.</strong> I could make the class read an external file, which is setup in a way the class would understand. Although this would just be separating the tests in different files, it could have some advantages when doing larger tests. And that is the problem with this solution, I don&#8217;t want to do larger tests with this class.</li>
</ul>
<p>Another thing that would be nice to have, would be an external interface. Mainly a tiny app that could read the output from the class (XML perhaps). Have the class automatically send the output to the app, which then stores it and gives you a quick overview of previous tests as well.<br />
But these are all just nice to have, so far I&#8217;ll aim for the goal when I get some free time.</p>
]]></content:encoded>
			<wfw:commentRss>http://birk-jensen.dk/2010/09/benchmark-class-what-do-i-need/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

