<?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>code-diesel &#187; libraries</title>
	<atom:link href="http://www.codediesel.com/category/libraries/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Amazon Advertising API BrowseNodes</title>
		<link>http://www.codediesel.com/libraries/amazon-advertising-api-browsenodes/</link>
		<comments>http://www.codediesel.com/libraries/amazon-advertising-api-browsenodes/#comments</comments>
		<pubDate>Sun, 01 Jan 2012 16:25:35 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[amazon]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2978</guid>
		<description><![CDATA[Amazon uses a hierarchy of nodes to organize its various items. Each node represents a collection of items, such as Harry Potter books, not the items themselves. Browse node IDs are positive integers that uniquely identify product collections, for example, Literature &#038; Fiction: (17), Medicine: (13996), Mystery &#038; Thrillers: (18), Nonfiction: (53), Outdoors &#038; Nature: [...]]]></description>
			<content:encoded><![CDATA[<p>Amazon uses a hierarchy of nodes to organize its various items. Each node represents a collection of items, such as Harry Potter books, not the items themselves. Browse node IDs are positive integers that uniquely identify product collections, for example, Literature &#038; Fiction: (17), Medicine: (13996), Mystery &#038; Thrillers: (18), Nonfiction: (53), Outdoors &#038; Nature: (290060). Amazon uses over 120,000 browse node IDs in the US locale alone.</p>
<p>The BrowseNodes tool automatically gets all the child BrowseNodes and their names. The program recursively traverses the BrowserNode hierarchy and returns all the BrowseNodes and their respective names. You can display the nodes on the console or save it to a CSV file. You can also include the library in your existing projects to process BrowseNodes.<br />
<span id="more-2978"></span></p>
<h4>Library Usage</h4>
<p>The library is easy to use, and requires PHP and CURL installed on your system. The download includes a sample file with which you can immediately test the code. The sample file is shown below.</p>

<div class="wp_codebox"><table><tr id="p297811"><td class="code" id="p2978code11"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-PUBLIC-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-SECRET-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$region</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$region</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Display the BrowseNodes to the console */</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMedia</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;display&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBrowseNodes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1266092011&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Before you begin to use the library, you need to enter your Amazon security credentials as shown above. Also in the &#8216;aws_signed_request.php&#8217; file you will need to enter your Amazon Associate ID, as it is now mandatory. Find the following line in the file and add your Associated ID.</p>

<div class="wp_codebox"><table><tr id="p297812"><td class="code" id="p2978code12"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* aws_signed_request.php */</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;AssociateTag&quot;</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">'YOUR-ASSOCIATES-ID-HERE'</span><span style="color: #339933;">;</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>That is it. Now you are ready to get the BrowserNodes from Amazon. You can display the BrowserNodes on you console/browser or save it directly to a CSV file. For that you have to use the &#8216;setMedia&#8217; method. Both example are shown below.</p>
<h4>Display the BrowseNodes on the console</h4>

<div class="wp_codebox"><table><tr id="p297813"><td class="code" id="p2978code13"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-PUBLIC-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-SECRET-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$region</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$region</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Display the BrowseNodes to the console */</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMedia</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;display&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBrowseNodes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1266092011&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>You enter the parent browsenode in the &#8216;getBrowseNodes&#8217; method, after which the function recursively retrieves all the children nodes. To get started, you can find all the top browsenode values <a href="http://affiliate-program.amazon.com/gp/redirect.html/ref=amb_link_7285822_2?location=http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/BrowseNodeIDs.html&#038;token=948F5A3F73BA8D32EB91AADB246FBC94772F93D8&#038;pf_rd_m=ATVPDKIKX0DER&#038;pf_rd_s=assoc-center-1&#038;pf_rd_r=&#038;pf_rd_t=501&#038;pf_rd_p=&#038;pf_rd_i=assoc_help_t41_a6" rel="nofollow"  target="_blank">here</a>, or you can get it from the Amazon link itself.</p>
<p>The output of the above is shown below. The output is indented so that you can visualize the hierarchy of the nodes.</p>

<div class="wp_codebox"><table><tr id="p297814"><td class="code" id="p2978code14"><pre class="text" style="font-family:monospace;">Television &amp; Video : AV Receivers &amp; Amplifiers : 3213035011
Television &amp; Video : Blu-ray Players &amp; Recorders : 3213025011
      Blu-ray Players &amp; Recorders : Blu-ray Recorders : 3213026011
      Blu-ray Players &amp; Recorders : Blu-ray Players : 352697011
Television &amp; Video : DVD Players &amp; Recorders : 3213027011
      DVD Players &amp; Recorders : DVD Players : 1036922
      DVD Players &amp; Recorders : DVD Recorders : 1036920
Television &amp; Video : DVD-VCR Combos : 886258
Television &amp; Video : HD DVD Players : 352696011
Television &amp; Video : Televisions : 172659
Television &amp; Video : Home Theater Systems : 281056
.
.</pre></td></tr></table></div>

<p>The format of each line is given below.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/12/browsenode.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/12/browsenode.gif" alt="" title="amazon advertising browsenode" width="436" height="88" class="aligncenter size-full wp-image-2958" /></a></p>
<h4>Saving the BrowseNodes to a CSV</h4>
<p>The following code shows how to save the browsenodes to a CSV file.</p>

<div class="wp_codebox"><table><tr id="p297815"><td class="code" id="p2978code15"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-PUBLIC-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-SECRET-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$region</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$region</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Write the BrowseNodes to a CSV file */</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMedia</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;csv&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;./nodes.csv&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBrowseNodes</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1266092011&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The code saves all the browsenodes in a CSV file, after which you can use it with Excel or a simple text editor. A sample CSV ouput is shown below. The format is the same as the one given above.</p>

<div class="wp_codebox"><table><tr id="p297816"><td class="code" id="p2978code16"><pre class="text" style="font-family:monospace;">&quot;PlayStation 3&quot;,&quot;All Games&quot;,&quot;14210861&quot;
&quot;PlayStation 3&quot;,&quot;Action&quot;,&quot;14210761&quot;
&quot;Action&quot;,&quot;Fighting&quot;,&quot;14210981&quot;
&quot;Action&quot;,&quot;Military &amp; Espionage&quot;,&quot;14210821&quot;
&quot;Action&quot;,&quot;Shooter&quot;,&quot;14210841&quot;
&quot;PlayStation 3&quot;,&quot;Adventure&quot;,&quot;14210851&quot;
&quot;PlayStation 3&quot;,&quot;Arcade&quot;,&quot;14210911&quot;
&quot;PlayStation 3&quot;,&quot;Board Games&quot;,&quot;14210921&quot;</pre></td></tr></table></div>

<h4>Changing Locales</h4>
<p>Amazon being a world-wide venture the Product Advertising API operates in the following locales:<br />
    CA<br />
    CN<br />
    DE<br />
    ES<br />
    FR<br />
    IT<br />
    JP<br />
    UK<br />
    US</p>
<p>You can use different locale code to get the BrowseNodes for different regions. You set the locale parameter using the &#8216;$region&#8217; variable; for example:</p>

<div class="wp_codebox"><table><tr id="p297817"><td class="code" id="p2978code17"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$region</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span></pre></td></tr></table></div>

<h4>Getting the parent node details</h4>
<p>You can also get information about a nodes parent node using the &#8216;getParentNode&#8217; method.</p>

<div class="wp_codebox"><table><tr id="p297818"><td class="code" id="p2978code18"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-PUBLIC-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-SECRET-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$region</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$region</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParentNode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;14210821&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>One other useful method you can use it &#8216;getNodeName&#8217;. This allows you to get the name of a given node id. This can be useful if you have a nodeid but need to gets it name.</p>

<div class="wp_codebox"><table><tr id="p297819"><td class="code" id="p2978code19"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$public_key</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-PUBLIC-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$private_key</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;YOUR-AMAZON-SECRET-KEY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$region</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com&quot;</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// or &quot;CA&quot; or &quot;DE&quot; etc.</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AmazonProductAPI<span style="color: #009900;">&#40;</span><span style="color: #000088;">$public_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$private_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$region</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNodeName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;14210821&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Note</strong>: Some categories contain hundreds to thousands of BrowserNodes, retreiving them can take some time and could trigger a PHP timeout error. To avoid this set PHP timeout to a large number as given below.</p>

<div class="wp_codebox"><table><tr id="p297820"><td class="code" id="p2978code20"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #990000;">set_time_limit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #009933; font-style: italic;">/* Example usage of the Amazon Product Advertising API */</span>
<span style="color: #000000; font-weight: bold;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;amazon_api_class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<div  class="download2">
<a href="http://www.codediesel.com/downloads/amazon-bnodes" rel="nofollow" >Download code files</a><br />
<span>Downloads : 172  / File size : 3.1 kB</span>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/libraries/amazon-advertising-api-browsenodes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Functional style programming with Underscore.php</title>
		<link>http://www.codediesel.com/php/functional-programming-with-underscore-php/</link>
		<comments>http://www.codediesel.com/php/functional-programming-with-underscore-php/#comments</comments>
		<pubDate>Tue, 13 Sep 2011 04:11:00 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[functional]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2909</guid>
		<description><![CDATA[Underscore.php is a PHP port of the popular Underscore.js library. Underscore.php provides a utility library for PHP that provides a lot of the functional programming support that a programmer would expect in Ruby, but without adding much overhead during execution. The only caveat is that underscore.php requires PHP 5.3 or greater. Although you could accomplish [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://brianhaveri.github.com/Underscore.php/" rel="nofollow"  title="underscore.php" target="_blank">Underscore.php</a> is a PHP port of the popular <a href="http://documentcloud.github.com/underscore/" rel="nofollow"  title="underscore.js" target="_blank">Underscore.js</a> library. Underscore.php provides a utility library for PHP that provides a lot of the functional programming support that a programmer would expect in Ruby, but without adding much overhead during execution. The only caveat is that underscore.php requires PHP 5.3 or greater. Although you could accomplish some of the things using PHP&#8217;s built in functions, the functional style approach looks intuitive and easy to work with. Note that this not a purely functional programming like Haskell. It would be nice to integrate the library in your CakePHP framework, which will help bring some functional flavor to the framework.</p>
<p>Take a quick example of the &#8216;pluck&#8217; method.</p>

<div class="wp_codebox"><table><tr id="p290930"><td class="code" id="p2909code30"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'underscore.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$members</span><span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'bill'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'age'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'m'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'john'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'age'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'m'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'sarah'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'age'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'f'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> __<span style="color: #339933;">::</span><span style="color: #004000;">pluck</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$members</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ret</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><span id="more-2909"></span><br />
returns&#8230;</p>

<div class="wp_codebox"><table><tr id="p290931"><td class="code" id="p2909code31"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> bill
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> john
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> sarah
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Here is another using the &#8216;map&#8217; method.</p>

<div class="wp_codebox"><table><tr id="p290932"><td class="code" id="p2909code32"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> __<span style="color: #339933;">::</span><span style="color: #004000;">map</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$num</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ret</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>returns&#8230;</p>

<div class="wp_codebox"><table><tr id="p290933"><td class="code" id="p2909code33"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">6</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">9</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Underscore.php works in both object-oriented and static styles. The following lines give the examples of both.</p>

<div class="wp_codebox"><table><tr id="p290934"><td class="code" id="p2909code34"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$members</span><span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'bill'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'age'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'m'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'john'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'age'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'m'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'sarah'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'age'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'f'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Static style */</span>
<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> __<span style="color: #339933;">::</span><span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$members</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$member</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$member</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'age'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Object Oriented style */</span>
<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> __<span style="color: #009900;">&#40;</span><span style="color: #000088;">$members</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$member</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$member</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'age'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ret</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>returns&#8230;</p>

<div class="wp_codebox"><table><tr id="p290935"><td class="code" id="p2909code35"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> sarah
    <span style="color: #009900;">&#91;</span>age<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">60</span>
    <span style="color: #009900;">&#91;</span>gender<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> f
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Another example using the &#8216;template&#8217; method.</p>

<div class="wp_codebox"><table><tr id="p290936"><td class="code" id="p2909code36"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$members</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'bill'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'age'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'m'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'john'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'age'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'m'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'sarah'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'age'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'f'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$template</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;% __::each($members, function($member) { 
             %&gt; &lt;li&gt;&lt;%= $member[&quot;name&quot;] %&gt;&lt;/li&gt; &lt;% }); %&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> __<span style="color: #339933;">::</span><span style="color: #004000;">template</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$template</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'members'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$members</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ret</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>returns&#8230;</p>

<div class="wp_codebox"><table><tr id="p290937"><td class="code" id="p2909code37"><pre class="html" style="font-family:monospace;">&lt;li&gt;bill&lt;/li&gt;  
&lt;li&gt;john&lt;/li&gt;  
&lt;li&gt;sarah&lt;/li&gt;</pre></td></tr></table></div>

<h4>How does it work</h4>
<p>The underscore.php files defines a &#8216;__&#8217; class which encapsulates all the functional methods and uses the functional programming ideas introduced in PHP 5.3.</p>

<div class="wp_codebox"><table><tr id="p290938"><td class="code" id="p2909code38"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">// Underscore.php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> __ <span style="color: #009900;">&#123;</span>
  <span style="color: #009933; font-style: italic;">// Return an array of values by mapping each item through the iterator</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> map<span style="color: #009900;">&#40;</span><span style="color: #000088;">$collection</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$iterator</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #339933;">...</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #339933;">.</span>
  <span style="color: #339933;">.</span>
  <span style="color: #009933; font-style: italic;">/* other methods */</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Presently the library supports the following methods.</p>
<p><strong>Collections</strong><br />
each, map, reduce, reduceRight, detect, select, reject, all, any, includ, invoke, pluck, max, min, groupBy, sortBy, sortedIndex, toArray, size</p>
<p><strong>Arrays</strong><br />
first, rest, last, compact, flatten, without, uniq, union, intersection, difference, zip, indexOf, lastIndexOf, range</p>
<p><strong>Functions</strong><br />
memoize, throttle, once, after, wrap, compose</p>
<p><strong>Objects</strong><br />
keys, values, functions, extend, defaults, clon, tap, isEqual, isEmpty, isObject, isArray, isFunction, isString, isNumber, isBoolean, isDate, isNaN, isNull</p>
<p><strong>Utility</strong><br />
identity, times, mixin, uniqueId, template</p>
<p><strong>Chaining</strong><br />
chain, value </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/functional-programming-with-underscore-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OPA : new generation of web development platform</title>
		<link>http://www.codediesel.com/software/opa-new-generation-of-web-development-platform/</link>
		<comments>http://www.codediesel.com/software/opa-new-generation-of-web-development-platform/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 08:23:56 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[opa]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2878</guid>
		<description><![CDATA[Opa is a new generation of web development platform &#8211; it is a new programming language, a new web server, a new database and a new distributed execution engine, all of them tightly integrated to provide a great experience for web developers. OPA is concise, simple and readable, it is also provides concurrent, dynamically distributed, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codediesel.com/wp-content/uploads/2011/08/banner-cloud-language.png"><img src="http://www.codediesel.com/wp-content/uploads/2011/08/banner-cloud-language.png" alt="" title="opa-language-cloud-language" width="108" height="94" class="alignleft size-full wp-image-2880" /></a>Opa is a new generation of web development platform &#8211; it is a new programming language, a new web server, a new database and a new distributed execution engine, all of them tightly integrated to provide a great experience for web developers.</p>
<p><a href="http://opalang.org/" rel="nofollow" target="_blank" >OPA </a>is concise, simple and readable, it is also provides concurrent, dynamically distributed, and extremely secure programming model. Opa is designed to make web development fun, secure and right. Opa is a complete language and not just a framework, the unified approach of Opa delivers a new level of developing experience with unmatched ease of use and security.<br />
<span id="more-2878"></span><br />
While Opa offers all the features that developers have come to expect from a high-end web server, a high-end distributed database management system, frameworks or distribution engine, Opa does not resemble Apache or Tomcat, MySQL or Oracle SQL, Drupal or Ruby on Rails, Google AppEngine or Hadoop.</p>
<p>Developing with Opa is as simple as it gets: first, you write your code with the Opa programming language — and only the Opa programming language, no JavaScript, no SQL, or anything such (this is something new, will take sometime getting used to). Then compile the code, and let Opa analyze security, generate the code for the browser, for the server, for the database, and combine it with the web server. Finally, launch your executable and your browser. No need to independently deploy, administer or maintain a database management system, a web server, frameworks or middleware. And, of course, the application works on any recent browser, whether mobile or desktop. Oh yes, and it is right away “Cloud-ready”.</p>
<p>Let us take a sneak peek preview and take a look at the simplest application: Hello, web.</p>

<div class="wp_codebox"><table><tr id="p287843"><td class="code" id="p2878code43"><pre class="php" style="font-family:monospace;">server <span style="color: #339933;">=</span> one_page_server<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">-&gt;</span> <span style="color: #339933;">&lt;&gt;</span>Hello<span style="color: #339933;">,</span> web<span style="color: #339933;">!&lt;/&gt;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>This is the full source code of the application. Without any setup (besides the installation of the compiler), obtaining a complete, deployable application is only a matter of one step:</p>

<div class="wp_codebox"><table><tr id="p287844"><td class="code" id="p2878code44"><pre class="php" style="font-family:monospace;">opa hello_web<span style="color: #339933;">.</span>opa</pre></td></tr></table></div>

<p>This command produces a stand-alone executable binary, containing everything required for execution, including database management, compiled requests, server code, client code, user interface and everything that may be needed to connect them or to handle malicious connections or inputs. Opa generated and checked all this code for you. You may now launch the server:</p>

<div class="wp_codebox"><table><tr id="p287845"><td class="code" id="p2878code45"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">./</span>hello_web<span style="color: #339933;">.</span>exe</pre></td></tr></table></div>

<p>Or, if you prefer, you may deploy it to a cloud:</p>

<div class="wp_codebox"><table><tr id="p287846"><td class="code" id="p2878code46"><pre class="php" style="font-family:monospace;">opa<span style="color: #339933;">-</span>cloud hello_web<span style="color: #339933;">.</span>exe <span style="color: #339933;">--</span>host localhost <span style="color: #339933;">--</span>host my<span style="color: #339933;">@</span>server1 <span style="color: #339933;">--</span>host my<span style="color: #339933;">@</span>server2</pre></td></tr></table></div>

<p>Your application is launched, load-balanced, and servers share whichever information is necessary.</p>
<p>Although I&#8217;ve yet to develop an OPA application of more than average complexity to gauge the actual benefits of it; it looks promising and also something new to try. The OPA <a href="http://opalang.org/resources/doc/index.html#crypto.opa.html/!/value_stdlib.crypto" rel="nofollow" target="_blank" >API</a> also looks quite complete and well documented.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/software/opa-new-generation-of-web-development-platform/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Infinite Wall of images using Mootools</title>
		<link>http://www.codediesel.com/javascript/infinite-wall-of-images-using-mootools/</link>
		<comments>http://www.codediesel.com/javascript/infinite-wall-of-images-using-mootools/#comments</comments>
		<pubDate>Sun, 17 Jul 2011 12:33:12 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[graphics]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[libraries]]></category>
		<category><![CDATA[visualization]]></category>
		<category><![CDATA[images]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2852</guid>
		<description><![CDATA[JavaScript Photo galleries are dime-a-dozen, with a large number of variations in size and features. But this particular one developed using Mootools is a breath of fresh air. Most galleries are spatially bounded, i.e. they have a limit how how much you can scroll in any direction. The Wall &#8211; is a Photo Gallery which [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codediesel.com/wp-content/uploads/2011/07/thumbnailExamples1.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2011/07/thumbnailExamples1.jpg" alt="" title="the wall" width="155" height="131" class="alignleft size-full wp-image-2855" /></a>JavaScript Photo galleries are dime-a-dozen, with a large number of variations in size and features.  But this particular one developed using Mootools is a breath of fresh air. Most galleries are spatially bounded, i.e. they have a limit how how much you can scroll in any direction.<br />
<span id="more-2852"></span><br />
The  Wall &#8211; is a Photo Gallery which can cover your entire screen with images.  It provides a spatially infinite plane to display images, video or other media. Imagine a infinite grid, in the cells of which you can put images. You can scroll the photos in any direction &#8211; vertical, horizontal, diagonal and reverse too. You can insert any number of images in the grid, which are repeated over the entire plane to give a illusion of an infinite wall.</p>
<p>Also, it’s not just for images, it also can hold videos and text and it can be used as an endless grid or also as the popular Coda Slider.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/07/the_wall1.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2011/07/the_wall1.jpg" alt="" title="the_wall" width="554" height="331" class="aligncenter size-full wp-image-2857" /></a></p>
<p>The plugin also provides many interesting variations on the basic theme as shown below.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/07/the_wall_options1.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2011/07/the_wall_options1.jpg" alt="the wall options" title="the_wall_options" width="550" height="318" class="aligncenter size-full wp-image-2859" /></a></p>
<p>Demos : <a href="http://wall.plasm.it/examples/" rel="nofollow" target="_blank" >http://wall.plasm.it/examples/</a><br />
URL : <a href="http://wall.plasm.it/" rel="nofollow" target="_blank" >http://wall.plasm.it/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/javascript/infinite-wall-of-images-using-mootools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Face detection in images using PHP</title>
		<link>http://www.codediesel.com/algorithms/face-detection-in-images-using-php/</link>
		<comments>http://www.codediesel.com/algorithms/face-detection-in-images-using-php/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 10:41:21 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[libraries]]></category>
		<category><![CDATA[images]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2846</guid>
		<description><![CDATA[In a previous post we saw how to detect human faces in images using JavaScript. In the current post we will look into a PHP port of a JavaScript face detection algorithm without using openCV. Since the code is completed written in PHP it cannot attain the raw speed that C,C++ provides, but for many [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://www.codediesel.com/javascript/face-detection-using-jquery/"target="_blank" >previous post</a> we saw how to detect human faces in images using JavaScript.  In the current post we will look into a PHP port of a JavaScript face detection algorithm without using openCV. Since the code is completed written in PHP it cannot attain the raw speed that C,C++ provides, but for many trivial application a PHP version can do.<br />
<span id="more-2846"></span></p>
<h4>Installing</h4>
<p>Before reading further you can download the code below. The original JavaScript port is located <a href="https://github.com/mauricesvay/php-facedetection" rel="nofollow" target="_blank" >here</a>, the following includes the complete code with a <em>crop</em> method added along with a few example files. As the code relies on the GD library, make sure that it is installed and activated on your system.</p>
<div  class="download2">
<a href="http://www.codediesel.com/downloads/face-detect" rel="nofollow" >Download Source</a><br />
<span>Downloads : 1480  / File size : 499.3 kB</span>
</div>
<h4>Running our sample code</h4>
<p>The sample code to show the detected face is show below. Note that in images having multiple faces only the first one is detected. Although this might seem like a limitation, this can be useful in web applications that need to create a user profile picture from a raw image. So that if a user uploads a full length picture of him/her, the given code can detect the face and automatically create a profile picture for the user.</p>

<div class="wp_codebox"><table><tr id="p284651"><td class="code" id="p2846code51"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">&quot;FaceDetector.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$face_detect</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Face_Detector<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'detection.dat'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$face_detect</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face_detect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sample-image3.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$face_detect</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toJpeg</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><a href="http://www.codediesel.com/wp-content/uploads/2011/07/face-detect-sample.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2011/07/face-detect-sample.jpg" alt="" title="face-detect-sample" width="253" height="392" class="aligncenter size-full wp-image-2849" /></a><br />
<em>Flickr :By modelxing</em></p>
<p>The above code displays a border around the detected face, instead we can directly crop the resulting face area and output the resulting image.</p>

<div class="wp_codebox"><table><tr id="p284652"><td class="code" id="p2846code52"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">&quot;FaceDetector.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$face_detect</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Face_Detector<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'detection.dat'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$face_detect</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face_detect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sample-image3.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$face_detect</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cropFace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Face coordinates</h4>
<p>If you need the coordinates of the detected face for further processing, you can use the following.</p>

<div class="wp_codebox"><table><tr id="p284653"><td class="code" id="p2846code53"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">// Return coordinates in JSON</span>
<span style="color: #009933; font-style: italic;">// {'x':56.375, 'y':45.1, 'w':227.55}</span>
<span style="color: #000088;">$face_detect</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toJson</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// OR</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Return coordinates as an Array</span>
<span style="color: #000088;">$face_detect</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>Extending the class</h4>
<p>You can extend the Face_Detector class using your own methods. For example I&#8217;ve added three methods &#8211; <strong>Rotate</strong>(), <strong>resizeFace</strong>() and <strong>toGrayScale</strong>() to the class, which lets you modify the final face area further.</p>

<div class="wp_codebox"><table><tr id="p284654"><td class="code" id="p2846code54"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">include</span> <span style="color: #0000ff;">&quot;FaceDetector.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* We now extend the above class so we can add our own methods */</span>
<span style="color: #000000; font-weight: bold;">class</span> FaceModify <span style="color: #000000; font-weight: bold;">extends</span> Face_Detector <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Rotate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$canvas</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">imagecopy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">canvas</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'x'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> 
              <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'x'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$canvas</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagerotate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">180</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_outImage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> toGrayScale<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$canvas</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">imagecopy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">canvas</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'x'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> 
              <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'x'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">imagefilter</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #339933;">,</span> IMG_FILTER_GRAYSCALE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_outImage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> resizeFace<span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$canvas</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">imagecopyresized</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">canvas</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'x'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> 
                     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'y'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #339933;">,</span> 
                     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_outImage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _outImage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: image/jpeg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">imagejpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$canvas</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/* Using the extended class */</span>
<span style="color: #000088;">$face_detect</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FaceModify<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'detection.dat'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$face_detect</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">face_detect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sample-image3.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$face_detect</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resizeFace</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>NB: If for some reason the code returns a error, turn off error reporting in php.ini</strong></p>
<h4>Further Links</h4>
<p><strong>1. </strong><a href="http://corpocrat.com/2009/08/18/automatic-face-detection-with-php-in-linux/" rel="nofollow" target="_blank" >Automatic Face Detection in Photos with PHP</a><br />
<strong>2. </strong><a href="http://www.facedetection.com/" rel="nofollow" target="_blank" >http://www.facedetection.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/algorithms/face-detection-in-images-using-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Taking screenshots of websites in PHP</title>
		<link>http://www.codediesel.com/php/taking-screenshots-of-websites-in-php/</link>
		<comments>http://www.codediesel.com/php/taking-screenshots-of-websites-in-php/#comments</comments>
		<pubDate>Thu, 16 Jun 2011 04:40:48 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[images]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2835</guid>
		<description><![CDATA[Taking screenshos of websites is not a frequent requirement for developers but can come handy on many occasions. Although there are some nice solutions on the web, a particular one I found very good is wkhtmltoimage. wkhtmltoimage is a simple shell utility which can be used to convert html to images using the webkit rendering [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codediesel.com/wp-content/uploads/2011/06/snapshot2.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/06/snapshot2.gif" alt="" title="snapshot2" width="192" height="163" class="alignleft size-full wp-image-2838" /></a>Taking screenshos of websites is not a frequent requirement for developers but can come handy on many occasions. Although there are some nice solutions on the web, a particular one  I found very good is <a href="http://code.google.com/p/wkhtmltopdf/" rel="nofollow" target="_blank" >wkhtmltoimage</a>.  </p>
<p>wkhtmltoimage is a simple shell utility which can be used to convert html to images using the <a href="http://www.webkit.org/" rel="nofollow" target="_blank" >webkit </a>rendering engine, and <a href="http://qt.nokia.com/" rel="nofollow" target="_blank" >qt</a>.<br />
<span id="more-2835"></span></p>
<h4>Installation</h4>
<p>To get started we need to first download and install the shell program <a href="http://code.google.com/p/wkhtmltopdf/" rel="nofollow" target="_blank" >wkhtmltoimage</a>. Select the appropriate binaries for your platform. As I&#8217;m using Ubuntu 10, I downloaded the complied binary &#8216;wkhtmltoimage-0.10.0_rc2-static-i386.tar.bz2&#8242;. Extract it to a appropriate folder and you are ready to go.</p>
<h4>Getting your first snapshot</h4>
<p>The simplest way to get a snapshot of a url is through the following:</p>

<div class="wp_codebox"><table><tr id="p283564"><td class="code" id="p2835code64"><pre class="text" style="font-family:monospace;">wkhtmltoimage http://www.bbc.com bbc.jpg</pre></td></tr></table></div>

<p>This will fetch the www.bbc.com index page and save it as a jpg image. Below are a few  websites rendered using the wkhtmltoimage  tool.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/06/screenshots.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/06/screenshots.gif" alt="" title="screenshots" width="495" height="418" class="aligncenter size-full wp-image-2837" /></a></p>
<h4>Customizing the output</h4>
<p>wkhtmltoimage   comes with a plethora of options, a few are shown below, more options can be found in the <a href="http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltoimage_0.10.0_rc2-doc.html" rel="nofollow" target="_blank" >documentation</a>.</p>
<p>The default output quality of the program is set to &#8217;94&#8242; which can make the size of some images a lot bigger, but you can change it to your liking using the below option.</p>

<div class="wp_codebox"><table><tr id="p283565"><td class="code" id="p2835code65"><pre class="bash" style="font-family:monospace;">wkhtmltoimage <span style="color: #660033;">--quality</span> <span style="color: #000000;">50</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.bbc.com bbc.jpg</pre></td></tr></table></div>

<p>Also by default all the images on a page are rendered in the final image, but you can disable images in the final screen-shot using the following:</p>

<div class="wp_codebox"><table><tr id="p283566"><td class="code" id="p2835code66"><pre class="bash" style="font-family:monospace;">wkhtmltoimage <span style="color: #660033;">--no-images</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.bbc.com bbc.jpg</pre></td></tr></table></div>

<p>You can also set the output height and width of the image (in pixels) as below.</p>

<div class="wp_codebox"><table><tr id="p283567"><td class="code" id="p2835code67"><pre class="bash" style="font-family:monospace;">wkhtmltoimage <span style="color: #660033;">--height</span> <span style="color: #000000;">600</span> <span style="color: #660033;">--width</span> <span style="color: #000000;">1800</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.bbc.com bbc.jpg</pre></td></tr></table></div>

<p>or crop the image to a specified size;</p>

<div class="wp_codebox"><table><tr id="p283568"><td class="code" id="p2835code68"><pre class="bash" style="font-family:monospace;">wkhtmltoimage <span style="color: #660033;">--crop-h</span> <span style="color: #000000;">300</span> <span style="color: #660033;">--crop-w</span> <span style="color: #000000;">300</span> <span style="color: #660033;">--crop-x</span> <span style="color: #000000;">0</span> <span style="color: #660033;">--crop-y</span> <span style="color: #000000;">0</span> 
       www.bbc.com bbc.jpg</pre></td></tr></table></div>

<p>Sometimes the JavaScript on the webpage you are rendering can cause problems during rendering, preventing the program from saving the screenshot or causing a huge delay. In such cases you can ask wkhtmltoimage to not run JavaScript on the page while rendering.</p>

<div class="wp_codebox"><table><tr id="p283569"><td class="code" id="p2835code69"><pre class="bash" style="font-family:monospace;">wkhtmltoimage <span style="color: #660033;">--disable-javascript</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.bbc.com bbc.jpg</pre></td></tr></table></div>

<h4>Using with PHP</h4>
<p>Although this is not a pure PHP solution for taking screen shots, you can wrap the final command in a <strong>shell_exec</strong> function or download the &#8216;snappy&#8217; PHP5 wrapper from below. The original library is located <a href="https://github.com/knplabs/snappy" rel="nofollow" target="_blank" >here</a>, but as the source keeps changing the examples given here do not work, so use the library given below.</p>
<div  class="download2">
<a href="http://www.codediesel.com/downloads/snappy" rel="nofollow" >Download Snappy</a><br />
<span>Downloads : 840  / File size : 4.2 kB</span>
</div>

<div class="wp_codebox"><table><tr id="p283570"><td class="code" id="p2835code70"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">shell_exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./wkhtmltoimage --quality 50 http://www.bbc.com bbc.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Below is a short code using &#8216;snappy&#8217; to take a screen-shot of bbc.com.</p>

<div class="wp_codebox"><table><tr id="p283571"><td class="code" id="p2835code71"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Tested on Ubuntu 10.0.4, requires PHP 5.3  */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">namespace</span> Knplabs\Snappy<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Knplabs/Snappy/Media.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Knplabs/Snappy/Image.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* 'wkhtmltoimage' executable  is located in the current directory */</span>
<span style="color: #000088;">$snap</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Image<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./wkhtmltoimage'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Displays the bbc.com website index page screen-shot in the browser */</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type: image/jpeg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$snap</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">output</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.bbc.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>and with a few options added&#8230;</p>

<div class="wp_codebox"><table><tr id="p283572"><td class="code" id="p2835code72"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Tested on Ubuntu 10.0.4, requires PHP 5.3  */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">namespace</span> Knplabs\Snappy<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Knplabs/Snappy/Media.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Knplabs/Snappy/Image.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'zoom'</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">0.5</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'no-images'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* 'wkhtmltoimage' executable  is located in the current directory */</span>
<span style="color: #000088;">$snap</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Image<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./wkhtmltoimage-i386'</span><span style="color: #339933;">,</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Displays the bbc.com website index page screen-shot in the browser */</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type: image/jpeg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$snap</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">output</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.bbc.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>In my opinion you should play with the shell program first till you get to know the complete options and only than use the &#8216;snappy&#8217; PHP wrapper.</p>
<p>In closing here is the snapshot of my site ,rendered at 50% quality (click to zoom).</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/06/codediesel-snapshot.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2011/06/codediesel-snapshot-89x300.jpg" alt="" title="codediesel-snapshot" width="89" height="300" class="aligncenter size-medium wp-image-2839" /></a></p>
<p>Happy rendering!!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/taking-screenshots-of-websites-in-php/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Detect if a mobile device is accessing your application</title>
		<link>http://www.codediesel.com/php/detect-if-a-mobile-device-is-accessing-your-application/</link>
		<comments>http://www.codediesel.com/php/detect-if-a-mobile-device-is-accessing-your-application/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 05:28:15 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[mobile]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2828</guid>
		<description><![CDATA[With a proliferation of mobile devices, web applications now need to be able to detect if it is being viewed on a mobile platform. The mobile detection library here can help you in these matters. Obviously I&#8217;ve not tested it on all available devices but it worked on a few I&#8217;ve got.Sample code and a [...]]]></description>
			<content:encoded><![CDATA[<p>With a proliferation of mobile devices, web applications now need to be able to detect if it is being viewed on a mobile platform. The mobile <a href="https://github.com/porkaria/ismobile" rel="nofollow" target="_blank" >detection library here</a> can help you in these matters. Obviously I&#8217;ve not tested it on all available devices but it worked on a few I&#8217;ve got.Sample code and a  quick demo are shown below.<br />
<span id="more-2828"></span></p>

<div class="wp_codebox"><table><tr id="p282875"><td class="code" id="p2828code75"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">&quot;ismobile.class.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$ismobile</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> IsMobile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ismobile</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">CheckMobile</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">'Your mobile device is a '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ismobile</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">GetMobileDevice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;It isn't a mobile device!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>View the following link on your mobile device.</p>
<p><a href="http://www.codediesel.com/demos/mobiledetect/"class="view_demo" target="_blank" ></a></p>
<p>Besides the above functions it can also detect if WAP is being used on the mobile.</p>

<div class="wp_codebox"><table><tr id="p282876"><td class="code" id="p2828code76"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$iswap</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ismobile</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">checkMobileWap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/detect-if-a-mobile-device-is-accessing-your-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Build a shopping cart in 20 minutes</title>
		<link>http://www.codediesel.com/php/build-a-shopping-cart-in-20-minutes/</link>
		<comments>http://www.codediesel.com/php/build-a-shopping-cart-in-20-minutes/#comments</comments>
		<pubDate>Tue, 24 May 2011 11:33:44 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[shoppingcart]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2818</guid>
		<description><![CDATA[Shopping carts are one of the most frequently used parts on the web. But for a programmer creating them has never been easy. Even for a few items the programmer has to create a database and other support files to create a functioning shopping cart. EasyBasket takes a different approach at creating a shopping cart; [...]]]></description>
			<content:encoded><![CDATA[<p>Shopping carts are one of the most frequently used parts on the web. But for a programmer creating them has never been easy. Even for a few items the programmer has to create a database and other support files to create a functioning shopping cart. </p>
<p><a href="http://www.easybasket.co.uk/index.html" rel="nofollow" target="_blank" >EasyBasket</a> takes a different approach at creating a shopping cart; although technically complex in its execution, it is extremely simple for the user to install. With EasyBasket you can quickly insert a shopping cart anywhere on your web page with a single line of code. No need of installing a database or a dozen class files. Google Checkout and PayPal Checkout are integrated into the code, all you need to do is change the credentials and your cart is ready to go.<br />
<span id="more-2818"></span><br />
<a href="http://www.easybasket.co.uk/index.html" rel="nofollow" target="_blank" ><img src="http://www.codediesel.com/wp-content/uploads/2011/05/easybasket.gif" alt="" title="easybasket" width="420" height="235" class="aligncenter size-full wp-image-2820" /></a></p>
<h4>How EasyBasket works</h4>
<p>EasyBasket bascially works by manipulating XSL forms. When a item is added on the client page, EasyBasket with the help of jQuery serializes the form and sends it to the server. On the server PHP uses the <strong>core.xsl</strong> file to update and transform the received form and send it back to the client. The complete shopping cart functions &#8211; create/update/add/delete (CRUD) is handled by the XLST logic; this is something original I&#8217;ve seen related to shopping carts.</p>
<p>Note that in all this flow a database is never used. This allows small websites having a few items to quickly add a shopping cart to their site.</p>
<h4>Other options</h4>
<p>EasyBasket supports various skins and also includes drag-drop functionality for adding items.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/build-a-shopping-cart-in-20-minutes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Data filtering and validation using Inspekt</title>
		<link>http://www.codediesel.com/php/data-filtering-and-validation-using-inspekt/</link>
		<comments>http://www.codediesel.com/php/data-filtering-and-validation-using-inspekt/#comments</comments>
		<pubDate>Sat, 26 Mar 2011 04:56:07 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2810</guid>
		<description><![CDATA[Most web security glitches are caused by insufficient input filtering and validation. Despite a large number of validation libraries being available, input sanitization is usually bypassed due to sheer laziness or the idea that your site is somehow immune from bad input data. Inspekt is one such library that has a small footprint and is [...]]]></description>
			<content:encoded><![CDATA[<p>Most web security glitches are caused by insufficient input filtering and validation. Despite a large number of validation libraries being available, input sanitization is usually bypassed due to sheer laziness or the idea that your site is somehow immune from bad input data. <a href="https://github.com/funkatron/inspekt" rel="nofollow" target="_blank" >Inspekt</a> is one such library that has a small footprint and is very easy to integrate into your application.<br />
<span id="more-2810"></span></p>
<h4>The basic principle behind the library</h4>
<p>Every data filtering/validation library hinges on three basic principles.</p>
<p>The first principle is of preventing users from accessing user input via the PHP superglobals, because the &#8220;default&#8221; action is to retrieve the raw, potentially dangerous data.</p>
<p>The second principle is preventing ad-hoc filtering/validation code at various places in the application which can be error-prone and difficult to modify.</p>
<p>The final principle is that of simplicity. A validation library should make a programmer&#8217;s job easier. Complex solutions should be avoided unless they are the only solution, as programmers tend to bypass intricate solutions citing various reasons for the same.</p>
<h4>Installing Inspekt</h4>
<p>Inspekt is easy to install. Just download the library and include the &#8216;Inspekt.php&#8217; in your application with the correct path.</p>

<div class="wp_codebox"><table><tr id="p281085"><td class="code" id="p2810code85"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Inspekt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>Caging data</h4>
<p>Take the following simple code using Inspekt. Here all the POST data is only made available through the &#8216;post_cage&#8217; object, once it is initialized. The original POST data is immediately deleted preventing you from accidentally using data from the $_POST array. In short all the POST data is now safety &#8216;caged&#8217; in the &#8216;post_cage&#8217; object, which can only be accessed by using the various objects functions.</p>

<div class="wp_codebox"><table><tr id="p281086"><td class="code" id="p2810code86"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Inspekt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/* This will print the $_POST data */</span>
    <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Store all the POST data in a 'cage', 
     * this will also automatically delete 
     * all the original POST data.
    */</span>
    <span style="color: #000088;">$post_cage</span> <span style="color: #339933;">=</span> Inspekt<span style="color: #339933;">::</span><span style="color: #004000;">makePostCage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* This will now return an empty array */</span>
    <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>form id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;myform&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;myform&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;POST&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fname&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;testString&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;id&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;500&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><strong>Example 1.</strong></p>
<h4>Accessing &#8216;caged&#8217; data</h4>
<p>Now that we have seen how the POST data is safely stored in a &#8216;cage&#8217;, we will look at how to access and validate the data.</p>
<p>In the above example we have a &#8216;fname&#8217; html field which we can access from the cage as following, which will print the string &#8220;testString&#8221;.</p>

<div class="wp_codebox"><table><tr id="p281087"><td class="code" id="p2810code87"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$post_cage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRaw</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>But wait, what does this buy us, we are just using another object to access the same POST data in a different way. Well this is just one method, which just returns the raw data, the real value comes when we use validation and filtering methods.</p>
<p>Suppose we wanted to make sure that only alpha-numeric characters are allowed in the &#8220;fname&#8221; variable, we can use the following code which will return the value of the variable if the value is alpha-numeric or &#8216;false&#8217; otherwise.</p>

<div class="wp_codebox"><table><tr id="p281088"><td class="code" id="p2810code88"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fname</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$post_cage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">testAlnum</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #009933; font-style: italic;">// Do something here if 'fname' is alphanumeric</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>Again let us say we have to check if the value of the &#8216;id&#8217; field lies between 100 &#8211; 600, we can use the following:</p>

<div class="wp_codebox"><table><tr id="p281089"><td class="code" id="p2810code89"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post_cage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">testBetween</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">600</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #009933; font-style: italic;">// value lies between 100 - 600, do something</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>Beside the above two, the following are some other useful methods:</p>
<p>    * testAlnum (mixed $key)<br />
    * testAlpha (mixed $key)<br />
    * testBetween (mixed $key, mixed $min, mixed $max, [boolean $inc = TRUE])<br />
    * testCcnum (mixed $key, [mixed $type = NULL])<br />
    * testDate (mixed $key)<br />
    * testDigits (mixed $key)<br />
    * testEmail (mixed $key)<br />
    * testFloat (mixed $key)<br />
    * testGreaterThan (mixed $key, [mixed $min = NULL])<br />
    * testHex (mixed $key)<br />
    * testHostname (mixed $key, [integer $allow = ISPK_HOST_ALLOW_ALL])<br />
    * testInt (mixed $key)<br />
    * testIp (mixed $key)<br />
    * testLessThan (mixed $key, [mixed $max = NULL])<br />
    * testOneOf (mixed $key, [ $allowed = NULL])<br />
    * testPhone (mixed $key, [ $country = 'US'])<br />
    * testRegex (mixed $key, [mixed $pattern = NULL])<br />
    * testUri (unknown_type $key)<br />
    * testZip (mixed $key)</p>
<h4>Caging other input data</h4>
<p>In the above examples we have seen how to add the POST data to a &#8216;cage&#8217; using the &#8216;Inspekt::makePostCage&#8217; method. Likewise, we can also use other methods for other input types:</p>
<p>•	Inspekt::makeGetCage()<br />
Returns an Inspekt_Cage for the $_GET array</p>
<p>•	Inspekt::makePostCage()<br />
Returns an Inspekt_Cage for the $_POST array</p>
<p>•	Inspekt::makeCookieCage()<br />
Returns an Inspekt_Cage for the $_COOKIE array</p>
<p>•	Inspekt::makeServerCage()<br />
Returns an Inspekt_Cage for the $_SERVER array</p>
<p>•	Inspekt::makeFilesCage()<br />
Returns an Inspekt_Cage for the $_FILES array</p>
<p>•	Inspekt::makeEnvCage()<br />
Returns an Inspekt_Cage for the $_ENV array</p>
<p>So to &#8216;cage&#8217; the $_GET data we can use the following:</p>

<div class="wp_codebox"><table><tr id="p281090"><td class="code" id="p2810code90"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$get_cage</span> <span style="color: #339933;">=</span> Inspekt<span style="color: #339933;">::</span><span style="color: #004000;">makeGetCage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>Using the Super Cage</h4>
<p>Using a different cage for each different input type can be cumbersome, for this we can use the  &#8216;SuperCage&#8217; method, which encapsulates all the above input data in a single object.</p>

<div class="wp_codebox"><table><tr id="p281091"><td class="code" id="p2810code91"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$super_cage</span> <span style="color: #339933;">=</span> Inspekt<span style="color: #339933;">::</span><span style="color: #004000;">makeSuperCage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Example 1. can be now be coded as below:</p>

<div class="wp_codebox"><table><tr id="p281092"><td class="code" id="p2810code92"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Inspekt.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$super_cage</span> <span style="color: #339933;">=</span> Inspekt<span style="color: #339933;">::</span><span style="color: #004000;">makeSuperCage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$super_cage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">testAlnum</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fname'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$super_cage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">testBetween</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">220</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>form id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;myform&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;myform&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;POST&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fname&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;testString&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;id&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;500&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<h4>In conclusion</h4>
<p>I find Inspekt a quite interesting validation/filtering library. Although the development seems to have stalled at Version 0.4.1, the code is complete and simple to use. This post has only covered a small portion of the library, for more information you can visit the <a href="http://funkatron.com/inspekt/user_docs/" rel="nofollow" target="_blank" >original source</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/data-filtering-and-validation-using-inspekt/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Easy thumbnail generation using PHP</title>
		<link>http://www.codediesel.com/php/easy-thumbnail-generation-using-php/</link>
		<comments>http://www.codediesel.com/php/easy-thumbnail-generation-using-php/#comments</comments>
		<pubDate>Mon, 21 Mar 2011 04:15:49 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[libraries]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[thumbnail]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2806</guid>
		<description><![CDATA[One of the frequent requirements in web design is that of thumbnail generation. Although there are many libraries out there I found PHP Thumb simple and quick enough to implement in a recent project. PHP Thumb is a light-weight image manipulation library aimed at thumbnail generation. It offers the ability to resize, create custom crops, [...]]]></description>
			<content:encoded><![CDATA[<p>One of the frequent requirements in web design is that of thumbnail generation. Although there are many libraries out there I found <a href="https://github.com/masterexploder/PHPThumb" rel="nofollow" target="_blank" >PHP Thumb</a> simple and quick enough to implement in a recent project. PHP Thumb is a light-weight image manipulation library aimed at thumbnail generation. It offers the ability to resize, create custom crops, or square crops from the center, and rotate the image. If the need arises you can also easily add custom functionality to the library through plugins. </p>
<p>phpthumb uses the &#8216;gd&#8217; library to process the images, so you will need to have gd installed on your system. You can use phpinfo() or the following to quickly check for the same:<br />
<span id="more-2806"></span></p>

<div class="wp_codebox"><table><tr id="p2806102"><td class="code" id="p2806code102"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">gd_info</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>If &#8216;gd&#8217; library is not installed the above will return a &#8216;Call to undefined function gd_info()&#8217; error.</p>
<h4>Simple examples</h4>
<p>The following simple code shows how to quickly resize a image.</p>

<div class="wp_codebox"><table><tr id="p2806103"><td class="code" id="p2806code103"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'../ThumbLib.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$thumb</span> <span style="color: #339933;">=</span> PhpThumbFactory<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'images/test.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/* Resize to 100x100 pixels */</span>
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>..rotate a image.</p>

<div class="wp_codebox"><table><tr id="p2806104"><td class="code" id="p2806code104"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'../ThumbLib.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$thumb</span> <span style="color: #339933;">=</span> PhpThumbFactory<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'images/test.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/* Rotate the image  by 180 degrees */</span>
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">rotateImageNDegrees</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">180</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>..crop a image.</p>

<div class="wp_codebox"><table><tr id="p2806105"><td class="code" id="p2806code105"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'../ThumbLib.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$thumb</span> <span style="color: #339933;">=</span> PhpThumbFactory<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'images/test.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/* $startX, $startY, $cropWidth, $cropHeight */</span>
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">crop</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>..resize by a percentage.</p>

<div class="wp_codebox"><table><tr id="p2806106"><td class="code" id="p2806code106"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'../ThumbLib.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$thumb</span> <span style="color: #339933;">=</span> PhpThumbFactory<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'images/test.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/* Percentage can be less than or greater than 100 */</span>
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resizePercent</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&#8230;or save it to a different format. Formats can be any from the following:<br />
&#8216;GIF&#8217;, &#8216;JPG&#8217;, &#8216;PNG&#8217;.</p>

<div class="wp_codebox"><table><tr id="p2806107"><td class="code" id="p2806code107"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'../ThumbLib.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$image_path</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'images/'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$thumb</span> <span style="color: #339933;">=</span> PhpThumbFactory<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_path</span>  <span style="color: #339933;">.</span> <span style="color: #0000ff;">'test.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">300</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">300</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image_path</span>  <span style="color: #339933;">.</span> <span style="color: #0000ff;">'test.png'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>Thumbnail options</h4>
<p>While creating a thumbnail you can pass various options in an array; for example, to scale an image up to the desired dimensions and to set the jpeg quality to 60 we will use the following code.</p>

<div class="wp_codebox"><table><tr id="p2806108"><td class="code" id="p2806code108"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'../ThumbLib.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'resizeUp'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'jpegQuality'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$thumb</span> <span style="color: #339933;">=</span> PhpThumbFactory<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'images/test.jpg'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">300</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">300</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>More options can be found <a href="https://github.com/masterexploder/PHPThumb/wiki/Getting-Started" rel="nofollow" target="_blank" >here</a> and documentation regarding other functions can be found <a href="https://github.com/masterexploder/PHPThumb/wiki/GD-API" rel="nofollow" target="_blank" >here</a>.</p>
<h4>Custom plugins</h4>
<p>phpthumb also lets you create custom plugin for further image processing. A sample plugin for reflection is provided in the download code, a example of which is shown below:</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/03/reflection.jpeg"><img src="http://www.codediesel.com/wp-content/uploads/2011/03/reflection.jpeg" alt="" title="reflection" width="128" height="179" class="aligncenter size-full wp-image-2808" style="border:none;" /></a></p>

<div class="wp_codebox"><table><tr id="p2806109"><td class="code" id="p2806code109"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'../ThumbLib.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$thumb</span> <span style="color: #339933;">=</span> PhpThumbFactory<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'images/test.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/* Params: $percent, $reflection, $white, $border, $borderColor */</span>
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createReflection</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'#a4a4a4'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h4>Chaining functions</h4>
<p>To quickly apply various formatting to the image we can chain various functions as shown below. The following code will crop an image, resize it to 150% of the original size and then add a reflection to the final image.</p>

<div class="wp_codebox"><table><tr id="p2806110"><td class="code" id="p2806code110"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'../ThumbLib.inc.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$thumb</span> <span style="color: #339933;">=</span> PhpThumbFactory<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'images/test.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">crop</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">93</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">58</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">40</span><span style="color: #009900;">&#41;</span>
      <span style="color: #339933;">-&gt;</span><span style="color: #004000;">resizePercent</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">150</span><span style="color: #009900;">&#41;</span>
      <span style="color: #339933;">-&gt;</span><span style="color: #004000;">createReflection</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'#a4a4a4'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$thumb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The output of the above is shown below.<br />
<a href="http://www.codediesel.com/wp-content/uploads/2011/03/chained-process.jpeg"><img src="http://www.codediesel.com/wp-content/uploads/2011/03/chained-process.jpeg" alt="" title="chained-process" width="60" height="84" class="aligncenter size-full wp-image-2809" style="border: none;"  /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/easy-thumbnail-generation-using-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

