<?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>stoimen&#039;s web log &#187; web development</title>
	<atom:link href="http://www.stoimen.com/blog/category/web-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stoimen.com/blog</link>
	<description>about web development</description>
	<lastBuildDate>Mon, 06 Feb 2012 20:50:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP Performance: Bitwise Division</title>
		<link>http://www.stoimen.com/blog/2012/01/05/php-performance-bitwise-division/</link>
		<comments>http://www.stoimen.com/blog/2012/01/05/php-performance-bitwise-division/#comments</comments>
		<pubDate>Thu, 05 Jan 2012 15:38:13 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[bitwise operation]]></category>
		<category><![CDATA[bitwise operators]]></category>
		<category><![CDATA[division]]></category>
		<category><![CDATA[performance tests]]></category>
		<category><![CDATA[php performance]]></category>
		<category><![CDATA[research]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2577</guid>
		<description><![CDATA[Recently I wrote about binary search and then I said that in some languages, like PHP, bitwise division by two is not faster than the typical “/” operator. However I decided to make some experiments and here are the results. &#8230; <a href="http://www.stoimen.com/blog/2012/01/05/php-performance-bitwise-division/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2012/01/24/javascript-performance-for-vs-while/' rel='bookmark' title='JavaScript Performance: for vs. while'>JavaScript Performance: for vs. while</a></li>
<li><a href='http://www.stoimen.com/blog/2010/02/02/firebugs-console-time-accuracy/' rel='bookmark' title='Firebug&#8217;s console.time() accuracy'>Firebug&#8217;s console.time() accuracy</a></li>
<li><a href='http://www.stoimen.com/blog/2009/12/30/jquery-live-vs-bind-performance/' rel='bookmark' title='jQuery live() vs bind() performance'>jQuery live() vs bind() performance</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Recently I wrote about <a href="http://www.stoimen.com/blog/2011/12/26/computer-algorithms-binary-search/" title="Computer Algorithms: Binary Search">binary search</a> and then I said that in some languages, like PHP, bitwise division by two is not faster than the typical “/” operator. However I decided to make some experiments and here are the results.</p>
<h2>Important Note</h2>
<p>It’s very important to say that the following results are dependant from the machine and the environment!</p>
<h2>Source Code</h2>
<p>Here&#8217;s the PHP source code.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> divide<span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$a</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>
	<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;">$n</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: #cc66cc;">300</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">echo</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> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
divide<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//divide(1000);</span>
<span style="color: #666666; font-style: italic;">//divide(10000);</span>
<span style="color: #666666; font-style: italic;">//divide(100000);</span>
<span style="color: #666666; font-style: italic;">//divide(1000000);</span>
<span style="color: #666666; font-style: italic;">//divide(10000000);</span></pre></div></div>

<p><span id="more-2577"></span><br />
and bitwise &#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> bitwise<span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$a</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>
	<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;">$n</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: #cc66cc;">300</span> <span style="color: #339933;">&gt;&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">echo</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> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
bitwise<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//bitwise(1000);</span>
<span style="color: #666666; font-style: italic;">//bitwise(10000);</span>
<span style="color: #666666; font-style: italic;">//bitwise(100000);</span>
<span style="color: #666666; font-style: italic;">//bitwise(1000000);</span>
<span style="color: #666666; font-style: italic;">//bitwise(10000000);</span></pre></div></div>

<p>Note that each method was called 6 times with the same parameter. This means that divide(100) was called 6 times and then I used the average value of these six times.</p>
<h2>Results</h2>
<p>I said back then in my binary search post, that in PHP the bitwise operator &#8220;>> 1&#8243; is not faster than the typical division with the &#8220;/&#8221; operator. However the results tells us that using bitwise division is slightly faster, as you can see at the diagram bellow.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">n		<span style="color: #0000ff;">&quot;&gt;&gt;&quot;</span>			<span style="color: #0000ff;">&quot;/&quot;</span>
<span style="color: #cc66cc;">100</span>		<span style="color:#800080;">0.0002334912618</span>		<span style="color:#800080;">0.000311803817749</span>
<span style="color: #cc66cc;">1000</span>		<span style="color:#800080;">0.001911004384359</span>	<span style="color:#800080;">0.007335503896078</span>
<span style="color: #cc66cc;">10000</span>		<span style="color:#800080;">0.013423800468445</span>	<span style="color:#800080;">0.039460102717081</span>
<span style="color: #cc66cc;">100000</span>		<span style="color:#800080;">0.14417803287506</span>	<span style="color:#800080;">0.21413381894429</span>
<span style="color: #cc66cc;">1000000</span>		<span style="color:#800080;">1.15839115778605</span>	<span style="color:#800080;">1.17152162392935</span>
<span style="color: #cc66cc;">10000000</span>	<span style="color:#800080;">10.556711634</span>		<span style="color:#800080;">11.0911625623705</span></pre></div></div>

<div id="attachment_2596" class="wp-caption alignnone" style="width: 610px"><a href="http://www.stoimen.com/blog/wp-content/uploads/2012/01/bitwise-divide-by-two.png"><img src="http://www.stoimen.com/blog/wp-content/uploads/2012/01/bitwise-divide-by-two.png" alt="PHP Performance: Bitwise division is slightly faster!" title="bitwise-divide-by-two" width="600" height="371" class="size-full wp-image-2596" /></a><p class="wp-caption-text">Bitwise division is slightly faster!</p></div>
<h2>Conclusion</h2>
<p>Although bitwise division is a bit faster the difference is so small that you should work with very large data in order to gain some performance.</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2012/01/24/javascript-performance-for-vs-while/' rel='bookmark' title='JavaScript Performance: for vs. while'>JavaScript Performance: for vs. while</a></li>
<li><a href='http://www.stoimen.com/blog/2010/02/02/firebugs-console-time-accuracy/' rel='bookmark' title='Firebug&#8217;s console.time() accuracy'>Firebug&#8217;s console.time() accuracy</a></li>
<li><a href='http://www.stoimen.com/blog/2009/12/30/jquery-live-vs-bind-performance/' rel='bookmark' title='jQuery live() vs bind() performance'>jQuery live() vs bind() performance</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2012/01/05/php-performance-bitwise-division/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>OOP JavaScript: Accessing Public Methods in Private Methods</title>
		<link>http://www.stoimen.com/blog/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/</link>
		<comments>http://www.stoimen.com/blog/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/#comments</comments>
		<pubDate>Thu, 28 Jul 2011 12:05:42 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Accessing Private]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[Object-oriented programming]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[private]]></category>
		<category><![CDATA[public]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Subroutine]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2339</guid>
		<description><![CDATA[As you know in JavaScript when you define a variable with the special word &#8220;var&#8221; the scope of this variable is within the function. So when you simply wite &#8220;var a = 5&#8243; the variable named &#8220;a&#8221; has a global &#8230; <a href="http://www.stoimen.com/blog/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/05/30/object-oriented-javascript-inheritance/' rel='bookmark' title='Object Oriented JavaScript: Inheritance'>Object Oriented JavaScript: Inheritance</a></li>
<li><a href='http://www.stoimen.com/blog/2011/10/20/some-notes-on-the-object-oriented-model-of-php/' rel='bookmark' title='Some Notes on the Object-oriented Model of PHP'>Some Notes on the Object-oriented Model of PHP</a></li>
<li><a href='http://www.stoimen.com/blog/2010/08/11/quick-look-at-javascript-objects/' rel='bookmark' title='Quick Look at JavaScript Objects'>Quick Look at JavaScript Objects</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>As you know in JavaScript when you define a variable with the special word &#8220;var&#8221; the scope of this variable is within the function. So when you simply wite &#8220;var a = 5&#8243; the variable named &#8220;a&#8221; has a global scope and can be accessed in any function in the global scope.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> f<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> a<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// returns 5</span></pre></div></div>

<p>Thus f will return the value of &#8220;a&#8221; which equals to 5. You can also change the value of the global variable in the function body.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> f<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">return</span> a<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// equals to 10</span></pre></div></div>

<p>Now after we call the function f the value of &#8220;a&#8221; will equal to 10. This is because we reference the global variable &#8220;a&#8221; into the function body without using the keyword &#8220;var&#8221;. This means that if you put the &#8220;var&#8221; keyword the variable &#8220;a&#8221; inside the function body is no longer the same variable as the variable defined outside the body. It becames &#8220;local&#8221; and it&#8217;s visible only inside the function.<br />
<span id="more-2339"></span></p>
<h2>Objects &#038; JavaScript</h2>
<p>Actually in JavaScript functions can be also objects. Any variable defined with the &#8220;var&#8221; keyword becomes private, because it&#8217;s only visible inside the function body, while by using the &#8220;this&#8221; keyword we can define global variables visible outside the function body. Let&#8217;s see na example.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> f <span style="color: #339933;">=</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> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">b</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> myfunc <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> f<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myfunc.<span style="color: #660066;">a</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// is undefined because a is private</span>
myfunc.<span style="color: #660066;">b</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// equals to 5 because b is public</span></pre></div></div>

<h2>Public and Private Methods in JavaScript</h2>
<p>As you may know in JS you can define functions inside other functions. Actually this is how you can define classes in JavaScript.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myClass <span style="color: #339933;">=</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> f <span style="color: #339933;">=</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: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>But this in fact defines a local (private) function into myClass and we cannot access it from the outside world. Here&#8217;s a fully functional class with one public and one private method.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myClass <span style="color: #339933;">=</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: #006600; font-style: italic;">// public method</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">a</span> <span style="color: #339933;">=</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: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// private method</span>
	<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</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: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> myClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
c.<span style="color: #660066;">a</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// will return 10</span>
c.<span style="color: #660066;">b</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// is undefined, because is private</span></pre></div></div>

<h2>Accessing Private Methods from Public Methods</h2>
<p>Easily we can access private methods from public ones.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myClass <span style="color: #339933;">=</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: #006600; font-style: italic;">// public method</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">a</span> <span style="color: #339933;">=</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: #000066; font-weight: bold;">return</span> b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// private method</span>
	<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</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: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>However accessing public mtehods in private ones is more difficult. That&#8217;s because we cannot use &#8220;this&#8221; into the private methods, just because &#8220;this&#8221; refers to the private method<br />
itself and not to the global method, which is &#8220;myClass&#8221; in our case.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myClass <span style="color: #339933;">=</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: #006600; font-style: italic;">// public method</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">a</span> <span style="color: #339933;">=</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: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// private method</span>
	<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</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: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">a</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// this will result in an error</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Accessing Public Methods from Private Methods</h2>
<p>To access public methods in private methods you need to define a variable that points to the global &#8220;this&#8221; object.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myClass <span style="color: #339933;">=</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> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span> 
&nbsp;
	<span style="color: #006600; font-style: italic;">// public method</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">a</span> <span style="color: #339933;">=</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: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// private method</span>
	<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</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: #000066; font-weight: bold;">return</span> self.<span style="color: #660066;">a</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// this will return 10</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This variable gives you the bridge between private methods and global &#8220;this&#8221; pointer.</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/05/30/object-oriented-javascript-inheritance/' rel='bookmark' title='Object Oriented JavaScript: Inheritance'>Object Oriented JavaScript: Inheritance</a></li>
<li><a href='http://www.stoimen.com/blog/2011/10/20/some-notes-on-the-object-oriented-model-of-php/' rel='bookmark' title='Some Notes on the Object-oriented Model of PHP'>Some Notes on the Object-oriented Model of PHP</a></li>
<li><a href='http://www.stoimen.com/blog/2010/08/11/quick-look-at-javascript-objects/' rel='bookmark' title='Quick Look at JavaScript Objects'>Quick Look at JavaScript Objects</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Object Oriented JavaScript: Inheritance</title>
		<link>http://www.stoimen.com/blog/2011/05/30/object-oriented-javascript-inheritance/</link>
		<comments>http://www.stoimen.com/blog/2011/05/30/object-oriented-javascript-inheritance/#comments</comments>
		<pubDate>Mon, 30 May 2011 09:43:53 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[code sample]]></category>
		<category><![CDATA[code snippet]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Literal]]></category>
		<category><![CDATA[object oriented]]></category>
		<category><![CDATA[Object-oriented programming]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[oop javascript]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Software engineering]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2314</guid>
		<description><![CDATA[Objects and JavaScript JavaScript, being a functional language, differs from most of the procedural/object oriented languages we know. The object oriented approach in JavaScript is rather strange. However there is much power in making objects! The syntax is really odd &#8230; <a href="http://www.stoimen.com/blog/2011/05/30/object-oriented-javascript-inheritance/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/' rel='bookmark' title='OOP JavaScript: Accessing Public Methods in Private Methods'>OOP JavaScript: Accessing Public Methods in Private Methods</a></li>
<li><a href='http://www.stoimen.com/blog/2011/10/20/some-notes-on-the-object-oriented-model-of-php/' rel='bookmark' title='Some Notes on the Object-oriented Model of PHP'>Some Notes on the Object-oriented Model of PHP</a></li>
<li><a href='http://www.stoimen.com/blog/2010/08/11/quick-look-at-javascript-objects/' rel='bookmark' title='Quick Look at JavaScript Objects'>Quick Look at JavaScript Objects</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>Objects and JavaScript</h2>
<p><a href="http://www.stoimen.com/blog/tag/javascript/" title="JavaScript on Stoimen.com">JavaScript</a>, being a functional language, differs from most of the procedural/object oriented languages we know. The object oriented approach in JavaScript is rather strange. However there is much power in making objects! The syntax is really odd and there are several approaches.</p>
<h2>Literal Notation</h2>
<p>As many of you may know the most used notation is the JSON (JavaScript Object Notation).</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'key1'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'val1'</span>
<span style="color: #339933;">,</span> <span style="color: #3366CC;">'key2'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'val2'</span>
<span style="color: #339933;">,</span> <span style="color: #3366CC;">'key3'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'val3'</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Of course this is the very basic example. You can use as value any JavaScript object &#8211; another similar object or a function.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'key1'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'val1'</span>
<span style="color: #339933;">,</span> <span style="color: #3366CC;">'key2'</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'inner_key1'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'inner_val1'</span> <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">,</span> <span style="color: #3366CC;">'key3'</span> <span style="color: #339933;">:</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: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">10</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
		 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The two examples above are showing an anonymous object in JavaScript, but you can assign this code to some variable.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myObject <span style="color: #339933;">=</span> 
	<span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'key1'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'val1'</span>
	<span style="color: #339933;">,</span> <span style="color: #3366CC;">'key2'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'val2'</span>
	<span style="color: #339933;">,</span> <span style="color: #3366CC;">'key3'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'val3'</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>or</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myObject <span style="color: #339933;">=</span>
	<span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'key1'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'val1'</span>
	<span style="color: #339933;">,</span> <span style="color: #3366CC;">'key2'</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'inner_key1'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'inner_val1'</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #339933;">,</span> <span style="color: #3366CC;">'key3'</span> <span style="color: #339933;">:</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: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">10</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
			 <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and then you can call the properties of these objects with the &#8216;.&#8217; operator:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">myObject.<span style="color: #660066;">key1</span><span style="color: #339933;">;</span>
myObject.<span style="color: #660066;">key2</span>.<span style="color: #660066;">inner_key1</span><span style="color: #339933;">;</span>
myObject.<span style="color: #660066;">key3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So far so good &#8211; this is the literal object notation in JavaScript. However there is another &#8220;objects&#8221; in JavaScript.<br />
<span id="more-2314"></span></p>
<h2>Objects and Functions in JavaScript</h2>
<p>Actually in JS the functions are also objects (classes). So you can define a function and then define another function inside or use the &#8220;this&#8221; operator.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">print</span> <span style="color: #339933;">=</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: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then you can make a new instance of this &#8220;class&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myObject <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> a<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hello world'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> anotherObject <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> a<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'some test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
myObject.<span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// will print the string &quot;hello world&quot;</span>
anotherObject.<span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// will print the string &quot;some test&quot;</span></pre></div></div>

<p>Thus the function <strong>&#8220;a&#8221;</strong> defined within the code <strong>&#8220;var a = function(name) &#8230;&#8221;</strong> is a class in JavaScript. You can make instances (objects) of this class with the &#8220;new&#8221; operator.</p>
<p>You can use another notation for defining the function &#8220;a&#8221;, so both can be used and it&#8217;s up to you to decide which one is more convenient.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> a<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">print</span> <span style="color: #339933;">=</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: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Prototypes</h2>
<p>JS gives you more power. In most of the languages you&#8217;ve to define the class first and then make objects. In JS you can define a &#8220;class&#8221; then make some objects and then add some properties to the class by using the prototype.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> myObj <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> a<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hello world'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
a.<span style="color: #660066;">prototype</span>.<span style="color: #000066;">print</span> <span style="color: #339933;">=</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: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span>
&nbsp;
myObj.<span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So you can see how you can add functions and properties to an already defined class using the prototype. Thus you can call the &#8220;print&#8221; function on the myObj object.</p>
<h2>Inheritance</h2>
<p>The prototype built-in property can be used to make use of one of the main OOP features &#8211; inheritance.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> SuperClass <span style="color: #339933;">=</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: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">superFunction</span> <span style="color: #339933;">=</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: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'function in Super Class'</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> sup <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> SuperClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
sup.<span style="color: #660066;">superFunction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// will return the string &quot;function in Super Class&quot;</span></pre></div></div>

<p>Now you can add one more class and set its prototype to the SuperClass</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> SubClass <span style="color: #339933;">=</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: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subFunction</span> <span style="color: #339933;">=</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: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'function in Sub Class'</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span>
&nbsp;
SubClass.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> SuperClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Thus you make SubClass inherit from SuperClass. Now you can call the SuperClass properties from any SubClass object.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> sub <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> SubClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
sub.<span style="color: #660066;">superFunction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// will return the string &quot;function in Super Class&quot;</span>
sub.<span style="color: #660066;">subFunction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// will return the string &quot;function in Sub Class&quot;</span></pre></div></div>

<p>Even more! You can add properties to the super class with prototype.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">SuperClass.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">myFunc</span> <span style="color: #339933;">=</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: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'my new function'</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And then call it from the sub class:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">sub.<span style="color: #660066;">myFunc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// will return the string &quot;my new function&quot;</span></pre></div></div>

<h2>Override a Function</h2>
<p>What happens if you have the same function name in both classes. Actually this overrides the method in the sub class.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> SuperClass <span style="color: #339933;">=</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: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">myFunc</span> <span style="color: #339933;">=</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: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'function in Super Class'</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> SubClass <span style="color: #339933;">=</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: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">myFunc</span> <span style="color: #339933;">=</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: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'function in Sub Class'</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>	
<span style="color: #009900;">&#125;</span>
&nbsp;
SubClass.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> SuperClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> sub <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> SubClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
sub.<span style="color: #660066;">myFunc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// will return the string &quot;function in Sub Class&quot;</span></pre></div></div>

<p>while</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> sup <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> SuperClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
sup.<span style="color: #660066;">myFunc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// will return the string &quot;function in Super Class&quot;</span></pre></div></div>

<h2>Conclusion</h2>
<p>Now you know there are classes, inheritance and overrides in JavaScript &#8211; thus you can improve your object oriented JavaScript!</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/' rel='bookmark' title='OOP JavaScript: Accessing Public Methods in Private Methods'>OOP JavaScript: Accessing Public Methods in Private Methods</a></li>
<li><a href='http://www.stoimen.com/blog/2011/10/20/some-notes-on-the-object-oriented-model-of-php/' rel='bookmark' title='Some Notes on the Object-oriented Model of PHP'>Some Notes on the Object-oriented Model of PHP</a></li>
<li><a href='http://www.stoimen.com/blog/2010/08/11/quick-look-at-javascript-objects/' rel='bookmark' title='Quick Look at JavaScript Objects'>Quick Look at JavaScript Objects</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/05/30/object-oriented-javascript-inheritance/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Scroll an IFRAME Content to a Predefined Position</title>
		<link>http://www.stoimen.com/blog/2011/03/29/scroll-an-iframe-content-to-a-predefined-position/</link>
		<comments>http://www.stoimen.com/blog/2011/03/29/scroll-an-iframe-content-to-a-predefined-position/#comments</comments>
		<pubDate>Tue, 29 Mar 2011 11:43:36 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[absolute iframe content]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[iframe]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2238</guid>
		<description><![CDATA[IFRAME Source Usually when you use an IFRAME tag to link an external source the page that&#8217;s referenced by the SRC attributes is loaded at the top left corner. This is the default behavior, but sometimes you&#8217;d like to show &#8230; <a href="http://www.stoimen.com/blog/2011/03/29/scroll-an-iframe-content-to-a-predefined-position/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2009/04/24/scroll-the-page-with-javascript/' rel='bookmark' title='Scroll the page with JavaScript'>Scroll the page with JavaScript</a></li>
<li><a href='http://www.stoimen.com/blog/2009/06/11/remove-iframe-border-on-ie/' rel='bookmark' title='remove iframe border on IE'>remove iframe border on IE</a></li>
<li><a href='http://www.stoimen.com/blog/2010/01/24/css-default-position-value/' rel='bookmark' title='CSS default position value'>CSS default position value</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>IFRAME Source</h2>
<p>Usually when you use an <a href="http://www.stoimen.com/blog/tag/iframe/">IFRAME</a> tag to link an external source the page that&#8217;s referenced by the SRC attributes is loaded at the top left corner. This is the default behavior, but sometimes you&#8217;d like to show to your users not the entire page from the top left corner, but to show only part of the external page instead.</p>
<p>In most of the cases the reference is external and you don&#8217;t have control over the external page. Thus you&#8217;ve to scroll the IFRAME content to the desired position. This of course is impossible. Yeah, there are some JavaScript hacks, but they&#8217;re all a bad solution, because the scrolling occurs only after the page is loaded.</p>
<h2>The Solution</h2>
<p>You can wrap the <a href="http://www.stoimen.com/blog/tag/iframe/">IFRAME</a> into a div and scroll the DIV content using absolute TOP and LEFT <a href="http://www.stoimen.com/blog/tag/css/">CSS</a> properties.</p>
<p>Here&#8217;s an example:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#my-div</span>
<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">width</span>    <span style="color: #00AA00;">:</span> <span style="color: #933;">400px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span>   <span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">overflow</span> <span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">position</span> <span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#my-iframe</span>
<span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">position</span> <span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">top</span>      <span style="color: #00AA00;">:</span> <span style="color: #933;">-100px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">left</span>     <span style="color: #00AA00;">:</span> <span style="color: #933;">-100px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span>    <span style="color: #00AA00;">:</span> <span style="color: #933;">1280px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span>   <span style="color: #00AA00;">:</span> <span style="color: #933;">1200px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Here you have one DIV with dimensions 400x200px. Now by moving the IFRAME within it you can position it on the right place.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;my-div&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">iframe</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.example.com&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;my-iframe&quot;</span> <span style="color: #000066;">scrolling</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;no&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">iframe</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p><span id="more-2238"></span><br />
Example with the awesome <a href="http://www.yahoo.com/" target="_blank" title="Yahoo! Homepage">Yahoo! Homepage</a>:<br />
<iframe src="http://stoimen.com/projects/iframe.scrolled.content/" scrolling="no" frameborder="0" style="width:400px;height:390px"></iframe></p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2009/04/24/scroll-the-page-with-javascript/' rel='bookmark' title='Scroll the page with JavaScript'>Scroll the page with JavaScript</a></li>
<li><a href='http://www.stoimen.com/blog/2009/06/11/remove-iframe-border-on-ie/' rel='bookmark' title='remove iframe border on IE'>remove iframe border on IE</a></li>
<li><a href='http://www.stoimen.com/blog/2010/01/24/css-default-position-value/' rel='bookmark' title='CSS default position value'>CSS default position value</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/03/29/scroll-an-iframe-content-to-a-predefined-position/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How to Collect the Images and Meta Tags from a Webpage with PHP</title>
		<link>http://www.stoimen.com/blog/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/</link>
		<comments>http://www.stoimen.com/blog/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/#comments</comments>
		<pubDate>Fri, 25 Feb 2011 09:23:50 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Facebook Inc]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[HTML element]]></category>
		<category><![CDATA[LinkedIn Corporation]]></category>
		<category><![CDATA[Meta element]]></category>
		<category><![CDATA[Meta Tags]]></category>
		<category><![CDATA[Online social networking]]></category>
		<category><![CDATA[Search engine optimization]]></category>
		<category><![CDATA[search engines]]></category>
		<category><![CDATA[social services]]></category>
		<category><![CDATA[tag]]></category>
		<category><![CDATA[Technical communication]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[Web page]]></category>
		<category><![CDATA[World Wide Web]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2216</guid>
		<description><![CDATA[Meta Tags and the Facebook Example You&#8217;ve definitely seen the &#8220;share a link&#8221; screen in Facebook. When you paste a link into the box (fig. 1) and press the &#8220;Attach&#8221; button you&#8217;ll get the prompted cite parsed with a title, &#8230; <a href="http://www.stoimen.com/blog/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2010/04/20/how-to-sanitize-user-input-in-php/' rel='bookmark' title='How to Sanitize User Input in PHP?'>How to Sanitize User Input in PHP?</a></li>
<li><a href='http://www.stoimen.com/blog/2010/09/10/automatically-upload-images-with-php-directly-from-the-uri/' rel='bookmark' title='Automatically Upload Images with PHP Directly from the URI'>Automatically Upload Images with PHP Directly from the URI</a></li>
<li><a href='http://www.stoimen.com/blog/2010/08/03/html-tags/' rel='bookmark' title='HTML Tags: &lt;base&gt;'>HTML Tags: &lt;base&gt;</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>Meta Tags and the Facebook Example</h2>
<p>You&#8217;ve definitely seen the &#8220;share a link&#8221; screen in <a title="Facebook Homepage" href="http://www.facebook.com/" target="_blank">Facebook</a>. When you paste a link into the box (fig. 1) and press the &#8220;Attach&#8221; button you&#8217;ll get the prompted cite parsed with a title, description and possibly thumb (fig. 2). This functionality is well known in Facebook, but it appears to be well known also in various social services. In fact <a title="LinkedIn Homepage" href="http://www.linkedin.com/" target="_blank">Linkedin</a>, <a title="Reddit Homepage" href="http://www.reddit.com/" target="_blank">Reddit</a>, <a title="DZone Homepage" href="http://www.dzone.com/" target="_blank">Dzone</a>&#8216;s <a title="DZone Bookmarklet" href="http://www.dzone.com/links/add.html" target="_blank">bookmarklet</a> use it.</p>
<div id="attachment_2222" class="wp-caption aligncenter" style="width: 528px"><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/02/facebook_attach_prompt_screen.png"><img class="size-full wp-image-2222" title="facebook_attach_prompt_screen" src="http://www.stoimen.com/blog/wp-content/uploads/2011/02/facebook_attach_prompt_screen.png" alt="Facebook Attach a Link Prompt Screen" width="518" height="155" /></a><p class="wp-caption-text">fig. 1 - Facebook Attach a Link Prompt Screen</p></div>
<p>Fist thing to notice is that this information, prompted by Facebook, is the same as the meta tag information. However there is a slight difference.</p>
<div id="attachment_2224" class="wp-caption aligncenter" style="width: 536px"><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/02/facebook_attached_link_screen.png"><img class="size-full wp-image-2224" title="facebook_attached_link_screen" src="http://www.stoimen.com/blog/wp-content/uploads/2011/02/facebook_attached_link_screen.png" alt="Facebook Attached Link Screen" width="526" height="362" /></a><p class="wp-caption-text">fig. 2 - Facebook Attached Link Screen</p></div>
<p>Facebook prefers for the thumb the image set into the &lt;meta property=&#8221;og:image&#8221; &#8230; /&gt;. In the case above this tag appears to be:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> property<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;og:image&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://b.vimeocdn.com/ts/572/975/57297584_200.jpg&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>And the image pointed in the SRC attribute is exactly the same as the one prompted by Facebook (fig. 3).</p>
<div id="attachment_2229" class="wp-caption aligncenter" style="width: 210px"><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/02/vimeo_thumb.jpg"><img class="size-full wp-image-2229" title="vimeo_thumb" src="http://www.stoimen.com/blog/wp-content/uploads/2011/02/vimeo_thumb.jpg" alt="Vimeo Thumb" width="200" height="150" /></a><p class="wp-caption-text">fig. 3 - Vimeo Thumb</p></div>
<p>First thing to note is that the real thumb is bigger than the thumb shown in Facebook, so Facebook resizes it and the second thing to note is that there are more meta tags of the og:&#8230; format.<span id="more-2216"></span></p>
<h2>Meta Tags and The Open Graph Protocol</h2>
<p>By default meta tags contain various information about the web page. They are not visible in the webpage, but contain some info about it. The most common meta tags are the title, description and keywords tags. They of course contain the title of the page, not that this can be different from the &lt;title&gt; tag, a short description of the page and some keywords describing the content of the page. They are well known also because the search engines make use of them when trying to collect information about the page and the process of SEO passes through it.</p>
<p>However the <a title="Default HTML Meta Tags Specification" href="http://www.w3schools.com/tags/tag_meta.asp" target="_blank">default HTML meta tags</a> cannot contain everything. Thus for example you cannot point the preferable thumbnail for a webpage. The solution is the <a title="The Open Graph Protocol Homepage" href="http://ogp.me/" target="_blank">Open Graph Protocol</a>. It comes with meta tags that can contain more and more valuable info. Such a tag is the og:image meta tag. Note that all the Open Graph (og) meta tags are defined by the og: prefix before the entity name. Thus og:image comes for images, while og:longitude for geo positioning.</p>
<p>That&#8217;s really useful, but how you can read them?</p>
<h2>PHP, Meta Tags and Regexps</h2>
<p>When you try to read information from a webpage source the first possible path is by using <a title="Regular Expressions Explained on Wikipedia" href="http://en.wikipedia.org/wiki/Regular_expression" target="_blank">regular expressions</a>. However PHP is smart enough to offer you some useful functions. Such a function is <a title="PHP get_meta_tags Function Documentation" href="http://php.net/manual/en/function.get-meta-tags.php" target="_blank">get_meta_tags()</a>. As you may guess this method reads the meta tags by given URL.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">get_meta_tags</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://vimeo.com/10758212'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>However this method can&#8217;t read Open Graph tags. So finally you&#8217;ve to use some regexps.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/&lt;meta property=&quot;og:image&quot; content=&quot;(.*?)&quot; \/&gt;/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$source</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now you can grab the og:image tag. And even more &#8211; grab every image (&lt;img&gt;) from that page.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/&lt;img src=&quot;(.*?)&quot;/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$source</span><span style="color: #339933;">,</span> <span style="color: #000088;">$m</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2010/04/20/how-to-sanitize-user-input-in-php/' rel='bookmark' title='How to Sanitize User Input in PHP?'>How to Sanitize User Input in PHP?</a></li>
<li><a href='http://www.stoimen.com/blog/2010/09/10/automatically-upload-images-with-php-directly-from-the-uri/' rel='bookmark' title='Automatically Upload Images with PHP Directly from the URI'>Automatically Upload Images with PHP Directly from the URI</a></li>
<li><a href='http://www.stoimen.com/blog/2010/08/03/html-tags/' rel='bookmark' title='HTML Tags: &lt;base&gt;'>HTML Tags: &lt;base&gt;</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>From SVG to Geo Coordinates &#8211; A Complete Guide</title>
		<link>http://www.stoimen.com/blog/2011/02/11/from-svg-to-geo-coordinates-a-complete-guide/</link>
		<comments>http://www.stoimen.com/blog/2011/02/11/from-svg-to-geo-coordinates-a-complete-guide/#comments</comments>
		<pubDate>Fri, 11 Feb 2011 08:30:11 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Algebraic curves]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[approaching algorithm]]></category>
		<category><![CDATA[Bézier curve]]></category>
		<category><![CDATA[Coordinates]]></category>
		<category><![CDATA[Cubic plane curve]]></category>
		<category><![CDATA[Curves]]></category>
		<category><![CDATA[Differential geometry of curves]]></category>
		<category><![CDATA[Geometry]]></category>
		<category><![CDATA[Graphic design]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[Interpolation]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Splines]]></category>
		<category><![CDATA[text editor]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2174</guid>
		<description><![CDATA[Why This Task Is Not Trivial? First of all what do we have? There is a vector shape, which may represent a map, which we&#8217;d like to convert into a GEO map. In other words there is a SVG file &#8230; <a href="http://www.stoimen.com/blog/2011/02/11/from-svg-to-geo-coordinates-a-complete-guide/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2009/04/14/you-should-not-insert-an-tag-in-another-tag-ie-breaks/' rel='bookmark' title='You should not insert an &#8220;a&#8221; tag in another &#8220;a&#8221; tag! &#8230;'>You should not insert an &#8220;a&#8221; tag in another &#8220;a&#8221; tag! &#8230;</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/29/html5-geolocation-what-if-the-user-doesnt-share-his-position/' rel='bookmark' title='HTML5 geolocation &#8211; What If the User Doesn&#8217;t Share His Position?'>HTML5 geolocation &#8211; What If the User Doesn&#8217;t Share His Position?</a></li>
<li><a href='http://www.stoimen.com/blog/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/' rel='bookmark' title='How to Collect the Images and Meta Tags from a Webpage with PHP'>How to Collect the Images and Meta Tags from a Webpage with PHP</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>Why This Task Is Not Trivial?</h2>
<p>First of all what do we have? There is a vector shape, which may represent a map, which we&#8217;d like to convert into a GEO map. In other words there is a SVG file containing the source shape, that you&#8217;d like to convert in geoJSON or whatever collection of geo points. This is not trivial, of course, first of all because there&#8217;s no an algorithm or automation that can do this, and because everybody knows that the resulting map will be only approached, but will never be so accurate as the vector shape. This is because in a vector shape you may contain <a title="Bézier curve" href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve" target="_blank">Bézier Curves</a>, which I&#8217;ll show a little bit later in this post, that are difficult to represent in geo coordinates.</p>
<p>So the first task will be to find an approaching algorithm. However there are two things that are optimistic:</p>
<ol>
<li>You can&#8217;t effectively represent Bézier curves in geo coordinates, but even if you could do it there&#8217;s no practical need, because the collection of geo coordinates will be huge and this will slow down you&#8217;re application. Remember that geoJSON is yet again possibly used by your browser and the amount of geo points will be proportionally slowing down the app.</li>
<li>As you may know Google&#8217;s visualization map is representing the World&#8217;s countries again with quite approached maps. Take a look at the following image &#8211; the country borders are quite sharpened:</li>
</ol>
<p style="text-align: center;"><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/02/Google_visualization_map.png"><img class="size-full wp-image-2183 aligncenter" title="Google_visualization_map" src="http://www.stoimen.com/blog/wp-content/uploads/2011/02/Google_visualization_map.png" alt="Google Visualization Map" width="562" height="351" /></a></p>
<p>So far we know that we need an approaching algorithm that will convert vector lines and possibly curves in geo coordinates, but before we proceed we&#8217;ve to understand the SVG format.<span id="more-2174"></span></p>
<h2>SVG Format and Possible Approaches</h2>
<p>Let me give you an example of a SVG file. By opening such file with a text editor you can see the file format:</p>

<div class="wp_syntax"><div class="code"><pre class="svg" style="font-family:monospace;">M76.484,153.703c-0.86-0.27-3.816,0.199-4.002-1.055
	c-0.049-0.328-1.109-1.799-1.512-1.824c-0.647-0.04-5.182,1.896-5.219,2.013c-0.051,0.161,0.928,0.627,0.319,0.832
	c-0.028-0.003-0.716-0.36-0.816-0.334c-0.13,0.034-0.262,0.834-0.363,0.771c-0.176-0.24-0.179-0.48-0.007-0.719
	c-0.986-0.764-0.345,0.764-0.526,0.764c-0.771,0,0.184-0.849-0.675,0.037c-2.13-0.433-2.267-0.756-4.129-2.754
	c-1.329-1.42,0.383-8.158-1.571-8.535c-2.223-3.153-6.413-3.037-8.806-5.037c0.107-0.278,0.201-0.563,0.282-0.853
	c0.369,0.372-4.136-0.768-4.373-0.862c-1.498-0.604-3.574-0.133-4.656,0.355c-0.028-1.22,0.171-2.329-0.593-3.027
	c0.588-2.548-0.652-4.752-2.98-4.199c-0.443-1.354,0.142-0.868-1.171-1.483c-0.154-2.069,2.703-7.729-2.239-8.185
	c2.009-3.137-6.201-0.985-6.627-3.125c-0.14-0.703-5.574,1.563-5.752,0.192c-0.39-2.987-4.411-2.185-5.371-3.372
	c0.518,0.641-6.067,1.078-5.645,0.85c-0.118,0.063-1.38,5.328-1.307,4.994c-0.457,2.076,3.774,9.01,2.893,9.625
	c-0.495,0.347-3.159,6.972-1.295,8.588c-0.59-0.512,2.894-0.209,3.163-0.129c1.333,0.396,3.091,1.66,3.469,3.176
	c0.52,2.486-0.793,2.359-2.357,3.013c-0.658,0.274-2.289,6.779-2.368,7.461c-0.319,2.747-1.413,4.468-1.264,7.483
	c0.098,1.984-0.368,2.326,0.891,4.287c1.933,3.004,2.018,2.57,2.178,5.813c-0.127,1.556-1.742,3.565-1.568,5.388
	c0.074,0.77,0.485,2.028,0.235,2.795c-0.286,0.875-1.936,1.557-1.573,2.627c3.253-2.051,4.522,3.092,8.168,2.83
	c0.958-0.069,3.996,1.508,4.536,1.938c1.437,1.12,2.39-1.618,3.232-1.618c-1.037,0,3.461,2.315,2.95,1.774
	c0.828,0.873,3.852-0.123,5.026,0.304c2.416,0.881,6.011-1.386,7.227-2.174c1.616-1.048,3.15-1.38,4.396-2.816
	c0.639-0.737,2.375-4.448,3.254-4.366c0.354,0.032,0.59,1.01,1.06,0.652c0.163-0.126-1.075-2.664,0.689-2.664
	c0.935,0,4.283-2.113,3.799-3.344c-1.384-3.521,4.799-1.365,6.683-1.926c0.335-0.1,7.816-8.23,7.865-8.641
	C70.216,157.721,77.5,154.025,76.484,153.703C76.247,153.629,76.721,153.777,76.484,153.703z</pre></div></div>

<p>This text represent this shape:</p>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/02/svg_map.png"><img class="aligncenter size-full wp-image-2190" title="svg_map" src="http://www.stoimen.com/blog/wp-content/uploads/2011/02/svg_map.png" alt="SVG Map" width="84" height="92" /></a></p>
<p>First thing to notice is there are things quite different from the HTML markup. Of course this things are noticeable. There is a M, c and z where the first two are followed by some numbers. What are these characters? Well lets see at <a title="W3.org SVG" href="http://www.w3.org/TR/SVG/" target="_blank">W3.org</a>. There are lots of SVG specific commands, but we need to understand only few of them.</p>
<p>1.<strong> M</strong> (absolute) <strong>m</strong> (relative) &#8211; moveto</p>
<blockquote><p>Start a new sub-path at the given (x,y) coordinate.           <strong>M</strong> (uppercase) indicates that absolute           coordinates will follow; <strong>m</strong> (lowercase)           indicates that relative coordinates will follow. If a moveto  is           followed by multiple pairs of coordinates, the subsequent  pairs           are treated as implicit lineto commands. Hence, implicit  lineto           commands will be relative if the moveto is relative, and           absolute if the moveto is absolute. If a relative moveto           (<strong>m</strong>) appears as the first element of the path,           then it is treated as a pair of absolute coordinates. In this           case, subsequent pairs of coordinates are treated as relative           even though the initial moveto is interpreted as an absolute  moveto.</p></blockquote>
<p>2. <strong>Z</strong> or <strong>z</strong> &#8211; closepath</p>
<blockquote><p>Close the current subpath by drawing a straight line from the         current point to current subpath&#8217;s initial point. Since the Z  and z         commands take no parameters, they have an identical effect.</p></blockquote>
<p>3. <strong>C</strong> (absolute) <strong>c</strong> (relative) &#8211; curveto (x1 y1 x2 y2 x y)+</p>
<blockquote><p>Draws a cubic Bézier curve from the current         point to (x,y) using (x1,y1) as the control point at the         beginning of the curve and (x2,y2) as the control point at         the end of the curve. <strong>C</strong> (uppercase)         indicates that absolute coordinates will follow;         <strong>c</strong> (lowercase) indicates that relative         coordinates will follow. Multiple sets of coordinates may         be specified to draw a polybézier. At the end of the         command, the new current point becomes the final (x,y)         coordinate pair used in the polybézier.</p></blockquote>
<p>4. <strong>L</strong> (absolute) <strong>l</strong> (relative) &#8211; lineto (x y)+</p>
<blockquote><p>Draw a line from the current point to the given (x,y)         coordinate which becomes the new current point.         <strong>L</strong> (uppercase) indicates that absolute         coordinates will follow; <strong>l</strong> (lowercase)         indicates that relative coordinates will follow. A number         of coordinates pairs may be specified to draw a polyline.         At the end of the command, the new current point is set to         the final set of coordinates provided.</p></blockquote>
<h3>1. Bézier Curves</h3>
<p>So far we know that the especially the c command draws a curve &#8211; a Bézier curve. But what is a Bézier curve anyway? Here&#8217;s a little explanation you can find at <a title="Bézier curve" href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve" target="_blank">Wikipedia</a>. Here&#8217;s an example of a cubic Bézier curve:</p>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/02/Cubic_Bezier_Curve.png"><img class="aligncenter size-full wp-image-2192" title="Cubic_Bezier_Curve" src="http://www.stoimen.com/blog/wp-content/uploads/2011/02/Cubic_Bezier_Curve.png" alt="Cubic Bezier Curve" width="300" height="192" /></a>First thing we see is that here there are four points &#8211; p0, p1, p2, p3. The curve between p0 and p3 is defined by them and the two control points &#8211; p1 and p2. To be more precise let me show you an animation of how this curve is constructed:</p>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/02/Bezier_3_big.gif"><img class="aligncenter size-full wp-image-2193" title="Bezier_3_big" src="http://www.stoimen.com/blog/wp-content/uploads/2011/02/Bezier_3_big.gif" alt="Cubic Bezier Curve" width="360" height="150" /></a></p>
<p>This is the way the red line is constructed with the help of the green and blue lines, and of course the points p1 and p2. The result is a smooth line that&#8217;s often used in animation and shapes. This is our case. As you&#8217;ve seen the example above is constructed only with curves.</p>
<p>Before we proceed let me say that there are lots of types of Bézier curves not only cubic. Here they are:</p>
<h4>1.1. Linear Bézier curve</h4>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/02/Bezier_1_big.gif"><img class="aligncenter size-full wp-image-2194" title="Bezier_1_big" src="http://www.stoimen.com/blog/wp-content/uploads/2011/02/Bezier_1_big.gif" alt="Linear Bezier Curve" width="360" height="150" /></a></p>
<h4>1.2. Quadratic Bézier curve</h4>
<h4><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/02/Bezier_2_big.gif"><img class="aligncenter size-full wp-image-2195" title="Bezier_2_big" src="http://www.stoimen.com/blog/wp-content/uploads/2011/02/Bezier_2_big.gif" alt="Quadratic Bezier Curve" width="360" height="150" /></a>1.3. And event Higher-order  curves:</h4>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/02/Bezier_4_big.gif"><img class="aligncenter size-full wp-image-2196" title="Bezier_4_big" src="http://www.stoimen.com/blog/wp-content/uploads/2011/02/Bezier_4_big.gif" alt="Bezier Curves" width="360" height="150" /></a></p>
<h3>2. What You Need To Know About The SVG Format</h3>
<p>However the most important thing to know is the heart of SVG curve format. First you MOVE to a point with the <strong>M</strong> command. Lets see how does it look in our case:</p>
<p><strong>M76.484,153.703</strong> &#8211; This simply means: move to the point with coordinates (76.484, 153.703)</p>
<p>The next step is to draw a curve. We&#8217;ve seen that a cubic curve is defined by four points &#8211; p0, p1, p2 and p3, but what we have in the SVG format:</p>
<p><strong>c-0.86-0.27-3.816,0.199-4.002-1.055</strong></p>
<p>Here there are only three points (-0.86, -0.27), (-3.816, 0.199), (-4.002, -1.055) &#8211; not four. Well these are only the last three points, the first one is the one defined by the <strong>M</strong> command. Next thing to know is that the <strong>M</strong> command defines an absolute point in the canvas of the SVG file, while the <strong>c</strong> (shouldn&#8217;t be mistaken with <strong>C</strong>) is a relative curve. This means that if the p0 is the point (76.484, 153.703), the p1 point is relative to it with small difference in coordinates &#8211; (-0.86, -0.27).</p>
<p>However the most <em><strong>important</strong></em> thing to know is that after drawing the curve:</p>
<p><em>At the end of the         command, the new current point becomes the final (x,y)         coordinate pair used in the polybézier.</em></p>
<p>This is extremely important! This means that the last point of this curve becomes the p0 of the next curve, and the three points of the second curve, in our case:</p>
<p><strong>c-0.049-0.328-1.109-1.799-1.512-1.824</strong></p>
<p>are relative to the last (x, y) of the previous curve &#8211; (-4.002, -1.055). Without this you cannot convert any SVG shape into geo coordinates.</p>
<h3>3. Lines Instead of Curves</h3>
<p>OK, I said about an approaching algorithm, that is only approaching the curved vector shape. This means that I can simply replace the curves with lines. Thus I&#8217;ve to change to SVG file format from using the <strong>c</strong> command to the <strong>l</strong> command, as<strong> l</strong> is also relative. The curve:</p>
<p><strong>M76.484,153.703 c-0.86-0.27-3.816,0.199-4.002-1.055</strong></p>
<p>will become</p>
<p><strong>M76.484,153.703 l-4.002-1.055</strong></p>
<p>and so forth. This will result into the more sharpened shape:<a href="http://www.stoimen.com/blog/wp-content/uploads/2011/02/SVG_Line_Map.png"><img class="aligncenter size-full wp-image-2199" title="SVG_Line_Map" src="http://www.stoimen.com/blog/wp-content/uploads/2011/02/SVG_Line_Map.png" alt="SVG Line Map" width="86" height="90" /></a></p>
<p>However this is again a SVG file and no Geo coordinates are present. To end up with this there&#8217;s a tiny PHP script that collects all the last pairs from the c commands and converts them to Geo Coordinates relative to the World&#8217;s center &#8211; (Lat, Lon) = (0, 0).</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: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'-0.86-0.27-3.816,0.199-4.002-1.055
c-0.049-0.328-1.109-1.799-1.512-1.824c-0.647-0.04-5.182,1.896-5.219,2.013
c-0.051,0.161,0.928,0.627,0.319,0.832c-0.028-0.003-0.716-0.36-0.816-0.334
c-0.13,0.034-0.262,0.834-0.363,0.771c-0.176-0.24-0.179-0.48-0.007-0.719
c-0.986-0.764-0.345,0.764-0.526,0.764c-0.771,0,0.184-0.849-0.675,0.037
c-2.13-0.433-2.267-0.756-4.129-2.754c-1.329-1.42,0.383-8.158-1.571-8.535
c-2.223-3.153-6.413-3.037-8.806-5.037c0.107-0.278,0.201-0.563,0.282-0.853
c0.369,0.372-4.136-0.768-4.373-0.862c-1.498-0.604-3.574-0.133-4.656,0.355
c-0.028-1.22,0.171-2.329-0.593-3.027c0.588-2.548-0.652-4.752-2.98-4.199
c-0.443-1.354,0.142-0.868-1.171-1.483c-0.154-2.069,2.703-7.729-2.239-8.185
c2.009-3.137-6.201-0.985-6.627-3.125c-0.14-0.703-5.574,1.563-5.752,0.192
c-0.39-2.987-4.411-2.185-5.371-3.372c0.518,0.641-6.067,1.078-5.645,0.85
c-0.118,0.063-1.38,5.328-1.307,4.994c-0.457,2.076,3.774,9.01,2.893,9.625
c-0.495,0.347-3.159,6.972-1.295,8.588c-0.59-0.512,2.894-0.209,3.163-0.129
c1.333,0.396,3.091,1.66,3.469,3.176c0.52,2.486-0.793,2.359-2.357,3.013
c-0.658,0.274-2.289,6.779-2.368,7.461c-0.319,2.747-1.413,4.468-1.264,7.483
c0.098,1.984-0.368,2.326,0.891,4.287c1.933,3.004,2.018,2.57,2.178,5.813
c-0.127,1.556-1.742,3.565-1.568,5.388c0.074,0.77,0.485,2.028,0.235,2.795
c-0.286,0.875-1.936,1.557-1.573,2.627c3.253-2.051,4.522,3.092,8.168,2.83
c0.958-0.069,3.996,1.508,4.536,1.938c1.437,1.12,2.39-1.618,3.232-1.618
c-1.037,0,3.461,2.315,2.95,1.774c0.828,0.873,3.852-0.123,5.026,0.304
c2.416,0.881,6.011-1.386,7.227-2.174c1.616-1.048,3.15-1.38,4.396-2.816
c0.639-0.737,2.375-4.448,3.254-4.366c0.354,0.032,0.59,1.01,1.06,0.652
c0.163-0.126-1.075-2.664,0.689-2.664c0.935,0,4.283-2.113,3.799-3.344
c-1.384-3.521,4.799-1.365,6.683-1.926c0.335-0.1,7.816-8.23,7.865-8.641'</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000088;">$matches</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'c'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$temp</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: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// get the new relative point</span>
    <span style="color: #000088;">$s</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$v</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// a really dummy regex to find the last pair</span>
    <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$s</span><span style="color: #339933;">,</span> <span style="color: #000088;">$m</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    <span style="color: #666666; font-style: italic;">// if the array is not empty</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$temp</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;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$temp</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;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'['</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">', '</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$m</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: #0000ff;">'],&lt;br /&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// save the last point</span>
    <span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$m</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: #009900;">&#125;</span></pre></div></div>

<h2>Final Adjustments</h2>
<p>I guess you don&#8217;t need any shape in the World&#8217;s center as you may not need it in the same scale. There are two things to do. First you can scale the map by multiplying by a factor every point:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// a really dummy regex to find the last pair</span>
    <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$s</span><span style="color: #339933;">,</span> <span style="color: #000088;">$m</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// multiply by some factor</span>
    <span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*=</span> <span style="color:#800080;">0.05</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*=</span> <span style="color: #339933;">-</span><span style="color:#800080;">0.05</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// if the array is not empty</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$temp</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;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$temp</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;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>and than you can move the shape all over the world by adding some offset to the resulting point:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'['</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span><span style="color:#800080;">38.9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">', '</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color:#800080;">10.7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'],&lt;br /&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Further Researches</h2>
<p>Here we just substitute the curves with lines, which is the most primitive approach. What about substituting each curve with corresponding two lines, or three lines. The resulting shape will be far more accurate in compare with the source &#8220;curved&#8221; shape. So there are still lots of things to be done. However this may help you do the job.</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2009/04/14/you-should-not-insert-an-tag-in-another-tag-ie-breaks/' rel='bookmark' title='You should not insert an &#8220;a&#8221; tag in another &#8220;a&#8221; tag! &#8230;'>You should not insert an &#8220;a&#8221; tag in another &#8220;a&#8221; tag! &#8230;</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/29/html5-geolocation-what-if-the-user-doesnt-share-his-position/' rel='bookmark' title='HTML5 geolocation &#8211; What If the User Doesn&#8217;t Share His Position?'>HTML5 geolocation &#8211; What If the User Doesn&#8217;t Share His Position?</a></li>
<li><a href='http://www.stoimen.com/blog/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/' rel='bookmark' title='How to Collect the Images and Meta Tags from a Webpage with PHP'>How to Collect the Images and Meta Tags from a Webpage with PHP</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/02/11/from-svg-to-geo-coordinates-a-complete-guide/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Wanted &#8211; onfocus/onblur. Why They Don&#8217;t Work Always!</title>
		<link>http://www.stoimen.com/blog/2011/02/09/wanted-onfocusonblur-why-they-dont-work-always/</link>
		<comments>http://www.stoimen.com/blog/2011/02/09/wanted-onfocusonblur-why-they-dont-work-always/#comments</comments>
		<pubDate>Wed, 09 Feb 2011 14:11:48 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[DOM events]]></category>
		<category><![CDATA[Focus]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[Markup languages]]></category>
		<category><![CDATA[Tag soup]]></category>
		<category><![CDATA[Technical communication]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[World Wide Web]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2160</guid>
		<description><![CDATA[On Focus Perhaps you think of onfocus and onblur events as a default behavior existing in any web page. This is not quite true! Onfocurs and onblur are well known in web developing (js) and are fired, of course, when &#8230; <a href="http://www.stoimen.com/blog/2011/02/09/wanted-onfocusonblur-why-they-dont-work-always/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2010/04/20/how-to-sanitize-user-input-in-php/' rel='bookmark' title='How to Sanitize User Input in PHP?'>How to Sanitize User Input in PHP?</a></li>
<li><a href='http://www.stoimen.com/blog/2009/12/30/jquery-live-vs-bind-performance/' rel='bookmark' title='jQuery live() vs bind() performance'>jQuery live() vs bind() performance</a></li>
<li><a href='http://www.stoimen.com/blog/2010/09/24/2xjquery-select-a-selector/' rel='bookmark' title='2xjQuery: Select a Selector'>2xjQuery: Select a Selector</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>On Focus</h2>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/02/onfocus.jpg"><img class="alignleft size-full wp-image-2177" title="onfocus" src="http://www.stoimen.com/blog/wp-content/uploads/2011/02/onfocus.jpg" alt="onfocus" width="450" height="253" /></a></p>
<p>Perhaps you think of onfocus and onblur events as a default behavior existing in any web page. This is not quite true! Onfocurs and onblur are well known in web developing (js) and are fired, of course, when the user tries to point something or leaves some element. Onfocurs is fired when the user either goes to an element with the Tab button on with the mouse. When the element is on focus, evidently, the onfocus event is fired. Actually you can see which element is on focus, like an anchor or input, when the element is outlined by the browser by default. In the same scenario, when some element has been on focus and than the user switches to another element, the onblur event is fired. Thus you may guess that this element is no longer on focus.<span id="more-2160"></span></p>
<p>This in general are the onfocus/onblur events. The interesting part is that by default are known as built-in events in every page, but that is wrong.</p>
<h2>Where Are My Events?</h2>
<p>A typical use case is to take the code from some web page, where you&#8217;re sure this works perfectly, you can focus and blur element with no obstacle, but once you paste it inside your markup everything&#8217;s going wrong. Imagine you&#8217;ve the following markup (a typical search box text):</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">onblur</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if(this.value=='') this.value = 'Search in This Site';&quot;</span> <span style="color: #000066;">onfocus</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if(this.value=='Search in This Site') this.value = '';&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>This works perfectly until you paste it to your site. And than nothing works correctly. Why? Where&#8217;s the problem?</p>
<h2>Make Them Work</h2>
<p>Actually the problem isn&#8217;t hiding somewhere in the markup above. It&#8217;s in the HTML definition. There are lots of web pages using strict HTML definition, that looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	...
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
	...</pre></div></div>

<p>while you&#8217;ve to use the pure HTML definition to make onfocus/onblur work.</p>
<p>You&#8217;ve to switch to:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
	...
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
	...</pre></div></div>

<p>Than you can continue using these useful events!</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2010/04/20/how-to-sanitize-user-input-in-php/' rel='bookmark' title='How to Sanitize User Input in PHP?'>How to Sanitize User Input in PHP?</a></li>
<li><a href='http://www.stoimen.com/blog/2009/12/30/jquery-live-vs-bind-performance/' rel='bookmark' title='jQuery live() vs bind() performance'>jQuery live() vs bind() performance</a></li>
<li><a href='http://www.stoimen.com/blog/2010/09/24/2xjquery-select-a-selector/' rel='bookmark' title='2xjQuery: Select a Selector'>2xjQuery: Select a Selector</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/02/09/wanted-onfocusonblur-why-they-dont-work-always/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading GPS Latitude and Longitude from Image and Video Files</title>
		<link>http://www.stoimen.com/blog/2011/02/04/reading-gps-latitude-and-longitude-from-image-and-video-files/</link>
		<comments>http://www.stoimen.com/blog/2011/02/04/reading-gps-latitude-and-longitude-from-image-and-video-files/#comments</comments>
		<pubDate>Fri, 04 Feb 2011 10:16:14 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Apple Inc.]]></category>
		<category><![CDATA[Application software]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Container formats]]></category>
		<category><![CDATA[Digital photography]]></category>
		<category><![CDATA[Exchangeable image file format]]></category>
		<category><![CDATA[ExifTool]]></category>
		<category><![CDATA[Geotagging]]></category>
		<category><![CDATA[GPS]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[jpeg]]></category>
		<category><![CDATA[Metadata]]></category>
		<category><![CDATA[mobile device]]></category>
		<category><![CDATA[Mobile Devices]]></category>
		<category><![CDATA[MPEG-4]]></category>
		<category><![CDATA[navigation software]]></category>
		<category><![CDATA[Nokia E71 Smartphone]]></category>
		<category><![CDATA[QuickTime]]></category>
		<category><![CDATA[Resolution Unit]]></category>
		<category><![CDATA[Smartphones]]></category>
		<category><![CDATA[Symbian]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[web platform]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2153</guid>
		<description><![CDATA[The State of GPS Data from Mobile Devices Most of the mobile devices today support GPS geo tagging. In fact most of them come bundled with navigation software that uses GPS and therefore all the pictures and (maybe) videos can &#8230; <a href="http://www.stoimen.com/blog/2011/02/04/reading-gps-latitude-and-longitude-from-image-and-video-files/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/01/27/few-thoughts-on-web-video/' rel='bookmark' title='Few Thoughts on Web Video'>Few Thoughts on Web Video</a></li>
<li><a href='http://www.stoimen.com/blog/2010/03/05/video-sites-must-use-mp4-and-only-mp4/' rel='bookmark' title='Video sites must use &#8230; mp4 and only mp4!'>Video sites must use &#8230; mp4 and only mp4!</a></li>
<li><a href='http://www.stoimen.com/blog/2010/05/25/download-files-with-zend-framework/' rel='bookmark' title='Download Files with Zend Framework'>Download Files with Zend Framework</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>The State of GPS Data from Mobile Devices</h2>
<p>Most of the mobile devices today support GPS geo tagging. In fact most of them come bundled with navigation software that uses GPS and therefore all the pictures and (maybe) videos can be geo tagged. But as expected different vendors come with different support and formats.<br />
<br clear="all" /><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/02/iphone.png"><img class="alignleft size-full wp-image-2162" title="iphone" src="http://www.stoimen.com/blog/wp-content/uploads/2011/02/iphone.png" alt="" width="120" height="78" /></a><br />
<br clear="all" /></p>
<p><a title="iPhone OS" href="http://en.wikipedia.org/wiki/IOS_%28Apple%29" target="_blank">iPhone OS</a> comes with geotagging both on video and image files, while the latest <a title="Android" href="http://www.android.com/" target="_blank">Android</a> and <a title="Symbian" href="http://blog.symbian.org/" target="_blank">Symbian</a> (the Nokia main OS for smartphones) can geo tag only images.</p>
<p>Even more &#8211; until recently Symbian didn&#8217;t support any geotagging before the installation of an additional software &#8211; such as <a title="Location Tagger" href="http://betalabs.nokia.com/apps/location-tagger" target="_blank">Location Tagger</a> . So generally the things are quite simple:</p>
<ol>
<li>iPhone OS geotags both video and image files;</li>
<li>Android geotags only images;</li>
<li>Symbian geotags only images &#8211; and on some devices this is possible only after installing a software;</li>
</ol>
<p>This is in breve the state of mobile device geotagging!<span id="more-2153"></span></p>
<h2>Why Use GPS Data?</h2>
<p>Perhaps one of the main reasons why not support geotagging especially on video files can be the usage of those geo tags. First of all what a geotag means?<br />
<br clear="all" /><br />
<a href="http://www.stoimen.com/blog/wp-content/uploads/2011/02/android.png"><img class="alignleft size-full wp-image-2165" title="android" src="http://www.stoimen.com/blog/wp-content/uploads/2011/02/android.png" alt="Android Logo" width="150" height="150" /></a><br />
<br clear="all" /><br />
You may know that even Android doesn&#8217;t &#8220;geotag&#8221; videos this is not quite true. Because after using you gallery you can see where those videos are shot. This is fantastic, but actually the real information about where the video has been taken is not into the video file, but it&#8217;s in an additional log file that keeps it. Thus actually the video files doesn&#8217;t know the geo coordinates.</p>
<p>Here comes the problem with video format, because you cannot be sure that every format supports tags that can keep geo coordinates. Actually quicktime&#8217;s MOV can store them, while Symbian&#8217;s 3GP cannot. In fact Symbian cannot store any geo information about video files!</p>
<p>So now we&#8217;ve at least three different formats for each of those three vendors.</p>
<ol>
<li>quicktime for iPhone</li>
<li>mpeg-4 with Android</li>
<li>3gp with Symbian</li>
</ol>
<p>For now I can only say that iPhone can keep the geotag into his video files.<br />
<br clear="all" /><br />
<a href="http://www.stoimen.com/blog/wp-content/uploads/2011/02/symbian.png"><img class="alignleft size-full wp-image-2164" title="symbian" src="http://www.stoimen.com/blog/wp-content/uploads/2011/02/symbian.png" alt="Symbian Logo" width="200" height="55" /></a><br />
<br clear="all" /><br />
But let me return to the question &#8211; why we need this geo tags? Until the video file is on the mobile device &#8211; there&#8217;s no problem. But once you try to download it &#8211; whether on <a title="Flickr" href="http://www.flickr.com/" target="_blank">Flickr</a>, <a title="YouTube" href="http://youtube.com" target="_blank">YouTube</a>, <a title="Picasa Web Albums" href="https://picasaweb.google.com/" target="_blank">Picasa</a>, etc. you&#8217;ll lose any geo information if it&#8217;s not into the file tags. And of course if the above sites can&#8217;t read it!</p>
<p>The general reason to store this data into the file is to move it along with the file. Once you move this file from your mobile device to a web platform you&#8217;ll see where the file has been created.</p>
<h2>EXIF, Exiftool and PHP&#8217;s exif_read_data</h2>
<p>There are several tools to read geotags. For images, and here we talk only for JPEGs, this is the <a title="EXIF" href="http://www.exif.org/" target="_blank">EXIF</a> information. You can download the exif command line program and try to reed data with it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">exif filename.jpg</pre></div></div>

<p>Which can result in something like that:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Manufacturer        <span style="color: #000000; font-weight: bold;">|</span>Nokia                                                     
Model               <span style="color: #000000; font-weight: bold;">|</span>E71                                                       
Orientation         <span style="color: #000000; font-weight: bold;">|</span>top - left                                                
x-Resolution        <span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000;">300.00</span>                                                    
y-Resolution        <span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000;">300.00</span>                                                    
Resolution Unit     <span style="color: #000000; font-weight: bold;">|</span>Inch                                                      
YCbCr Positioning   <span style="color: #000000; font-weight: bold;">|</span>centered                                                  
Compression         <span style="color: #000000; font-weight: bold;">|</span>JPEG compression                                          
x-Resolution        <span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000;">72.00</span>                                                     
y-Resolution        <span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000;">72.00</span>                                                     
Resolution Unit     <span style="color: #000000; font-weight: bold;">|</span>Inch                                                      
FNumber             <span style="color: #000000; font-weight: bold;">|</span>f<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">3.2</span>                                                     
Exif Version        <span style="color: #000000; font-weight: bold;">|</span>Exif Version <span style="color: #000000;">2.2</span>                                          
Date and Time <span style="color: #7a0874; font-weight: bold;">&#40;</span>origi<span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000;">2011</span>:01:<span style="color: #000000;">24</span> <span style="color: #000000;">12</span>:<span style="color: #000000;">15</span>:01                                       
Date and Time <span style="color: #7a0874; font-weight: bold;">&#40;</span>digit<span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000;">2011</span>:01:<span style="color: #000000;">24</span> <span style="color: #000000;">12</span>:<span style="color: #000000;">15</span>:01                                       
ComponentsConfigurat<span style="color: #000000; font-weight: bold;">|</span>Y Cb Cr -                                                 
Aperture            <span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000;">3.20</span> EV <span style="color: #7a0874; font-weight: bold;">&#40;</span>f<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">3.0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>                                           
Light Source        <span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000;">0</span>                                                         
Flash               <span style="color: #000000; font-weight: bold;">|</span>Flash did not fire, auto mode.                            
Focal Length        <span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000;">4.9</span> mm                                                    
FlashPixVersion     <span style="color: #000000; font-weight: bold;">|</span>FlashPix Version <span style="color: #000000;">1.0</span>                                      
Color Space         <span style="color: #000000; font-weight: bold;">|</span>sRGB                                                      
PixelXDimension     <span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000;">2048</span>                                                      
PixelYDimension     <span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000;">1536</span>                                                      
Custom Rendered     <span style="color: #000000; font-weight: bold;">|</span>Normal process                                            
Exposure Mode       <span style="color: #000000; font-weight: bold;">|</span>Auto exposure                                             
White Balance       <span style="color: #000000; font-weight: bold;">|</span>Auto white balance                                        
Digital Zoom Ratio  <span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000;">1.00</span>                                                      
Scene Capture Type  <span style="color: #000000; font-weight: bold;">|</span>Standard                                                  
GPS tag version     <span style="color: #000000; font-weight: bold;">|</span>0x02, 0x02, 0x00, 0x00                                    
North or South Latit<span style="color: #000000; font-weight: bold;">|</span>N                                                         
Latitude            <span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000;">42.00</span>, <span style="color: #000000;">39.00</span>, <span style="color: #000000;">24.67</span>                                       
East or West Longitu<span style="color: #000000; font-weight: bold;">|</span>E                                                         
Longitude           <span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000;">23.00</span>, <span style="color: #000000;">21.00</span>, <span style="color: #000000;">28.45</span>                                       
Altitude reference  <span style="color: #000000; font-weight: bold;">|</span>0x00                                                      
Altitude            <span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000;">625.50</span></pre></div></div>

<p>While for video files (iPhone) exif cannot help you, this is possible through <a title="Exiftool" href="http://www.sno.phy.queensu.ca/~phil/exiftool/" target="_blank">Exiftool</a>. With it you can read even iPhone video geotags:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>exiftool filname.mov</pre></div></div>

<p>In addition this information can be read with PHP via <a title="read_exif_data" href="http://php.net/manual/en/function.read-exif-data.php" target="_blank">exif_read_data</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$info</span> <span style="color: #339933;">=</span> <span style="color: #990000;">exif_read_data</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'filename.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$info</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And this will give you not only standard tags, but geo tags too.</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/01/27/few-thoughts-on-web-video/' rel='bookmark' title='Few Thoughts on Web Video'>Few Thoughts on Web Video</a></li>
<li><a href='http://www.stoimen.com/blog/2010/03/05/video-sites-must-use-mp4-and-only-mp4/' rel='bookmark' title='Video sites must use &#8230; mp4 and only mp4!'>Video sites must use &#8230; mp4 and only mp4!</a></li>
<li><a href='http://www.stoimen.com/blog/2010/05/25/download-files-with-zend-framework/' rel='bookmark' title='Download Files with Zend Framework'>Download Files with Zend Framework</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/02/04/reading-gps-latitude-and-longitude-from-image-and-video-files/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Few Thoughts on Web Video</title>
		<link>http://www.stoimen.com/blog/2011/01/27/few-thoughts-on-web-video/</link>
		<comments>http://www.stoimen.com/blog/2011/01/27/few-thoughts-on-web-video/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 09:19:00 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Adobe Flash]]></category>
		<category><![CDATA[Application software]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Container formats]]></category>
		<category><![CDATA[FFmpeg]]></category>
		<category><![CDATA[Flash Video]]></category>
		<category><![CDATA[Google Inc.]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[M&A]]></category>
		<category><![CDATA[MPEG-4]]></category>
		<category><![CDATA[Ogg]]></category>
		<category><![CDATA[On2 Technologies]]></category>
		<category><![CDATA[On2 Technologies Inc]]></category>
		<category><![CDATA[player]]></category>
		<category><![CDATA[Plex]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Theora]]></category>
		<category><![CDATA[Video codecs]]></category>
		<category><![CDATA[VP8]]></category>
		<category><![CDATA[web video field]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2145</guid>
		<description><![CDATA[On Air It&#8217;s really full of video sharing websites out there, but in fact almost all of them use Flash player to display their video files. This is the reality now, but with the coming of HTML5, perhaps the things &#8230; <a href="http://www.stoimen.com/blog/2011/01/27/few-thoughts-on-web-video/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2010/03/05/video-sites-must-use-mp4-and-only-mp4/' rel='bookmark' title='Video sites must use &#8230; mp4 and only mp4!'>Video sites must use &#8230; mp4 and only mp4!</a></li>
<li><a href='http://www.stoimen.com/blog/2010/03/04/does-firefox-play-mp4-h-264-within-the-html5-video-tag/' rel='bookmark' title='Does Firefox play mp4 h.264 within the HTML5 video tag?'>Does Firefox play mp4 h.264 within the HTML5 video tag?</a></li>
<li><a href='http://www.stoimen.com/blog/2010/11/12/how-to-make-mp4-progressive-with-qt-faststart/' rel='bookmark' title='How to Make MP4 Progressive with qt-faststart'>How to Make MP4 Progressive with qt-faststart</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>On Air</h2>
<p><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/01/975857_64336474.jpg"><img src="http://www.stoimen.com/blog/wp-content/uploads/2011/01/975857_64336474.jpg" alt="" title="Few Thoughts on Web Video" width="450" height="368" class="alignleft size-full wp-image-2156" /></a><br />
It&#8217;s really full of video sharing websites out there, but in fact almost all of them use Flash player to display their video files. This is the reality now, but with the coming of HTML5, perhaps the things are changing a bit!</p>
<p>First of all if you start dealing with video sharing platforms the first thing to do maybe is to find a good Flash (.flv) player and to convert all your video content in FLV.</p>
<p>Few things to know with Flash video players:</p>
<ol>
<li>The user can play those videos only after installing the Flash Plugin to his browser;</li>
<li>The video must be encoded either in FLV (or FLash Video) or in MPEG-4 with h.264 codec. Only than the Flash player can play it;</li>
</ol>
<p>The HTML5, which can be described as variety of new &#8220;things&#8221; in the HTML comes with a native &lt;video&gt; tag. Something like the &lt;img&gt; tag where you can just point the source of the image in the src attribute.<span id="more-2145"></span></p>
<h2>First Question &#8211; How Does it Look Like?</h2>
<p>To get more fluent with the &lt;video&gt; tag you must simply thing of it as a &#8220;complex&#8221; &lt;img&gt; tag. You just point to the video and the browser will do the rest. To be more exact let me send you to the <a title="HTML5 Video" href="http://www.w3schools.com/html5/html5_video.asp" target="_blank">w3schools page</a>, but the important thing as they say:</p>
<blockquote><p>Until now, there has never been a standard for showing video on a web  page.</p></blockquote>
<p>However to start from somewhere here&#8217;s a little snippet showing you the basic, but very basic usage of the HTML5 video tag:</p>

<div class="wp_syntax"><div class="code"><pre class="html4stict" style="font-family:monospace;">&lt;video src=&quot;path_to_the_video&quot; controls=&quot;controls&quot;&gt;&lt;/video&gt;</pre></div></div>

<h2>Second Question &#8211; Is Every Video Playable?</h2>
<p>But of course not! There are different browser &#8211; different video formats. Currently there are three formats, supported by the different browsers.</p>
<ol>
<li>H.264</li>
<li>VP8</li>
<li>Theora</li>
</ol>
<p>Note that these are <span style="text-decoration: underline;">video</span> codecs and a video files does not consists of only a video codec, but also of an audio codec. This makes the video file a container of several things. As you may know already H.264 is used with MPEG-4 video format, which results in .mp4 extended files, Theora is used in OGG, which are all those .ogg files out in the web, and finaly VP8 is used in WebM &#8230; or in .webm videos.</p>
<p>First thing to know FLV files (.flv) can also be encoded with H.264. Actually Flash player has a h.264 decoder so this makes it possible to play both .mp4 and .flv files encoded with H.264.</p>
<p>Second thing to notice &#8211; where are those .webm files? I&#8217;ve never seen them!</p>
<h2>Third Question &#8211; What is WebM and VP8?</h2>
<p>Behind <a title="The WebM Project" href="http://www.webmproject.org/" target="_blank">WebM</a> and VP8 is Google. This is a new video format based again on Theora. Actually it pretends to be as good in compressing as H.264, which by the way is meant to be the best. But let me take a look at <a title="The state of the video on the web" href="http://net.tutsplus.com/articles/the-state-of-video-on-the-web/" target="_blank">Nettuts+</a>:</p>
<blockquote><p>VP8, a relatively new codec, is the brainchild of On2 — the same guys  behind Theora. Google acquired On2 in 2010, and opened up all the  underlying patents for the codec into the public domain.</p>
<p>WebM, the container of choice for most current browsers, utilizes VP8  for compressing its video content and Vorbis for its audio. It produces  content similar in quality to H.264.</p>
<p>It is completely royalty free, now and for the future. On the  downside, though, it has limited hardware decoding support as well as  third party device/mobile support.</p></blockquote>
<h2>Final Question &#8211; How Can I Convert a Video in WebM?</h2>
<p>This answer is given by <a title="FFMPEG and WebM" href="http://paulrouget.com/e/funwithwebm" target="_blank">this article</a>, where as it is said after having the ffmpeg version &gt;= 0.6 you simply can use this command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span> <span style="color: #660033;">-i</span> girltalk.mp4 <span style="color: #660033;">-f</span> webm <span style="color: #660033;">-vcodec</span> libvpx <span style="color: #660033;">-acodec</span> libvorbis <span style="color: #660033;">-aq</span> <span style="color: #000000;">90</span> <span style="color: #660033;">-ac</span> <span style="color: #000000;">2</span> girltalk.webm</pre></div></div>

<h2>Conclusion</h2>
<p>It&#8217;s interesting to see what will happen one day. There is going to be either a harmonious life in the web video field or the codecs/formats will be as much as the browser? Who knows?</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2010/03/05/video-sites-must-use-mp4-and-only-mp4/' rel='bookmark' title='Video sites must use &#8230; mp4 and only mp4!'>Video sites must use &#8230; mp4 and only mp4!</a></li>
<li><a href='http://www.stoimen.com/blog/2010/03/04/does-firefox-play-mp4-h-264-within-the-html5-video-tag/' rel='bookmark' title='Does Firefox play mp4 h.264 within the HTML5 video tag?'>Does Firefox play mp4 h.264 within the HTML5 video tag?</a></li>
<li><a href='http://www.stoimen.com/blog/2010/11/12/how-to-make-mp4-progressive-with-qt-faststart/' rel='bookmark' title='How to Make MP4 Progressive with qt-faststart'>How to Make MP4 Progressive with qt-faststart</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/01/27/few-thoughts-on-web-video/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Google Analytics for Nokia &#8230; and Mobiles</title>
		<link>http://www.stoimen.com/blog/2011/01/21/google-analytics-for-nokia-and-mobiles/</link>
		<comments>http://www.stoimen.com/blog/2011/01/21/google-analytics-for-nokia-and-mobiles/#comments</comments>
		<pubDate>Fri, 21 Jan 2011 10:08:19 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Economy of Finland]]></category>
		<category><![CDATA[Google Analytics]]></category>
		<category><![CDATA[Google Inc.]]></category>
		<category><![CDATA[Nokia]]></category>
		<category><![CDATA[Nokia E71 Smartphone]]></category>
		<category><![CDATA[Ovi]]></category>
		<category><![CDATA[Smartphones]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[tiny web app]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2136</guid>
		<description><![CDATA[Is There an Analytics Interface for Mobile? The last few months I was searching about some kind of mobile interface of Google Analytics for my Nokia e71. However I did not find one?! Please correct me if I&#8217;m wrong. In &#8230; <a href="http://www.stoimen.com/blog/2011/01/21/google-analytics-for-nokia-and-mobiles/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2010/01/18/google-closure-compiler/' rel='bookmark' title='Google Closure Compiler?'>Google Closure Compiler?</a></li>
<li><a href='http://www.stoimen.com/blog/2010/01/20/google-closure-compiler-doesnt-work/' rel='bookmark' title='Google Closure Compiler doesn&#8217;t work?!'>Google Closure Compiler doesn&#8217;t work?!</a></li>
<li><a href='http://www.stoimen.com/blog/2010/03/16/retrieving-youtube-channels-videos-with-zend_gdata_youtube/' rel='bookmark' title='Retrieving YouTube Channel&#8217;s Videos with Zend_Gdata_YouTube'>Retrieving YouTube Channel&#8217;s Videos with Zend_Gdata_YouTube</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>Is There an Analytics Interface for Mobile?</h2>
<p>The last few months I was searching about some kind of mobile interface of Google Analytics for my Nokia e71. However I did not find one?! Please correct me if I&#8217;m wrong.</p>
<p>In the same time I knew there is a GA API, which is free to use, so finally I wrote a tiny web app allowing the reading of basic information about your Google Analytics accounts.<a href="http://www.stoimen.com/blog/wp-content/uploads/2011/01/nokia.jpg"><img class="alignleft size-full wp-image-2149" title="nokia" src="http://www.stoimen.com/blog/wp-content/uploads/2011/01/nokia.jpg" alt="" width="430" height="322" /></a></p>
<p><em>Note that your username and password are <strong>NOT SAVED!</strong> However it&#8217;s up to you to try. </em></p>
<p>An important note is that the API has a limitation in requests so yet again, it&#8217;s possible to be unavailable in case of too many requests.</p>
<h2>Visit with Your Mobile Browser</h2>
<p>this link:</p>
<p><a title="Google Analytics for Nokia and Mobile Phones" href="http://www.stoimen.com/ga/" target="_blank">http://www.stoimen.com/ga/</a></p>
<p>Note that I made it primary about my Nokia, so any screenshots will be welcomed as well.</p>
<p>It is still beta so be careful. In fact any suggestions are welcome. Either for improving the security and error handling as well as new data exports.</p>
<p>There are lots of things to be done, but this is really basic.</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2010/01/18/google-closure-compiler/' rel='bookmark' title='Google Closure Compiler?'>Google Closure Compiler?</a></li>
<li><a href='http://www.stoimen.com/blog/2010/01/20/google-closure-compiler-doesnt-work/' rel='bookmark' title='Google Closure Compiler doesn&#8217;t work?!'>Google Closure Compiler doesn&#8217;t work?!</a></li>
<li><a href='http://www.stoimen.com/blog/2010/03/16/retrieving-youtube-channels-videos-with-zend_gdata_youtube/' rel='bookmark' title='Retrieving YouTube Channel&#8217;s Videos with Zend_Gdata_YouTube'>Retrieving YouTube Channel&#8217;s Videos with Zend_Gdata_YouTube</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/01/21/google-analytics-for-nokia-and-mobiles/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

