<?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>Emanuele Feronato &#187; Php</title>
	<atom:link href="http://www.emanueleferonato.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emanueleferonato.com</link>
	<description>italian geek and PROgrammer</description>
	<lastBuildDate>Thu, 02 Sep 2010 09:32:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Tic Tac Toe Artificial Intelligence</title>
		<link>http://www.emanueleferonato.com/2010/08/26/tic-tac-toe-artificial-intelligence/</link>
		<comments>http://www.emanueleferonato.com/2010/08/26/tic-tac-toe-artificial-intelligence/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 09:29:51 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3260</guid>
		<description><![CDATA[During these days I played a bit with Artificial Intelligence for a project I am working on (big news on the horizon) and I&#8217;m coming with a brief introduction to the simplest game you can use to test artificial intelligence. Tic Tac Toe. It&#8217;s a pencil-and-paper game for two players, O and X, who take [...]]]></description>
			<content:encoded><![CDATA[<p>During these days I played a bit with Artificial Intelligence for a project I am working on (big news on the horizon) and I&#8217;m coming with a brief introduction to the simplest game you can use to test artificial intelligence. Tic Tac Toe.</p>
<p>It&#8217;s a pencil-and-paper game for two players, O and X, who take turns marking the spaces in a 3×3 grid, usually X  going first. The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal row wins the game. (source: <a href="http://en.wikipedia.org/wiki/Tic-tac-toe" target = "_blank">Wikipedia</a>).</p>
<p>But most of all, it&#8217;s a good playground to apply artificial intelligence for four reasons:</p>
<p>It&#8217;s a <strong>perfect information game</strong>: all players know all moves that have taken place.</p>
<p>It&#8217;s a <strong>sequential game</strong>: when a player moves, the other player waits. And have the waiting player see the moves of the playing one.</p>
<p>It&#8217;s a <strong>zero sum game</strong>: if one player wins, the ohter loses. No cooperation. </p>
<p>It&#8217;s a <strong>non-random game</strong>: there isn&#8217;t any randomizing element such as dices, dealing of cards, and so on. Non-random games are pure strategy.</p>
<p><strong>How many board configurations?</strong></p>
<p>Now, we need to know how many boards configurations can exist in a Tic Tac Toe game. We&#8217;ll do it using php. We&#8217;ll code an empty board like a string with nine points (<code>.........</code>). Every point represents an empty cell. Then, with a recursive function, we&#8217;ll generate all possible board combinations, assuming the first player puts an <code>X</code> and the second player uses the <code>O</code>.</p>
<p>Here it is the script:<span id="more-3260"></span></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>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> place_move<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #339933;">,</span><span style="color: #000088;">$player</span><span style="color: #339933;">,</span><span style="color: #000088;">$turn</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$boards</span><span style="color: #339933;">,</span><span style="color: #000088;">$symbols</span><span style="color: #339933;">;</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: #cc66cc;">9</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#125;</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;">$app</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$board</span><span style="color: #339933;">;</span>
               <span style="color: #000088;">$app</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">=</span><span style="color: #000088;">$symbols</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$player</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
               <span style="color: #000088;">$boards</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">substr_count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
               place_move<span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #000088;">$player</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$symbols</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;XO&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$boards</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
place_move<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.........&quot;</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;">$end</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$boards</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$empty_spots</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Combinations with &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">-</span><span style="color: #000088;">$empty_spots</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; symbol(s): <span style="color: #006699; font-weight: bold;">$values</span>&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Elapsed time: &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$end</span><span style="color: #339933;">-</span><span style="color: #000088;">$start</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>And the result is:</p>
<p>Combinations with <strong>1</strong> symbol(s): <strong>9</strong> &#8211; easy to see, in an empty board you can place an <code>X</code> in any of the nine spots</p>
<p>Combinations with <strong>2</strong> symbol(s): <strong>72</strong> &#8211; for each of the 9 different combinations with one symbol, you can place a <code>O</code> in any of the remaining eight spots. 9*8 =72.</p>
<p>Combinations with <strong>3</strong> symbol(s): <strong>504</strong> &#8211; for each of the 72 previous combinations, you can place an X in any of the remaining seven spots. 72*7 = 504.</p>
<p>Combinations with <strong>4</strong> symbol(s): <strong>3024</strong> &#8211; 504*6</p>
<p>Combinations with <strong>5</strong> symbol(s): <strong>15120</strong> &#8211; 3024*5</p>
<p>Combinations with <strong>6</strong> symbol(s): <strong>60480</strong> &#8211; 15120*4 </p>
<p>Combinations with <strong>7</strong> symbol(s): <strong>181440</strong> &#8211; 60480*3</p>
<p>Combinations with <strong>8</strong> symbol(s): <strong>362880</strong> &#8211; 181440*2</p>
<p>Combinations with <strong>9</strong> symbol(s): <strong>362880</strong> &#8211; 362880*1. Same number as the combinations with 8 symbols, because when there are 8 symbols in game, you can only place an <code>O</code> in just one spot.</p>
<p>Elapsed time: <strong>6.0569689273834</strong> &#8211; six seconds to calculate <strong>986409</strong> different combinations. So the total possible board configurations are <strong>986410</strong>, including the empty board.</p>
<p>According to these results, we can say the number of possible combinations in a board with n symbols is <strong>9!/(9-n)!</strong>.</p>
<p><strong>How many REAL board configurations?</strong></p>
<p>Obviously there&#8217;s something wrong with this script because it does not consider when a player wins. If a player wins, the game is over and there are no more moves. So a lot of board configurations are impossible to reach.</p>
<p>So let&#8217;s modify a bit the script to check for victories and the number of necessary moves to win:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> place_move<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #339933;">,</span><span style="color: #000088;">$player</span><span style="color: #339933;">,</span><span style="color: #000088;">$turn</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$boards</span><span style="color: #339933;">,</span><span style="color: #000088;">$wins</span><span style="color: #339933;">,</span><span style="color: #000088;">$symbols</span><span style="color: #339933;">;</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: #cc66cc;">9</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#125;</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;">$app</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$board</span><span style="color: #339933;">;</span>
               <span style="color: #000088;">$app</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">=</span><span style="color: #000088;">$symbols</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$player</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
               <span style="color: #000088;">$boards</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">substr_count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
               <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>winning_board<span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    place_move<span style="color: #009900;">&#40;</span><span style="color: #000088;">$app</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #000088;">$player</span><span style="color: #339933;">,</span><span style="color: #000088;">$turn</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #009900;">&#125;</span>
               <span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$wins</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$turn</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
               <span style="color: #009900;">&#125;</span>
          <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> winning_board<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</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: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>or
		<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>or
		<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>or
		<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>or
		<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>or
		<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>or
		<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>or
		<span style="color: #009900;">&#40;</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;.&quot;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span> and <span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">==</span><span style="color: #000088;">$board</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</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>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$symbols</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;XO&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$boards</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$wins</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
place_move<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.........&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$end</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$boards</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$empty_spots</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Combinations with &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">-</span><span style="color: #000088;">$empty_spots</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; symbol(s): <span style="color: #006699; font-weight: bold;">$values</span>&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<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;">5</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">9</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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Wins in &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$x</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; moves: <span style="color: #006699; font-weight: bold;">$wins</span>[<span style="color: #006699; font-weight: bold;">$x</span>]&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Elapsed time: &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$end</span><span style="color: #339933;">-</span><span style="color: #000088;">$start</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>And the result is:</p>
<p>Combinations with <strong>1</strong> symbol(s): <strong>9</strong></p>
<p>Combinations with <strong>2</strong> symbol(s): <strong>72</strong></p>
<p>Combinations with <strong>3</strong> symbol(s): <strong>504</strong></p>
<p>Combinations with <strong>4</strong> symbol(s): <strong>3024</strong></p>
<p>Combinations with <strong>5</strong> symbol(s): <strong>15120</strong></p>
<p>Combinations with <strong>6</strong> symbol(s): <strong>54720</strong></p>
<p>Combinations with <strong>7</strong> symbol(s): <strong>148176</strong></p>
<p>Combinations with <strong>8</strong> symbol(s): <strong>200448</strong></p>
<p>Combinations with <strong>9</strong> symbol(s): <strong>127872</strong></p>
<p>Wins in <strong>5</strong> moves: <strong>1440</strong></p>
<p>Wins in <strong>6</strong> moves: <strong>5328</strong></p>
<p>Wins in <strong>7</strong> moves: <strong>47952</strong></p>
<p>Wins in <strong>8</strong> moves: <strong>72576</strong></p>
<p>Wins in <strong>9</strong> moves: <strong>81792</strong></p>
<p>Elapsed time: <strong>4.7160170078278</strong> &#8211; Less than five seconds to generate all possible Tic Tac Toe combinations.</p>
<p>This means <strong><code>X</code> will win</strong> 1440+47952+81792 = <strong>131184 times</strong> while <strong><code>O</code> will win</strong> 5328+72576 = <strong>77904 times</strong>. The first player has more chances to win.</p>
<p>How can we determine the drawn games? They are represented by the number of possible boards with nine symbols (127872) minus the number of wins at the 9th move (81792) = <strong>46080 draws</strong>.</p>
<p>According to these results, next time we&#8217;ll introduce an algorithm to make the computer play Tic Tac Toe.</p>
<p>Meanwhile enjoy these results.</p>
<p>PS: anyone willing to translate this into AS3 or other languages?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/08/26/tic-tac-toe-artificial-intelligence/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The fastest way to find the distance between two points</title>
		<link>http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/</link>
		<comments>http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 22:16:44 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[PROgramming]]></category>
		<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3187</guid>
		<description><![CDATA[While I was reading Claytus Hood Tower Defense case study I was impressed by this: To estimate the distances and so check the range, I use Manhattan distance formula, less accurate but faster than Euclidian distance Named after the grid layout of most Manhattan streets, it&#8217;s a form of geometry in which the usual metric [...]]]></description>
			<content:encoded><![CDATA[<p>While I was reading <a href="http://www.emanueleferonato.com/2010/07/26/claytus-hood-tower-defense-case-study/">Claytus Hood Tower Defense</a> case study I was impressed by this:</p>
<blockquote><p>
To estimate the distances and so check the range, I use Manhattan distance formula, less accurate but faster than Euclidian distance</p></blockquote>
<p>Named after the grid layout of most Manhattan streets, it&#8217;s a form of geometry in which the usual metric of Euclidean geometry is replaced by a new metric in which the distance between two points is<strong> the sum of the absolute differences of their coordinates</strong> (<a href="http://en.wikipedia.org/wiki/Taxicab_geometry" target = "_blank">for more information refer to Wikipedia&#8217;s Taxicab geometry page</a>).</p>
<p>Now the question is: is Manhattan distance really faster than Euclidian distance?</p>
<p>For the following test I used PHP, but feel free to make it in any other language and I&#8217;ll be glad to publish your results.<span id="more-3187"></span></p>
<p>First, I set two variables, named <code>$x_dist</code> and <code>$y_dist</code>, to <code>10</code> and <code>20</code> respectively, then to <code>-10</code> and <code>-20</code></p>
<p>These variables should represent the <code>x</code> and <code>y</code> distance between two points.</p>
<p>I generated a script to calculate <code>a million times</code> the distance with Euclidean geometry this way:</p>
<p><code>$distance = sqrt($x_dist*$x_dist+$y_dist*$y_dist);</code></p>
<p>The server took an average <strong>566.56 milliseconds</strong> to give me the response.</p>
<p>I thought most of the time is spent to calculate the square root, so a smart programmer could remove the square root and eventually compare the result with a given number multiplied by itself&#8230; so if the distance has to b less than 30, I will check if it&#8217;s less than 30*30=900.</p>
<p>This way to determine the distance:</p>
<p><code>$distance = $x_dist*$x_dist+$y_dist*$y_dist;</code></p>
<p>took an average time of <strong>204.43 milliseconds</strong> to generate a million results.</p>
<p>Then it&#8217;s time to use Manhattan distance:</p>
<p><code>$distance = abs($x_dist)+abs($y_dist);</code></p>
<p>This method took <strong>694.83 milliseconds</strong>&#8230; the slowest so far&#8230; so I tried to avoid using <code>abs</code> this way:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x_dist</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$x_dist</span> <span style="color: #339933;">*=-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y_dist</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$y_dist</span> <span style="color: #339933;">*=-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$distance</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x_dist</span><span style="color: #339933;">+</span><span style="color: #000088;">$y_dist</span><span style="color: #339933;">;</span></pre></div></div>

<p>and I got <strong>243.11 milliseconds</strong>. Trying with positive or negative starting distance values did not change response times in a valuable manner.</p>
<p>So I would say: &#8220;<strong>Myth Busted</strong>&#8220;.</p>
<p>Do you agree?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/07/29/the-fastest-way-to-find-the-distance-between-two-points/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>PHP face detection class</title>
		<link>http://www.emanueleferonato.com/2010/07/06/php-face-detection-class/</link>
		<comments>http://www.emanueleferonato.com/2010/07/06/php-face-detection-class/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 15:04:30 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[Users contributions]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3095</guid>
		<description><![CDATA[Face detection is a computer technology that determines the locations and sizes of human faces in arbitrary (digital) images. It detects facial features and ignores anything else, such as buildings, trees and bodies. (source: Wikipedia) There is so much to say about face detection and all its algorithms&#8230; I am planning a step by step [...]]]></description>
			<content:encoded><![CDATA[<p>Face detection is a computer technology that determines the locations and sizes of human faces in arbitrary (digital) images.</p>
<p>It detects facial features and ignores anything else, such as buildings, trees and bodies.</p>
<p>(source: <a href="http://en.wikipedia.org/wiki/Face_detection" target = "_blank">Wikipedia</a>)</p>
<p>There is so much to say about face detection and all its algorithms&#8230; I am planning a step by step tutorial about every branch of object detection in digital images, but first I want to publish <strong>Maurice Svay</strong>&#8216;s PHP class.</p>
<p>Maurice, in his blog <a href="http://svay.com/" target = "_blank">svay.com</a>, explains he was looking for a face detection script for PHP, but wasn&#8217;t able to find one working without <a href="http://opencv.willowgarage.com/wiki/" target = "_blank">OpenCV</a> (Open Source Computer Vision), an opensource lib that was originally developed by Intel.</p>
<p>OpenCV seems to perform well but you need to be able to install it on your server.</p>
<p>So he coded his own pure PHP solution, that does not require any library to be installed on the server.</p>
<p>And, obviously, that can be easily ported into any language. His class itself has been translated from a javascript code that actually is no longer available online</p>
<p>So this is the class:<span id="more-3095"></span></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: #666666; font-style: italic;">// </span>
<span style="color: #666666; font-style: italic;">// This program is free software; you can redistribute it and/or</span>
<span style="color: #666666; font-style: italic;">// modify it under the terms of the GNU General Public License</span>
<span style="color: #666666; font-style: italic;">// as published by the Free Software Foundation; either version 2</span>
<span style="color: #666666; font-style: italic;">// of the License, or (at your option) any later version.</span>
<span style="color: #666666; font-style: italic;">// </span>
<span style="color: #666666; font-style: italic;">// This program is distributed in the hope that it will be useful,</span>
<span style="color: #666666; font-style: italic;">// but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span style="color: #666666; font-style: italic;">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span>
<span style="color: #666666; font-style: italic;">// GNU General Public License for more details.</span>
<span style="color: #666666; font-style: italic;">// </span>
<span style="color: #666666; font-style: italic;">// You should have received a copy of the GNU General Public License</span>
<span style="color: #666666; font-style: italic;">// along with this program; if not, write to the Free Software</span>
<span style="color: #666666; font-style: italic;">// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.     </span>
<span style="color: #666666; font-style: italic;">// </span>
<span style="color: #666666; font-style: italic;">// @Author Karthik Tharavaad </span>
<span style="color: #666666; font-style: italic;">//         karthik_tharavaad@yahoo.com</span>
<span style="color: #666666; font-style: italic;">// @Contributor Maurice Svay</span>
<span style="color: #666666; font-style: italic;">//              maurice@svay.Com</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Face_Detector <span style="color: #009900;">&#123;</span>
&nbsp;
    protected <span style="color: #000088;">$detection_data</span><span style="color: #339933;">;</span>
    protected <span style="color: #000088;">$canvas</span><span style="color: #339933;">;</span>
    protected <span style="color: #000088;">$face</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$reduced_canvas</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$detection_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'detection.dat'</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: #990000;">is_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$detection_file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">detection_data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$detection_file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't load detection data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">//$this-&gt;detection_data = json_decode(file_get_contents('data.js'));</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> face_detect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</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: #339933;">!</span><span style="color: #990000;">is_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Can not load <span style="color: #006699; font-weight: bold;">$file</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">canvas</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatefromjpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$im_width</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">canvas</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$im_height</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">canvas</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Resample before detection?</span>
        <span style="color: #000088;">$ratio</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$diff_width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">320</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$im_width</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$diff_height</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">240</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$im_height</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$diff_width</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$diff_height</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$ratio</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$im_width</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">320</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$ratio</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$im_height</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">240</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ratio</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reduced_canvas</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im_width</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$ratio</span><span style="color: #339933;">,</span> <span style="color: #000088;">$im_height</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$ratio</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #990000;">imagecopyresampled</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reduced_canvas</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">canvas</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: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$im_width</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$ratio</span><span style="color: #339933;">,</span> <span style="color: #000088;">$im_height</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$ratio</span><span style="color: #339933;">,</span> <span style="color: #000088;">$im_width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$im_height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$stats</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_img_stats</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reduced_canvas</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">do_detect_greedy_big_to_small</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stats</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ii'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stats</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ii2'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stats</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'width'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stats</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'height'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'x'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$ratio</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'y'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$ratio</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$ratio</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$stats</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_img_stats</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">canvas</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">do_detect_greedy_big_to_small</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stats</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ii'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stats</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ii2'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stats</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'width'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stats</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'height'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</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: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> toJpeg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$color</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">canvas</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</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: #666666; font-style: italic;">//red</span>
        <span style="color: #990000;">imagerectangle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">canvas</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'x'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'y'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'x'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'y'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: image/jpeg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">imagejpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">canvas</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> toJson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;{'x':&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'x'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;, 'y':&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'y'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;, 'w':&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;}&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFace<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    protected <span style="color: #000000; font-weight: bold;">function</span> get_img_stats<span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$image_width</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesx</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$image_height</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagesy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     
        <span style="color: #000088;">$iis</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">compute_ii</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #339933;">,</span> <span style="color: #000088;">$image_width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$image_height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'width'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$image_width</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'height'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$image_height</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'ii'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$iis</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ii'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'ii2'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$iis</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ii2'</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;
    protected <span style="color: #000000; font-weight: bold;">function</span> compute_ii<span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #339933;">,</span> <span style="color: #000088;">$image_width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$image_height</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$ii_w</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$image_width</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$ii_h</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$image_height</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$ii</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$ii2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>      
&nbsp;
        <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: #000088;">$ii_w</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;">$ii</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: #cc66cc;">0</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$ii2</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: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>                        
&nbsp;
        <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;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$ii_w</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;">$ii</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">*</span><span style="color: #000088;">$ii_w</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>       
            <span style="color: #000088;">$ii2</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">*</span><span style="color: #000088;">$ii_w</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> 
            <span style="color: #000088;">$rowsum</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$rowsum2</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$ii_h</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$rgb</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ImageColorAt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #339933;">,</span> <span style="color: #000088;">$j</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$red</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rgb</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xFF</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$green</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rgb</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xFF</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$blue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rgb</span> <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xFF</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$grey</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color:#800080;">0.2989</span><span style="color: #339933;">*</span><span style="color: #000088;">$red</span> <span style="color: #339933;">+</span> <span style="color:#800080;">0.587</span><span style="color: #339933;">*</span><span style="color: #000088;">$green</span> <span style="color: #339933;">+</span> <span style="color:#800080;">0.114</span><span style="color: #339933;">*</span><span style="color: #000088;">$blue</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// this is what matlab uses</span>
                <span style="color: #000088;">$rowsum</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$grey</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$rowsum2</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$grey</span><span style="color: #339933;">*</span><span style="color: #000088;">$grey</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000088;">$ii_above</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$ii_w</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$j</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$ii_this</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$i</span><span style="color: #339933;">*</span><span style="color: #000088;">$ii_w</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$j</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000088;">$ii</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ii_this</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ii</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ii_above</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$rowsum</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$ii2</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ii_this</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ii2</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ii_above</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$rowsum2</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ii'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$ii</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ii2'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$ii2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    protected <span style="color: #000000; font-weight: bold;">function</span> do_detect_greedy_big_to_small<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ii</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ii2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$s_w</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$width</span><span style="color: #339933;">/</span><span style="color:#800080;">20.0</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$s_h</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$height</span><span style="color: #339933;">/</span><span style="color:#800080;">20.0</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$start_scale</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$s_h</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$s_w</span> ? <span style="color: #000088;">$s_h</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$s_w</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$scale_update</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">/</span> <span style="color:#800080;">1.2</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$scale</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$start_scale</span><span style="color: #339933;">;</span> <span style="color: #000088;">$scale</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$scale</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$scale_update</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$w</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #339933;">*</span><span style="color: #000088;">$scale</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$endx</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$width</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$w</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$endy</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$height</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$w</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$step</span> <span style="color: #339933;">=</span> <span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$scale</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$inv_area</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$w</span><span style="color: #339933;">*</span><span style="color: #000088;">$w</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;">$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;">$endy</span> <span style="color: #339933;">;</span> <span style="color: #000088;">$y</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$step</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;">$endx</span> <span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$step</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$passed</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">detect_on_sub_image</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #339933;">,</span> <span style="color: #000088;">$scale</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ii</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ii2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$w</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$inv_area</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;">$passed</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'y'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$y</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$w</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end x</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end y</span>
        <span style="color: #009900;">&#125;</span>  <span style="color: #666666; font-style: italic;">// end scale</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    protected <span style="color: #000000; font-weight: bold;">function</span> detect_on_sub_image<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #339933;">,</span> <span style="color: #000088;">$scale</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ii</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ii2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$w</span><span style="color: #339933;">,</span> <span style="color: #000088;">$iiw</span><span style="color: #339933;">,</span> <span style="color: #000088;">$inv_area</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$mean</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ii</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #339933;">+</span><span style="color: #000088;">$w</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$iiw</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$w</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$ii</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$y</span><span style="color: #339933;">*</span><span style="color: #000088;">$iiw</span><span style="color: #339933;">+</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$ii</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #339933;">+</span><span style="color: #000088;">$w</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$iiw</span><span style="color: #339933;">+</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$ii</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$y</span><span style="color: #339933;">*</span><span style="color: #000088;">$iiw</span><span style="color: #339933;">+</span><span style="color: #000088;">$x</span><span style="color: #339933;">+</span><span style="color: #000088;">$w</span><span style="color: #009900;">&#93;</span>  <span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$inv_area</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$vnorm</span> <span style="color: #339933;">=</span>  <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ii2</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #339933;">+</span><span style="color: #000088;">$w</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$iiw</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$w</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$ii2</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$y</span><span style="color: #339933;">*</span><span style="color: #000088;">$iiw</span><span style="color: #339933;">+</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$ii2</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #339933;">+</span><span style="color: #000088;">$w</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$iiw</span><span style="color: #339933;">+</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$ii2</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$y</span><span style="color: #339933;">*</span><span style="color: #000088;">$iiw</span><span style="color: #339933;">+</span><span style="color: #000088;">$x</span><span style="color: #339933;">+</span><span style="color: #000088;">$w</span><span style="color: #009900;">&#93;</span>  <span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$inv_area</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$mean</span><span style="color: #339933;">*</span><span style="color: #000088;">$mean</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
        <span style="color: #000088;">$vnorm</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vnorm</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span> ? <span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$vnorm</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$passed</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_stage</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i_stage</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">detection_data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i_stage</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$stage</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">detection_data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i_stage</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  
            <span style="color: #000088;">$trees</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$stage</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  
&nbsp;
            <span style="color: #000088;">$stage_thresh</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$stage</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$stage_sum</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i_tree</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i_tree</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$trees</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i_tree</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$tree</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$trees</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i_tree</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$current_node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tree</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: #000088;">$tree_sum</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;">$current_node</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$vals</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$current_node</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: #000088;">$node_thresh</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vals</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: #000088;">$leftval</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vals</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                    <span style="color: #000088;">$rightval</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vals</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                    <span style="color: #000088;">$leftidx</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vals</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                    <span style="color: #000088;">$rightidx</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vals</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                    <span style="color: #000088;">$rects</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$current_node</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #000088;">$rect_sum</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$i_rect</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i_rect</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rects</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i_rect</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$s</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$scale</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$rect</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rects</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i_rect</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$rx</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rect</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: #000088;">$s</span><span style="color: #339933;">+</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$ry</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rect</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #000088;">$s</span><span style="color: #339933;">+</span><span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$rw</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rect</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #000088;">$s</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>  
                        <span style="color: #000088;">$rh</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rect</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span><span style="color: #000088;">$s</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$wt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rect</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #000088;">$r_sum</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ii</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ry</span><span style="color: #339933;">+</span><span style="color: #000088;">$rh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$iiw</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$rx</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$rw</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$ii</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ry</span><span style="color: #339933;">*</span><span style="color: #000088;">$iiw</span><span style="color: #339933;">+</span><span style="color: #000088;">$rx</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$ii</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ry</span><span style="color: #339933;">+</span><span style="color: #000088;">$rh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$iiw</span><span style="color: #339933;">+</span><span style="color: #000088;">$rx</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$ii</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$ry</span><span style="color: #339933;">*</span><span style="color: #000088;">$iiw</span><span style="color: #339933;">+</span><span style="color: #000088;">$rx</span><span style="color: #339933;">+</span><span style="color: #000088;">$rw</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$wt</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$rect_sum</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$r_sum</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span> 
&nbsp;
                    <span style="color: #000088;">$rect_sum</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$inv_area</span><span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #000088;">$current_node</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$rect_sum</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$node_thresh</span><span style="color: #339933;">*</span><span style="color: #000088;">$vnorm</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;">$rightidx</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span> 
                            <span style="color: #000088;">$tree_sum</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rightval</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">else</span>
                            <span style="color: #000088;">$current_node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tree</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$rightidx</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$leftidx</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span>
                            <span style="color: #000088;">$tree_sum</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$leftval</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">else</span>
                            <span style="color: #000088;">$current_node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tree</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$leftidx</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span> 
                <span style="color: #000088;">$stage_sum</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$tree_sum</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> 
            <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$stage_sum</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$stage_thresh</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</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>
        <span style="color: #009900;">&#125;</span> 
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Using it is very easy, just write</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$detector</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Face_Detector<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'detection.dat'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$detector</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face_detect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'your_file.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$detector</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toJpeg</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>once you unpacked and included <a href="/wp-content/uploads/2010/07/detection.zip">detection.dat</a> and the image in the same path as your class.</p>
<p>Do you want to see a result?</p>
<p><img src="/wp-content/uploads/2010/07/fc.jpg" /></p>
<p>This was generated by the script, just changing the frame color from red to green, to make it more visible.</p>
<p>The class does not work that well on every photo, but it&#8217;s a good start for a journey into face detection</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/07/06/php-face-detection-class/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Create a contact page in WordPress 3.0</title>
		<link>http://www.emanueleferonato.com/2010/07/01/create-a-contact-page-in-wordpress-3-0/</link>
		<comments>http://www.emanueleferonato.com/2010/07/01/create-a-contact-page-in-wordpress-3-0/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 08:40:27 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3076</guid>
		<description><![CDATA[We know there are hundreds of plugins to let you host a contact form in WordPress, but I am going to show you how to create your own one. We are about to create a custom contact page that will work under WP 3.0, and more precisely with Twenty Ten theme. Obviously, with some minor [...]]]></description>
			<content:encoded><![CDATA[<p>We know there are hundreds of plugins to let you host a contact form in WordPress, but I am going to show you how to create your own one.</p>
<p>We are about to create a custom contact page that will work under WP 3.0, and more precisely with Twenty Ten theme. Obviously, with some minor changes you will be able to do the same in your theme.</p>
<p><strong>The script</strong></p>
<p>Let&#8217;s start: in your theme folder, create a new file called <code>contact.php</code> (or whatever other name with <code>php</code> extension) and write this 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
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
</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;">/*
Template Name: Contact me
*/</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>sent<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>your_name<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$error</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Please enter your name&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">filter_var</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>your_email<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>FILTER_VALIDATE_EMAIL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$error</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Please enter a valid email address&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>                        
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>your_message<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$error</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;p&gt;Please enter a message&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mail</span><span style="color: #009900;">&#40;</span>get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;admin_email&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>your_name<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; sent you a message from &quot;</span><span style="color: #339933;">.</span>get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;blogname&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>your_message<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;From: &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>your_name<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &lt;&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>your_email<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&gt;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>Reply-To:&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>your_email<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> get_header<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>
&lt;div id=&quot;container&quot;&gt;
	&lt;div id=&quot;content&quot; role=&quot;main&quot;&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<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>
		&lt;div id=&quot;post-<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<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>&quot; <span style="color: #000000; font-weight: bold;">&lt;?php</span> post_class<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>&gt;	
			&lt;h1 class=&quot;entry-title&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<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>&lt;/h1&gt;
			&lt;div class=&quot;entry-content&quot;&gt;
			<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
				&lt;p&gt;&lt;strong&gt;Message succesfully sent. I'll reply as soon as I can&lt;/strong&gt;&lt;/p&gt;
			<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
				&lt;p&gt;&lt;strong&gt;Your messange hasn't been sent&lt;/strong&gt;&lt;p&gt;
				<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$error</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
			<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> the_content<span style="color: #009900;">&#40;</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>
				&lt;form action=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_permalink<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>&quot; id=&quot;contact_me&quot; method=&quot;post&quot;&gt;
					&lt;input type=&quot;hidden&quot; name=&quot;sent&quot; id=&quot;sent&quot; value=&quot;1&quot; /&gt;
					&lt;ul class=&quot;contactform&quot;&gt;
						&lt;li&gt;
							&lt;label for=&quot;your_name&quot;&gt;Name&lt;/label&gt;
							&lt;input type=&quot;text&quot; name=&quot;your_name&quot; id=&quot;your_name&quot; value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>your_name<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;
						&lt;/li&gt;
						&lt;li&gt;
							&lt;label for=&quot;your_email&quot;&gt;Email&lt;/label&gt;
							&lt;input type=&quot;text&quot; name=&quot;your_email&quot; id=&quot;your_email&quot; value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>your_email<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;
						&lt;/li&gt;
						&lt;li&gt;
							&lt;label for=&quot;your_message&quot;&gt;Message:&lt;/label&gt;
							&lt;textarea name=&quot;your_message&quot; id=&quot;your_message&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>your_message<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/textarea&gt;
						&lt;/li&gt;
						&lt;li&gt;
							&lt;input type=&quot;submit&quot; name = &quot;send&quot; value = &quot;Send email&quot; /&gt;
						&lt;/li&gt;
					&lt;/ul&gt;
				&lt;/form&gt;
				<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
			&lt;/div&gt;&lt;!-- .entry-content --&gt;
		&lt;/div&gt;&lt;!-- #post-## --&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;/div&gt;&lt;!-- #content --&gt;
&lt;/div&gt;&lt;!-- #container --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> get_sidebar<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>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> get_footer<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>

<p>Now let&#8217;s see what we&#8217;ve done:<span id="more-3076"></span></p>
<p><strong>Lines 2-4</strong>: This is the way we assign a template name to the page. In this case, the name is <code>Contact me</code></p>
<p>We&#8217;ll see later what <strong>lines 5-19</strong> do, and let&#8217;s jump to&#8230;</p>
<p><strong>Lines 21-27</strong>: this is the same content you can find in <code>page.php</code> file into Twenty Ten theme folder, and if you&#8217;re using a different theme it should be the same content you can find in your theme <code>page.php</code> file until the beginning of the content of the page (typically <code>the_content()</code>).</p>
<p>In my case, I just replaced</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</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;">if</span> <span style="color: #009900;">&#40;</span> is_front_page<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;h2 class=&quot;entry-title&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<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>&lt;/h2&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>	
	&lt;h1 class=&quot;entry-title&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<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>&lt;/h1&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>with</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;h1 class=&quot;entry-title&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<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>&lt;/h1&gt;</pre></div></div>

<p>because I know a contact page can&#8217;t be the front page.</p>
<p><strong>Lines 28-33</strong>: Here come to play two variables, called <code>$error</code> and <code>$email</code>, you&#8217;ll see later how to set their values, meanwhile you have to know <code>$error</code> is an empty string or a string with an error (something like &#8220;You email address is not valid&#8221;) and <code>$email</code> is <code>true</code> if the email has been successfully sent.</p>
<p>The code proceeds this way:</p>
<p>* If the email has been sent, show a &#8220;Thank you message&#8221; and nothing else</p>
<p>* If the email has not been sent and there is an error, show the error and the form</p>
<p>* If the email has not been sent and there isn&#8217;t an error, show the page content (something like &#8220;Hello!! This is my fancy contact page!! Drop me a mail!!) and the form</p>
<p><strong>Lines 34-53</strong>: The form itself, the easiest possible, it&#8217;s up to you to add some more fields, if needed, and some styling.</p>
<p><strong>Lines 55-61</strong>: The end of the page, like in the normal Twenty Ten page.</p>
<p>Now it&#8217;s time to see the php that handles emails and errors</p>
<p><strong>Line 5</strong>: If &#8220;Send email&#8221; button (<strong>line 50</strong>) has been pressed&#8230;</p>
<p><strong>Line 6</strong>: set <code>$error</code> variable to empty string</p>
<p><strong>Lines 7-9</strong>: creating an error message if the user did not enter his name</p>
<p><strong>Lines 10-12</strong>: same as before, looking for a valid email address</p>
<p><strong>Lines 13-15</strong>: same as before, looking for a message</p>
<p><strong>Lines 16-18</strong>: if I did not find any error, I send the email with a basic formatting and sanitizing.</p>
<p><strong>The setup</strong></p>
<p>Once you created this file, create a new page this way:</p>
<p><img src="/wp-content/uploads/2010/07/pagetemplate.png" /></p>
<p>Look how I assign the page template according to the file recently created.</p>
<p>And this is what you&#8217;ll get:</p>
<p><img src="/wp-content/uploads/2010/07/result.png" /></p>
<p>Your own custom contact page. It&#8217;s just a basic one, but if I receive good feedback, I can improve it with some jQuery magic&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/07/01/create-a-contact-page-in-wordpress-3-0/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>How to list all WordPress posts with the same custom field value</title>
		<link>http://www.emanueleferonato.com/2010/05/29/how-to-list-all-wordpress-posts-with-the-same-custom-field-value/</link>
		<comments>http://www.emanueleferonato.com/2010/05/29/how-to-list-all-wordpress-posts-with-the-same-custom-field-value/#comments</comments>
		<pubDate>Fri, 28 May 2010 22:16:02 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2972</guid>
		<description><![CDATA[When I celebrated my four years of blogging I also introduced a big problem this blog is facing: there is so much content it&#8217;s not easy to find what you are looking for. I received some interesting suggestions, and one of them is to link all posts of the same series. This is what a [...]]]></description>
			<content:encoded><![CDATA[<p>When I celebrated my <a href="http://www.emanueleferonato.com/2010/05/26/four-years-of-blogging/">four years of blogging</a> I also introduced a big problem this blog is facing: there is so much content it&#8217;s not easy to find what you are looking for.</p>
<p>I received some interesting suggestions, and one of them is to link all posts of the same series. This is what a reader said: &#8220;In the first post, put a link of all the steps (requires manual editing)&#8221;.</p>
<p>I found a way to put an index on each post of a series using custom fields, and I am sure you&#8217;ll find it interesting. Let&#8217;s take <a href="http://www.emanueleferonato.com/2010/05/10/create-a-flash-game-like-rebuild-chile/">Create a Flash game like Rebuild Chile</a> series as example.</p>
<p>I want, in every post of the series, to show the full list of posts of the series. Without a lot of manual editing, of course.</p>
<p>So, that&#8217;s what I did: first, for every post of the series, I added a custom field called <code>series</code> with <code>Create a Flash game like Rebuild Chile</code> value. The idea is to add a <code>series</code> custom field to every post in every series with a proper value.</p>
<p>If you don&#8217;t know how to add a custom field, here it is a little help:</p>
<p><img src="/wp-content/uploads/2010/05/series.jpg" /></p>
<p>At the end of the Post page admin, you&#8217;ll find the form to add new custom fields. That&#8217;s how I added <code>series</code> custom field. Now, a few lines of code to rule them all&#8230; this is what you have to include somewhere inside the so-called <a href="http://codex.wordpress.org/The_Loop" target="_blank">WordPress Loop</a>:<span id="more-2972"></span></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="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$series</span> <span style="color: #339933;">=</span> get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'series'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</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;">$series</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;div id = &quot;series&quot;&gt;
		&lt;h2&gt;Read all &lt;em&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$series</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/em&gt; series&lt;/h2&gt;
		&lt;ul&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$chapters</span> <span style="color: #339933;">=</span> get_posts<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'numberposts=-1&amp;meta_key=series&amp;orderby=date&amp;order=ASC&amp;meta_value='</span><span style="color: #339933;">.</span><span style="color: #000088;">$series</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chapters</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$chapter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
			&lt;li&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$chapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;a href =&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> get_permalink<span style="color: #009900;">&#40;</span><span style="color: #000088;">$chapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$chapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_title</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/a&gt;&lt;/li&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		&lt;/ul&gt;
	&lt;/div&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Line 1</strong>: Assigning to a variable called <code>$series</code> the value of the <code>series</code> custom field value, if any. Then, if there is a value&#8230;</p>
<p><strong>Line 5</strong>: Creating a <code>$chapters</code> array and populating it with every post (numberpost=-1) whose <code>series</code> custom field (meta_key=series) has <code>$series</code> value (meta_value=$series) ordering them by date (orderby=date) in ascending way (order=ASC). Then scanning the entire array, saving each item into <code>$chapter</code><br />
variable.</p>
<p><strong>Line 6</strong>: Adding the post title, along with a link if it&#8217;s not the current post</p>
<p>The remaining lines are just HTML to let me style the content.</p>
<p>You can see the result in the <a href="http://www.emanueleferonato.com/2010/05/10/create-a-flash-game-like-rebuild-chile/">Create a Flash game like Rebuild Chile</a> series&#8230; I have to find the right style but I think I&#8217;m on the right way.</p>
<p>Do you like this way to use custom fields?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/05/29/how-to-list-all-wordpress-posts-with-the-same-custom-field-value/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Integrate Mochi games in your custom arcade website</title>
		<link>http://www.emanueleferonato.com/2010/05/21/integrate-mochi-games-in-your-custom-arcade-website/</link>
		<comments>http://www.emanueleferonato.com/2010/05/21/integrate-mochi-games-in-your-custom-arcade-website/#comments</comments>
		<pubDate>Fri, 21 May 2010 08:40:04 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Monetize]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Users contributions]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2928</guid>
		<description><![CDATA[Do you want to create your custom arcade website without using popular arcade scripts or WordPress plugins? Maybe with a Mochi integration? That&#8217;s what Gabriel Bianconi did, and he wants to share his script with us. It&#8217;s a simple, interesting script dedicated to all developers looking for a quick way to integrate Mochi games into [...]]]></description>
			<content:encoded><![CDATA[<p>Do you want to create your custom arcade website without using popular arcade scripts or WordPress plugins? Maybe with a Mochi integration?</p>
<p>That&#8217;s what <a href="http://www.gabrielbianconi.com/" target = "_blank"><strong>Gabriel Bianconi</strong></a> did, and he wants to share his script with us.</p>
<p>It&#8217;s a simple, interesting script dedicated to all developers looking for a quick way to integrate Mochi games into their custom arcade sites.</p>
<p>&laquo; Hello. My name is Gabriel Bianconi. I’m a 16 years old Brazilian student and I’m interested in game and web development.<br />
 I’ve recently launched my first arcade website, <a href="http://www.plugb.com/" target = "_blank"><strong>PlugB.com</strong></a>.  I decided to create my own script for two reasons: to improve my PHP skills and to have a more customizable website.</p>
<p>One of the features I integrated to my admin was the <a href="https://www.mochimedia.com/r/972ae333a3c92a2a" target = "_blank">MochiAds</a> ‘Post Game to my Site’ button. Since I add games manually (I don’t want to import all Mochi games), this saves me a lot of time.</p>
<p>When I tried to search about this feature, I realized that there is no tutorial about this online (at least, I couldn’t find one).  I searched a little more and found a few scripts about this.<span id="more-2928"></span></p>
<p>They were integrated to some arcade scripts, so I couldn’t just copy and paste them. But it was a start. </p>
<p>The script that I most used was from <a href="http://timarcher.com/blog/" target = "_blank">Timothy E. Archer</a>.</p>
<p>After a few tests, I’ve created a working script:</p>
<p>Firstly, you’ll need a <a href="https://www.mochimedia.com/r/972ae333a3c92a2a" target = "_blank">MochiAds</a> publisher account.</p>
<p>After registering and adding your website, go to the <a href="https://www.mochimedia.com/pub/settings" target = "_blank">Settings</a> page.</p>
<p>In the bottom of the page, below ‘Auto Post Settings’, set the ‘Auto Post Method’ to ‘Custom built script’ and the ‘Auto Post URL’ to the URL of your script.</p>
<p>Now the PHP part:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$game_tag</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'game_tag'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Sample tag to fake a mochi request... Remove this line in final version...</span>
<span style="color: #000088;">$game_tag</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'cd698a06f727e887'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$game_tag</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No gametag.'</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;">// This can be found in the settings page.</span>
<span style="color: #000088;">$mochi_pub_id</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'xxxxxxxxxxxxxxxxxxxxx'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$game_feed_url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.mochiads.com/feeds/games/<span style="color: #006699; font-weight: bold;">$mochi_pub_id</span>/<span style="color: #006699; font-weight: bold;">$game_tag</span>/?format=json&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Retrieves and parses the JSON data of the game.	</span>
<span style="color: #000088;">$json_feed</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$game_feed_url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$json_items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$json_feed</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$game</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$json_items</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'games'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Now you can access all variables about the game with $game['variable_name'];
 * For example: $game['name'] or $game['swf_url']
 * The full list of the variables can be found here: https://www.mochimedia.com/support/pub_docs#feed_3
 * I did not add how to save in a database because it changes from script to script.
 */</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">// Sample: (can be removed in final version)</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$game</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' - '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$game</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'swf_url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* You should get: Sokobones - http://games.mochiads.com/c/g/sokobones_v1/Sokobones-MochiAds_Secure.swf
 * if you used the sample tag.
 * Remove that line in the final version.
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Remember to remove the sample game tag in the final version</strong>.</p>
<p>Thank you for reading the tutorial. I hope this helps you. Please let me know what you think about this and visit my <a href="http://www.plugb.com/" target = "_blank">arcade website</a>. &raquo;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/05/21/integrate-mochi-games-in-your-custom-arcade-website/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Loading WP posts with Ajax and jQuery &#8211; a real world example</title>
		<link>http://www.emanueleferonato.com/2010/04/06/loading-wordpress-posts-with-ajax-and-jquery-a-real-world-example/</link>
		<comments>http://www.emanueleferonato.com/2010/04/06/loading-wordpress-posts-with-ajax-and-jquery-a-real-world-example/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 21:28:26 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2677</guid>
		<description><![CDATA[If you are looking for a real world example made with Loading WordPress posts with Ajax and jQuery or you haven&#8217;t played LineBall yet, or you played it, you loved it and you want to know who made the music, this is the right blog post. I am finishing and polishing LineBall&#8217;s official site built [...]]]></description>
			<content:encoded><![CDATA[<p>If you are looking for a real world example made with <a href="http://www.emanueleferonato.com/2010/04/01/loading-wordpress-posts-with-ajax-and-jquery/">Loading WordPress posts with Ajax and jQuery</a> or you haven&#8217;t played <a href="http://www.emanueleferonato.com/2010/03/29/lineball-flash-game-sponsored-and-released/">LineBall</a> yet, or you played it, you loved it and you want to know who made the music, this is the right blog post.</p>
<p>I am finishing and polishing <a href="http://www.lineball-game.com/" target = "_blank">LineBall&#8217;s official site</a> built with WordPress and Ajax.</p>
<p>I plan to finish it before this week ends, then release the theme next week. So if you have suggestions, feedback or want more information, this is your turn.</p>
<p>Help me to make the ultimate WordPress Flash game theme.</p>
<p>Some features already developed:</p>
<p>* Highly customizable by setup page &#8211; no need to edit the code</p>
<p>* Ajax powered to browse it without reloading the header (the game itself)</p>
<p>* Minimal theme, a very few lines of code</p>
<p>Let me know what do you think about it. And don&#8217;t pay that much attention to contents&#8230; I just wrote a couple of lines just to make some tests</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/04/06/loading-wordpress-posts-with-ajax-and-jquery-a-real-world-example/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Loading WordPress posts with Ajax and jQuery</title>
		<link>http://www.emanueleferonato.com/2010/04/01/loading-wordpress-posts-with-ajax-and-jquery/</link>
		<comments>http://www.emanueleferonato.com/2010/04/01/loading-wordpress-posts-with-ajax-and-jquery/#comments</comments>
		<pubDate>Thu, 01 Apr 2010 21:52:41 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2650</guid>
		<description><![CDATA[After seeing how to include jQuery Ajax calls in your WordPress blog, it&#8217;s time to load posts on the fly, without reloading the page. As for the previous example I am using the standard Kubrick theme&#8230; without any plugin installed. Look how I load the posts under the header by clicking on their titles&#8230; even [...]]]></description>
			<content:encoded><![CDATA[<p>After seeing <a href="http://www.emanueleferonato.com/2010/03/30/including-jquery-ajax-calls-in-your-wordpress-blog/">how to include jQuery Ajax calls in your WordPress blog</a>, it&#8217;s time to load posts on the fly, without reloading the page.</p>
<p>As for the previous example I am using the standard Kubrick theme&#8230; without any plugin installed.</p>
<p>Look how I load the posts under the header by clicking on their titles&#8230; even the ones with &#8220;more&#8221; tag appear complete without reloading the page:</p>
<p><object width="520" height="417"><param name="movie" value="http://www.youtube.com/v/_q7tKTn_z9s&#038;hl=it_IT&#038;fs=1&#038;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/_q7tKTn_z9s&#038;hl=it_IT&#038;fs=1&#038;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="520" height="417"></embed></object></p>
<p>Let&#8217;s start with <code>header.php</code> modifications</p>
<p>This is the script I added under</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> wp_head<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></div></div>

<p>That is the JQuery part<span id="more-2650"></span></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="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$.<span style="color: #660066;">ajaxSetup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>cache<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;h2 a&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> post_id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;rel&quot;</span><span style="color: #009900;">&#41;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#your_post_here&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;loading...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#your_post_here&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://&lt;?php echo $_SERVER[HTTP_HOST]; ?&gt;/triqui-ajax/&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span>post_id<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>I already explained the first 4 lines in the <a href="http://www.emanueleferonato.com/2010/03/30/including-jquery-ajax-calls-in-your-wordpress-blog/">previous tutorial</a>.</p>
<p><strong>Line 5</strong>: Waiting for the user to click on an hypertext over an <code>h2</code> heading. I am doing it because in Kubrick post titles are rendered this way:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;h2&gt;&lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot;....</pre></div></div>

<p>This should change from theme to theme, but this works on Kubrick</p>
<p><strong>Line 6</strong>: Retrieving the <code>rel</code> attribute of the link and saving it in a variable called <code>post_id</code>. You&#8217;ll see later in this tutorial how this attribute will contain the unique ID of the post we want to show.</p>
<p><strong>Line 7</strong>: In an element with <code>your_post_here</code> id I am writing &#8220;loading&#8230;&#8221; because I&#8217;m starting to load the post. You&#8217;ll see later in this tutorial where to place the element</p>
<p><strong>Line 8</strong>: Now thw ajax magic&#8230; in the <code>your_post_here</code> element this time I load the output of a page of the blog called <code>triqui-ajax</code>. You&#8217;ll se later in this tutorial how to create it. This will work if the permalinks of your blog aren&#8217;t the default ones. Go to <code>Settings</code> -> <code>Permalinks</code> and select <code>Day and name</code></p>
<p><img src="/wp-content/uploads/2010/04/wp_ajax_2.jpg" alt="" /></p>
<p>I am also passing in <code>POST</code> a variable called <code>id</code> with the content of <code>post_id</code> variable (the post unique id)</p>
<p><strong>Line 9</strong>: I added this line to prevent the browser to jump to the link&#8230; remember? I want to load the post in the same page.</p>
<p>Now it&#8217;s time to make some changes to <code>index.php</code></p>
<p>This is how I modified 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</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: #009933; font-style: italic;">/**
 * @package WordPress
 * @subpackage Default_Theme
 */</span>
&nbsp;
get_header<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>
&nbsp;
	&lt;div id=&quot;content&quot; class=&quot;narrowcolumn&quot; role=&quot;main&quot;&gt;
	&lt;div id = &quot;your_post_here&quot;&gt;&lt;/div&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</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>
&nbsp;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<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>
&nbsp;
			&lt;div <span style="color: #000000; font-weight: bold;">&lt;?php</span> post_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> id=&quot;post-<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<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>&quot;&gt;
				&lt;h2&gt;&lt;a href=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; rel=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<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>&quot; title=&quot;Permanent Link to <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title_attribute<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>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<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>&lt;/a&gt;&lt;/h2&gt;
				&lt;small&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'F jS, Y'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;!-- by <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_author<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> --&gt;&lt;/small&gt;
&nbsp;
				&lt;div class=&quot;entry&quot;&gt; 
					<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_content<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Read the rest of this entry &amp;raquo;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
				&lt;/div&gt;
&nbsp;
				&lt;p class=&quot;postmetadata&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_tags<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tags: '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">', '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> Posted in <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_category<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">', '</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> | <span style="color: #000000; font-weight: bold;">&lt;?php</span> edit_post_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Edit'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' | '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>  <span style="color: #000000; font-weight: bold;">&lt;?php</span> comments_popup_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No Comments &amp;#187;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1 Comment &amp;#187;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'% Comments &amp;#187;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
			&lt;/div&gt;
&nbsp;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
		&lt;div class=&quot;navigation&quot;&gt;
			&lt;div class=&quot;alignleft&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> next_posts_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&amp;laquo; Older Entries'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/div&gt;
			&lt;div class=&quot;alignright&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> previous_posts_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Newer Entries &amp;raquo;'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/div&gt;
		&lt;/div&gt;
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
		&lt;h2 class=&quot;center&quot;&gt;Not Found&lt;/h2&gt;
		&lt;p class=&quot;center&quot;&gt;Sorry, but you are looking for something that isn't here.&lt;/p&gt;
		<span style="color: #000000; font-weight: bold;">&lt;?php</span> get_search_form<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>
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
	&lt;/div&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> get_sidebar<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>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> get_footer<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>

<p>To tell the truth, I only added a line and changed a bit another one:</p>
<p><strong>Line 10</strong>: This is the div that will contain the post, the one I change with jQuery (remember? <strong>lines 7</strong> and <strong>8</strong> in the jQuery script)</p>
<p><strong>Line 16</strong>: Here I changed the <code>rel</code> attribute to store the unique id of the post I want to load. I used it at <strong>line 6</strong> in the jQuery script</p>
<p>And the template files do not need any other change.</p>
<p>Now it&#8217;s time to create a new page template with some code that will load the selected post.</p>
<p>In your template directory create a new php file, no matter its name&#8230; you can call it <code>example.php</code> and write this:</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: #666666; font-style: italic;">/*
Template Name: Triqui Ajax Post
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> get_post<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> setup_postdata<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;div <span style="color: #000000; font-weight: bold;">&lt;?php</span> post_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> id=&quot;post-<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<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>&quot;&gt;
		&lt;h2&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_title<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>&lt;/h2&gt;
		&lt;small&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'F jS, Y'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> &lt;!-- by <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_author<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> --&gt;&lt;/small&gt;
&nbsp;
		&lt;div class=&quot;entry&quot;&gt; 
			<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_content<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Read the rest of this entry &amp;raquo;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		&lt;/div&gt;
&nbsp;
		&lt;p class=&quot;postmetadata&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_tags<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Tags: '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">', '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> Posted in <span style="color: #000000; font-weight: bold;">&lt;?php</span> the_category<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">', '</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> | <span style="color: #000000; font-weight: bold;">&lt;?php</span> edit_post_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Edit'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' | '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>  <span style="color: #000000; font-weight: bold;">&lt;?php</span> comments_popup_link<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No Comments &amp;#187;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1 Comment &amp;#187;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'% Comments &amp;#187;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
	&lt;/div&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>As you can see it&#8217;s almost the same code you can find in <code>index.php</code> at <strong>lines 15-24</strong>&#8230; because I just want to load a post in the same way the index file does. I just removed the anchor tag in the title, because I don&#8217;t want it to be clickable. And obviously I read the <code>id</code> value passed by the jQuery script at <strong>line 8</strong></p>
<p>The only interesting lines are <strong>lines 2-4</strong>&#8230; yes, the comment&#8230; because this way I am giving the template page the name <code>Triqui Ajax Post</code>.</p>
<p>Now in your admin area create a new page, call it <code>Triqui Ajax</code> (do you remember the permalink url at <strong>line 8</strong> in the jQuery script&#8230;) and select <code>Triqui Ajax Post</code> as template</p>
<p><img src="/wp-content/uploads/2010/04/wp_ajax.jpg" alt="" /></p>
<p>And that&#8217;s it&#8230; you don&#8217;t need anything else, and the blog will work as in the video.</p>
<p>Enjoy</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/04/01/loading-wordpress-posts-with-ajax-and-jquery/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Including jQuery Ajax calls in your WordPress blog</title>
		<link>http://www.emanueleferonato.com/2010/03/30/including-jquery-ajax-calls-in-your-wordpress-blog/</link>
		<comments>http://www.emanueleferonato.com/2010/03/30/including-jquery-ajax-calls-in-your-wordpress-blog/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 15:33:46 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2637</guid>
		<description><![CDATA[I want to show you how to include jQuery ajax calls in your WordPress blog. The reason is simple: sometimes you may need to update the content of a WP page without reloading the entire page. This is useful when you want the user to interact with your blog without forcing him to reload the [...]]]></description>
			<content:encoded><![CDATA[<p>I want to show you how to include jQuery ajax calls in your WordPress blog.</p>
<p>The reason is simple: sometimes you may need to update the content of a WP page without reloading the entire page.</p>
<p>This is useful when you want the user to interact with your blog without forcing him to reload the page. Or when you want some events to happen &#8220;automatically&#8221; without user interaction.</p>
<p>In this video I am changing the header at every second without reloading the page.</p>
<p><object width="520" height="420"><param name="movie" value="http://www.youtube.com/v/DbEC5qjlS8E&#038;hl=it&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/DbEC5qjlS8E&#038;hl=it&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="520" height="420"></embed></object></p>
<p>Also notice I am doing it from the default Kubrick theme with a fresh installation&#8230; I am not using any jQuery powered theme or plugin. Let&#8217;s see how can you make it.<span id="more-2637"></span></p>
<p>In this case all changes have been made in <code>header.php</code> file, adding nine lines after</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> wp_head<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></div></div>

<p>Here they are:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$.<span style="color: #660066;">ajaxSetup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>cache<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> refresh <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>   	
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#description_span&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;?php bloginfo('template_url'); ?&gt;/example.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>First, I am loading jQuery using Google as content delivery network.</p>
<p>Then, when the document is ready, I disable the cache to stop jQuery <code>.load</code> response from being cached.</p>
<p>Now I just execute an ajax call to <code>example.php</code> file at every second and print the result in the element with <code>description_span</code> id</p>
<p>This is <code>example.php</code> content</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>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now it&#8217;s time to locate <code>description_span</code> element&#8230; it&#8217;s in the header, next to the description of the blog.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;description&quot;&gt;&lt;?php bloginfo('description'); ?&gt; like other &lt;span id = &quot;description_span&quot;&gt;&lt;/span&gt; ones&lt;/div&gt;</pre></div></div>

<p>And that&#8217;s it. This time I changed the header without touching the content, next time (the aim of my experiment) I&#8217;ll change the content without touching the header, maybe because the header contains a Flash game called <a href="http://www.emanueleferonato.com/2010/03/29/lineball-flash-game-sponsored-and-released/">LineBall</a>&#8230; (yes, I am designing a WP theme for a game official site).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/03/30/including-jquery-ajax-calls-in-your-wordpress-blog/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Horror Profile Facebook application source code released</title>
		<link>http://www.emanueleferonato.com/2009/12/04/horror-profile-facebook-application-source-code-released/</link>
		<comments>http://www.emanueleferonato.com/2009/12/04/horror-profile-facebook-application-source-code-released/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 09:23:57 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=2027</guid>
		<description><![CDATA[Horror Profile is a Facebook application I developed a month ago. Now it&#8217;s time to release the source code. You will find some useful tips about dynamic image creation with php. If you are absolutely new to Facebook applications, I suggest you to read &#8220;Developing a Facebook Application for absolute beginners&#8221; posts 1, 2, 3, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://apps.facebook.com/horror_name/" target ="_blank"><strong>Horror Profile</strong></a> is a Facebook application <a href="http://www.emanueleferonato.com/2009/10/28/horror-profile-facebook-application/">I developed a month ago</a>.</p>
<p>Now it&#8217;s time to release  the source code. You will find some useful tips about dynamic image creation with php.</p>
<p>If you are absolutely new to Facebook applications, I suggest you to read &#8220;Developing a Facebook Application for absolute beginners&#8221; posts <a href="http://www.emanueleferonato.com/2009/09/20/developing-a-facebook-application-for-absolute-beginners/">1</a>, <a href="http://www.emanueleferonato.com/2009/09/28/developing-a-facebook-application-for-absolute-beginners-step-2/">2</a>, <a href="http://www.emanueleferonato.com/2009/09/30/developing-a-facebook-application-for-absolute-beginners-%E2%80%93-step-3/">3</a>, <a href="http://www.emanueleferonato.com/2009/10/07/developing-a-facebook-application-for-absolute-beginners-step-4/">4</a> and <a href="http://www.emanueleferonato.com/2009/10/09/developing-a-facebook-application-for-absolute-beginners-step-5/">5</a>.</p>
<p>The idea is simple: placing the profile picture in a mirror (that is a transparent PNG image) and writing the user first name as big as I can.<span id="more-2027"></span></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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// this is the core function.</span>
<span style="color: #666666; font-style: italic;">// giving an user id, a text (user first name) and an url (path to the user profile picture)</span>
<span style="color: #666666; font-style: italic;">// creates the horror picture</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> create_pic<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span><span style="color: #000088;">$text</span><span style="color: #339933;">,</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// creation of a new image, the final one</span>
	<span style="color: #000088;">$im</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// importing profile picture</span>
	<span style="color: #000088;">$profile</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatefromjpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// importing transparent mirror image</span>
	<span style="color: #000088;">$mirror</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatefrompng</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mirror.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// retrieving profile picture size</span>
	<span style="color: #000088;">$sizes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$x_dim</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sizes</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: #000088;">$y_dim</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sizes</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// mirror transparency is 265x335, so I resize the profile picture</span>
	<span style="color: #666666; font-style: italic;">// to fit this size, keeping widht/height ratio</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x_dim</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">265</span> or <span style="color: #000088;">$y_dim</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">335</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$mult</span> <span style="color: #339933;">=</span> <span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">265</span><span style="color: #339933;">/</span><span style="color: #000088;">$x_dim</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">335</span><span style="color: #339933;">/</span><span style="color: #000088;">$y_dim</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$final_x</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x_dim</span><span style="color: #339933;">*</span><span style="color: #000088;">$mult</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$final_y</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$y_dim</span><span style="color: #339933;">*</span><span style="color: #000088;">$mult</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// pasting the resized profile picture on the final image</span>
	<span style="color: #990000;">imagecopyresampled</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span><span style="color: #000088;">$profile</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">250</span><span style="color: #339933;">-</span><span style="color: #000088;">$final_x</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">300</span><span style="color: #339933;">-</span><span style="color: #000088;">$final_y</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</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: #339933;">,</span><span style="color: #000088;">$final_x</span><span style="color: #339933;">,</span><span style="color: #000088;">$final_y</span><span style="color: #339933;">,</span><span style="color: #000088;">$x_dim</span><span style="color: #339933;">,</span><span style="color: #000088;">$y_dim</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// pasting the mirror on the final image</span>
	<span style="color: #990000;">imagecopyresampled</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span><span style="color: #000088;">$mirror</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: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">500</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">500</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">500</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// creating some colors</span>
	<span style="color: #000088;">$color</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">125</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;">$shadow</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</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: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// this is the path of the font I am using</span>
	<span style="color: #000088;">$font</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SF Gushing Meadow.ttf&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// starting at font size 0</span>
	<span style="color: #000088;">$size</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// boolean variable to determine if the font fits on the picture</span>
	<span style="color: #000088;">$it_fits</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// increasing font zize by one unit until it won't fit anymore</span>
	<span style="color: #b1b100;">do</span><span style="color: #009900;">&#123;</span>
	     <span style="color: #000088;">$last_dim</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dim</span><span style="color: #339933;">;</span>
	     <span style="color: #000088;">$size</span><span style="color: #339933;">++;</span>
	     <span style="color: #000088;">$dim</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imageftbbox</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$font</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</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;">$dim</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span><span style="color: #000088;">$dim</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">500</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	          <span style="color: #000088;">$it_fits</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	     <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$it_fits</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// adding the text</span>
	<span style="color: #000088;">$center</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #339933;">-</span><span style="color: #000088;">$last_dim</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #000088;">$last_dim</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</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: #990000;">imagettftext</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #000088;">$size</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$center</span><span style="color: #339933;">,</span> <span style="color: #000088;">$last_dim</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #000088;">$shadow</span><span style="color: #339933;">,</span> <span style="color: #000088;">$font</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">imagettftext</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span> <span style="color: #000088;">$size</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$center</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$last_dim</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color</span><span style="color: #339933;">,</span> <span style="color: #000088;">$font</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// saving the image</span>
	<span style="color: #990000;">imagejpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;temp/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.jpg&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">80</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// freeing memory</span>
	<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$im</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$profile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mirror</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
     <span style="color: #000000; font-weight: bold;">function</span> grant<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         document<span style="color: #339933;">.</span>setLocation<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://apps.facebook.com/horror_name/?pub=1&quot;</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;">&lt;/script&gt;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">&quot;facebook.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$appapikey</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;xxxxx&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$appsecret</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;xxxx&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$facebook</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Facebook<span style="color: #009900;">&#40;</span><span style="color: #000088;">$appapikey</span><span style="color: #339933;">,</span> <span style="color: #000088;">$appsecret</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$user_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">require_login</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$my_array</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api_client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">users_getInfo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_id</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;first_name,pic_big&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// getting user name and profile picture</span>
<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$my_array</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>first_name<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pic</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$my_array</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>pic_big<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// core function: creation of the horror picture</span>
create_pic<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_id</span><span style="color: #339933;">,</span><span style="color: #000088;">$text</span><span style="color: #339933;">,</span><span style="color: #000088;">$pic</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$has_permission</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api_client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">users_hasAppPermission</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;photo_upload&quot;</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;">$has_permission</span> and <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>pub<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$response_array</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">api_client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">photos_upload</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;temp/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$user_id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Created with http://apps.facebook.com/horror_name/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>style<span style="color: #339933;">&gt;</span>
     <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;style.css&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</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>
<span style="color: #339933;">&lt;/</span>style<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;</span>Hello <span style="color: #339933;">&lt;</span>fb<span style="color: #339933;">:</span>name uid<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;?php echo <span style="color: #006699; font-weight: bold;">$user_id</span>; ?&gt;&quot;</span> useyou<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;false&quot;</span> linked<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;false&quot;</span> firstnameonly<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #339933;">&gt;&lt;/</span>fb<span style="color: #339933;">:</span>name<span style="color: #339933;">&gt;,</span> let<span style="color: #0000ff;">'s see how horrific is your profile:&lt;/h2&gt;
&nbsp;
&lt;div class = &quot;image&quot;&gt;&lt;img src = &quot;http://www.gamemummy.com/facebook/horror_name/temp/&lt;?php echo $user_id; ?&gt;.jpg&quot; /&gt;&lt;/div&gt;
&nbsp;
&lt;?php if(!$has_permission){ ?&gt;</span>
     &lt;div class = &quot;publish&quot;&gt;&lt;fb:prompt-permission perms=&quot;photo_upload&quot; next_fbjs=&quot;grant()&quot;&gt;Scare your friends!! Publish this horrific photo on your profile!!&lt;/fb:prompt-permission&gt;&lt;/div&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>pub<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
     	&lt;div class = &quot;publish&quot;&gt;&lt;a href = &quot;http://apps.facebook.com/horror_name/?pub=1&quot;&gt;Scare your friends!! Publish this horrific photo on your profile!!&lt;/a&gt;&lt;/div&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		&lt;div class = &quot;publish&quot;&gt;PHOTO PUBLISHED&lt;/div&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;div class = &quot;ad&quot;&gt;&lt;fb:iframe src = &quot;http://www.gamemummy.com/facebook/horror_name/ad.html&quot; width = &quot;728&quot; height = &quot;90&quot; frameborder = &quot;0&quot;&gt;&lt;/fb:iframe&gt;&lt;/div&gt;
&nbsp;
&lt;ul&gt;
	&lt;li&gt;Concept and coding by &lt;a href = &quot;http://emanueleferonato.com/&quot; target = &quot;_blank&quot;&gt;Emanuele Feronato&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;Picture by &lt;a href = &quot;http://aliceindeadland.deviantart.com/&quot; target = &quot;_blank&quot;&gt;AliceinDeadLand&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</pre></td></tr></table></div>

<p>Hope you will make something decent out of it&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2009/12/04/horror-profile-facebook-application-source-code-released/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
