<?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>Compiled Thoughts by John Ciacia &#187; PHP</title>
	<atom:link href="http://www.johnciacia.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.johnciacia.com</link>
	<description>Science, Technology, and Beyond</description>
	<lastBuildDate>Fri, 06 Jan 2012 15:46:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Object and Array Casting</title>
		<link>http://www.johnciacia.com/2011/11/29/object-and-array-casting/</link>
		<comments>http://www.johnciacia.com/2011/11/29/object-and-array-casting/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 19:24:16 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[cast]]></category>
		<category><![CDATA[object]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=1355</guid>
		<description><![CDATA[A few days ago, I posted about some interesting behavior when casting arrays to objects. I would like to take a look at some other interesting behavior. Lets start be creating a basic class Test with three properties each with a different visibility. [code]class Test { private $foo; protected $bar; public $bat; public function __construct() [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago, I posted about some interesting behavior when casting <a href="http://www.johnciacia.com/2011/11/08/curly-braces-for-invalid-class-property-name/">arrays to objects</a>. I would like to take a look at some other interesting behavior. Lets start be creating a basic class <strong>Test</strong> with three properties each with a different visibility.</p>
<p>[code]class Test {<br />
  private $foo;<br />
  protected $bar;<br />
  public $bat;</p>
<p>  public function __construct() {<br />
    $this-&gt;foo = &quot;lorem&quot;;<br />
    $this-&gt;bar = &quot;dolor&quot;;<br />
    $this-&gt;bat = &quot;amit&quot;;<br />
  }<br />
}[/code]</p>
<p>Now create a new instance of the class and cast it to an array.<br />
[code]$test = new Test();<br />
print_r((array)$test);[/code]<br />
The result is as follows:<br />
[code]Array<br />
(<br />
    [Testfoo] =&gt; lorem<br />
    [*bar] =&gt; dolor<br />
    [bat] =&gt; amit<br />
)[/code]<br />
On the surface it appears as if the private property had the class name prepended and the and the protected property had an astrick prepended. Now what happens if we cast this array back to an object?<br />
[code]$test_object = new Test();<br />
$test_array = (array)$test_object;<br />
var_dump((object)$test_array);[/code]</p>
<p>Results in:<br />
[code]object(stdClass)#2 (3) {<br />
  [&quot;foo&quot;:&quot;Test&quot;:private]=&gt;<br />
  string(5) &quot;lorem&quot;<br />
  [&quot;bar&quot;:protected]=&gt;<br />
  string(5) &quot;dolor&quot;<br />
  [&quot;bat&quot;]=&gt;<br />
  string(4) &quot;amit&quot;<br />
}[/code]</p>
<p>How did PHP know the <strong>Testfoo</strong> key and the <strong>*bar</strong> key were private and protected? After all, doing the following does not work:<br />
[code]$test_array = array(&quot;Testfoo&quot; =&gt; &quot;lorem&quot;, &quot;*bar&quot; =&gt; &quot;dolor&quot;, &quot;bat&quot; =&gt; &quot;amit&quot;);<br />
var_dump((object)$test_array);[/code]</p>
<p>It turns out, PHP stores meta information with the keys in the form of null bytes. Thus<br />
[code]$test_array = array(<br />
  &quot;\x00Test\x00foo&quot; =&gt; &quot;lorem&quot;,<br />
  &quot;\x00*\x00bar&quot; =&gt; &quot;dolor&quot;,<br />
  &quot;bat&quot; =&gt; &quot;amit&quot;<br />
  );<br />
var_dump((object)$test_array);[/code]<br />
Results in the object we expect.<br />
[code]object(stdClass)#2 (3) {<br />
  [&quot;foo&quot;:&quot;Test&quot;:private]=&gt;<br />
  string(5) &quot;lorem&quot;<br />
  [&quot;bar&quot;:protected]=&gt;<br />
  string(5) &quot;dolor&quot;<br />
  [&quot;bat&quot;]=&gt;<br />
  string(4) &quot;amit&quot;<br />
}[/code]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2011/11/29/object-and-array-casting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Array vs SplFixedArray</title>
		<link>http://www.johnciacia.com/2011/02/01/array-vs-splfixedarray/</link>
		<comments>http://www.johnciacia.com/2011/02/01/array-vs-splfixedarray/#comments</comments>
		<pubDate>Tue, 01 Feb 2011 06:06:14 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[spl]]></category>
		<category><![CDATA[splfixedarray]]></category>
		<category><![CDATA[standard php library]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=814</guid>
		<description><![CDATA[PHP offers eight primitive types to the user. One of them being an array. An interesting aspect of PHP arrays is that they are allocated dynamically. Thus, the following code is perfectly acceptable: [code lang="php"]]]></description>
			<content:encoded><![CDATA[<p>PHP offers eight primitive types to the user. One of them being an array. An interesting aspect of PHP arrays is that they are allocated dynamically. Thus, the following code is perfectly acceptable:</p>
<p>[code lang="php"]<?php<br />
$arr = array(1, 2, 3, 4);<br />
$arr[50] = 50;<br />
[/code]</p>
<p>
The first line set the first four indexes to one through four respectively. This creates an array of size four. The second line sets index fifty to the value fifty augmenting the array size by one. Indexes five through forty nine do not exist, and attempting to access a non existing element will only raise an E_Notice level error message. The dynamic allocation of arrays allows programmers to ignore out of bounds exceptions, segmentation faults, and hours of debugging, but it also leads to a slower write time because the memory locations needed to hold the new data is not already allocated.</p>
<p>The Standard PHP Library offers a <strong><a href="http://www.php.net/manual/en/class.splfixedarray.php">SplFixedArray</a></strong> object that pre-allocates the necessary memory thereby solving the issue of slower write times.</p>
<p>[code lang="php"]<?php</p>
<p>$array = new SplFixedArray(5);<br />
print_r($array);[/code]<br />
Which results in (a pre-allocated array)</p>
<blockquote><p>SplFixedArray Object<br />
(<br />
[0] =&gt;<br />
[1] =&gt;<br />
[2] =&gt;<br />
[3] =&gt;<br />
[4] =&gt;<br />
)</p></blockquote>
<h2>Creation Time</h2>
<p>To verify this, I ran a test that writes data to an array and an SplFixedArray and measures the time for different amounts of elements.</p>
<p>[code lang="php"]<?php</p>
<p>for($size = 1000; $size < 100000; $size *= 2) {<br />
  $f = $size;<br />
  for($t = microtime(true), $a = array(), $i = 0; $i < $size; $i++) {<br />
    $a[$i] = NULL;<br />
  }<br />
  $f .= "," . (microtime(true) - $t) . ",";</p>
<p>  for($t = microtime(true), $a = new SplFixedArray($size), $i = 0; $i < $size; $i++) {<br />
    $a[$i] = NULL;<br />
  }<br />
  $f .= (microtime(true) - $t) . "\n";<br />
  file_put_contents("./data.csv", $f, FILE_APPEND);<br />
}[/code]</p>
<div align="left"><a href="http://www.johnciacia.com/wp-content/uploads/2011/01/1.png"><img class="size-full wp-image-818 aligncenter" title="1" src="http://www.johnciacia.com/wp-content/uploads/2011/01/1.png" alt="" width="540" height="335" /></a></div>
<h2>Write Time</h2>
<p>It is clear that the SplFixedArray has faster a write time. However, there is a significant overhead when an object is created. To test this, I ran the same test but each time I created a new data structure. From the plot, we can see that creating an array has minimal overhead, whereas creating a new SplFixedArray object and allocating the memory takes a significant more amount of time.</p>
<p>[code lang="php"]<?php</p>
<p>$elements = 20;</p>
<p>for($size = 1000; $size &lt; 100000; $size *= 2) {<br />
  $f = $size;<br />
  for($t = microtime(true), $i = 0; $i < $size; $i++) {<br />
    for($j = 0,$a = array(); $j < $elements; $j++ ) {<br />
      $a[$i] = NULL;<br />
    }<br />
  }<br />
  $f .= "," . (microtime(true) - $t) . ",";</p>
<p>  for($t = microtime(true), $i = 0; $i &lt; $size; $i++) {<br />
    for($j = 0, $a = new SplFixedArray($elements); $j < $elements; $j++) {<br />
      $a[$j] = NULL;<br />
    }<br />
  }<br />
  $f .= (microtime(true) - $t) . "\n";     file_put_contents("./data.csv", $f, FILE_APPEND);<br />
}<br />
[/code]</p>
<div align="left"><a href="http://www.johnciacia.com/wp-content/uploads/2011/01/2.png"><img class="alignnone size-full wp-image-820" title="2" src="http://www.johnciacia.com/wp-content/uploads/2011/01/2.png" alt="" width="540" height="336" /></a></div>
<h2>Memory Footprint</h2>
<p>Lastly I wanted to see how the memory usage differs between each data structure.</p>
<p>[code lang="php"]<?php</p>
<p>for($size = 1000; $size < 100000; $size *= 2) {</p>
<p>  $f = $size . ",";</p>
<p>  $t = memory_get_usage();<br />
  for($a = array(), $i = 0; $i < $size; $i++) {<br />
    $a[$i] = NULL;<br />
  }</p>
<p>  $f .= (memory_get_usage() - $t) . ",";</p>
<p>  $t = memory_get_usage();<br />
  for($a = new SplFixedArray($size), $i = 0; $i < $size; $i++) {<br />
    $a[$i] = NULL;<br />
  }</p>
<p>  $f .= ($t - memory_get_usage()) . "\n";</p>
<p>  file_put_contents("./data.csv", $f, FILE_APPEND);<br />
}</p>
<p>[/code]</p>
<div align="left"><a href="http://www.johnciacia.com/wp-content/uploads/2011/01/3.png"><img class="alignnone size-full wp-image-821" title="3" src="http://www.johnciacia.com/wp-content/uploads/2011/01/3.png" alt="" width="540" height="336" /></a></div>
<h2>Conclusion</h2>
<p>The conclusion is one typical of Computer Science, trade-offs. If you are in an environment where you will need a few arrays with predetermined sizes, the SplFixedArray is the best choice. However, if you will be creating numerous arrays, or the size is unknown, then it is probably better to use the native PHP array.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2011/02/01/array-vs-splfixedarray/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Crappy Code: date and mktime</title>
		<link>http://www.johnciacia.com/2011/01/03/crappy-code-date-and-mktime/</link>
		<comments>http://www.johnciacia.com/2011/01/03/crappy-code-date-and-mktime/#comments</comments>
		<pubDate>Mon, 03 Jan 2011 05:44:29 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Crappy Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[crappy code]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[mktime]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=755</guid>
		<description><![CDATA[Occasionally (when creating forms) I will need to create a menu that lists the months. I normally use a predefined array and loop through it echoing out the month. From my experience, this is the best method since everything is defined in a single location that has a constant lookup time. [code lang="PHP"]]]></description>
			<content:encoded><![CDATA[<p>Occasionally (when creating forms) I will need to create a menu that lists the months. I normally use a predefined array and loop through it echoing out the month. From my experience, this is the best method since everything is defined in a single location that has a constant lookup time.</p>
<p>[code lang="PHP"]<br />
<select>
<?php<br />
$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");<br />
for($i = 0; $i < 12; $i++) {<br />
    echo "<br />
<option value='$i'>$months[$i]</option>
<p>";<br />
}<br />
?><br />
</select>
<p>[/code]</p>
<p>Recently, I was asked to help debug the following code:<br />
[code lang="PHP"]<br />
<select>
<?php<br />
for($i = 0; $i < 12; $i++) {<br />
    echo "<br />
<option value='$i'>" . date("F", mktime(0, 0, 0, $i)) . "</option>
<p>";<br />
}<br />
?><br />
</select>
<p>[/code]</p>
<p>After I flamed this guy for making 24 unnecessary function calls, I noted that <em>arguments may be left out in order from right to left; any arguments thus omitted will be set to the current value according to the local date and time.</em> Thus when this function ran on the 31st of the month, mktime&#8217;s 5th argument was 31 which causes an error in the following instance <em>mktime(0, 0, 0, 2, 31)</em> (because February does not have 31 days). The solution was to manually set the 5th parameter to 1.</p>
<p>[code lang="PHP"]<br />
<select>
<?php<br />
for($i = 0; $i < 12; $i++) {<br />
    echo "<br />
<option value='$i'>" . date("F", mktime(0, 0, 0, $i, 1)) . "</option>
<p>";<br />
}<br />
?><br />
</select>
<p>[/code]</p>
<p>This will generate the correct months, but still does not fix the problem of making 24 unnecessary function calls.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2011/01/03/crappy-code-date-and-mktime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>No require with autoload</title>
		<link>http://www.johnciacia.com/2011/01/02/no-require-with-autoload/</link>
		<comments>http://www.johnciacia.com/2011/01/02/no-require-with-autoload/#comments</comments>
		<pubDate>Sun, 02 Jan 2011 21:57:27 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[autoload]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[namespace]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[php6]]></category>
		<category><![CDATA[require]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=734</guid>
		<description><![CDATA[If you have ever worked with a PHP framework you probably have noticed some features that allow you to develop code more rapidly. In this blog, I am going to address autoloading. Traditionally, in object oriented PHP, we create a new source file for each class. If we want to create an object of a [...]]]></description>
			<content:encoded><![CDATA[<p>If you have ever worked with a PHP framework you probably have noticed some features that allow you to develop code more rapidly. In this blog, I am going to address autoloading.</p>
<p>Traditionally, in object oriented PHP, we create a new source file for each class. If we want to create an object of a class, we have to include the php file using require_once (or using a similar function). If we do not include the source file, we will be issued a fatal error.</p>
<p>
<pre><strong>Fatal error</strong>: Class 'User' not found in ...</pre>
</p>
<p>Lets say we have a User class defined as follows in a User.php file</p>
<p>[code lang="PHP"]<?php</p>
<p>class User {<br />
    function __construct() {<br />
        echo "Hello, World!";<br />
    }<br />
}<br />
?>[/code]</p>
<p>If we want to create a new User object we would include user.php and use the new keyword to instantiate the class.</p>
<p>[code lang="PHP"]<?php<br />
require_once('user.php');<br />
$user = new User();[/code]</p>
<p>This is method reasonable for a small number of classes. However, large frameworks can span hundreds of classes, and having to write require_once for each class becomes a huge annoyance. PHP5 introduced an autoload <a href="http://php.net/manual/en/language.oop5.magic.php">magic method</a> which is automatically called if the class source file cannot be found. The __autoload function takes a single parameter - the class being instantiated. If our class name corresponds to its file name we can do something as simple as:</p>
<p>[code lang="PHP"]<?php<br />
function __autoload($class) {<br />
    require_once($class . ".php");<br />
}[/code]</p>
<p>Now, each time you instantiate a class, the autoload function will be automatically ran and it will automagically require your class. This is great, however all classes have to be in the same directory as the callee, and any decent framework uses a class hierarchy and spreads classes out into different directories. When I was writing my framework a few years ago, I stumbled upon a pretty awesome use of the autoload function. I think the idea was borrowed from the Zend framework. In a nutshell, the class names would correspond to their location on the file system. For example, the Zend framework has a class named Zend_Controller_Action_HelperBroker, and the class path Zend/Controller/Action/HelperBroker.php. What does this mean? With some additional code in our autoload function, we can convert the class name into its respective path allowing use to create a class hierarchy on our file system.</p>
<p>[code lang="PHP"]<?php<br />
function __autoload($class) {<br />
    $file = str_replace("_", "/", $class);<br />
    require_once($file . ".php");<br />
}[/code]</p>
<p>Now, with the onset of PHP5.3 and the introduction of namespaces, this method has been kind of depreciated for a more pragmatic method. Instead of naming our classes with really long names that correspond to their path, we can use a namespace instead. Thus our autoload function would look like:</p>
<p>[code lang="PHP"]<?php<br />
function __autoload($class) {<br />
    $file = str_replace("\\", "/", $class);<br />
    require_once($file . ".php");<br />
}[/code]</p>
<p>In my next blog, I will talk more about namespaces and give a better demo on how this is used.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2011/01/02/no-require-with-autoload/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sending Text Messages in PHP</title>
		<link>http://www.johnciacia.com/2010/08/07/sending-text-messages-in-php/</link>
		<comments>http://www.johnciacia.com/2010/08/07/sending-text-messages-in-php/#comments</comments>
		<pubDate>Sat, 07 Aug 2010 17:15:05 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[sms]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=648</guid>
		<description><![CDATA[Recently I&#8217;ve had to implement an alert system in PHP were the alerts are sent via a text message. The concept is very simple &#8211; use an email to SMS gateway. Essentially, you send an email to a phone companies gateway server, that server then relays the email to the phone number specified via a [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve had to implement an alert system in PHP were the alerts are sent via a text message. The concept is very simple &#8211; use an email to SMS gateway. Essentially, you send an email to a phone companies gateway server, that server then relays the email to the phone number specified via a SMS. Since PHP has the ability to send emails, the whole process is quite easy.</p>
<p>1. First you need to acquire a list of SMS gateways you are going to support. For an extensive list, check out Wikipedias <a href="http://en.wikipedia.org/wiki/List_of_carriers_providing_SMS_transit">List of SMS Gateways</a>. For this tutorial, I will only be using AT&#038;T Wireless (number@txt.att.net) and Verison (number@vtext.com).</p>
<p>2. Create a simple HTML form.<br />
[code lang="html"]<!DOCTYPE html><br />
<html><br />
<head><br />
	<meta charset="utf-8"></meta></p>
<p></head></p>
<p><body></p>
<form action="POST">
<p>		<label for="provider">Provider: </label></p>
<select>
<option value="txt.att.net">AT&#038;T Wireless</option>
<option value="vtext.com">Verizon</option>
</select>
<p></p>
<p>		<label for="number">Number: </label></p>
<input type="number" /></p>
<p>		<label for="subject">Subject: </label></p>
<input type="subject" /></p>
<p>		<label for="message">Message: </label></p>
<input type="message" /></p>
<input type="submit" /></form>
<p></body></p>
<p></html><br />
[/code]</p>
<p>3. Add the necessary PHP code to send the email<br />
[code lang="PHP"]<!DOCTYPE html><br />
<html><br />
<head><br />
	<meta charset="utf-8"></meta></p>
<p></head></p>
<p><body><br />
	<?php<br />
		if(!empty($_POST['number'])) {<br />
			mail($_POST['number'] . $_POST['provider'], $_POST['subject'], $_POST['message']);<br />
        }<br />
	?></p>
<form action="POST">
<p>		<label for="provider">Provider: </label></p>
<select>
<option value="@txt.att.net">AT&#038;T Wireless</option>
<option value="@vtext.com">Verizon</option>
</select>
<p></p>
<p>		<label for="number">Number: </label></p>
<input type="number" /></p>
<p>		<label for="subject">Subject: </label></p>
<input type="subject" /></p>
<p>		<label for="message">Message: </label></p>
<input type="message" /></p>
<input type="submit" /></form>
<p></body></p>
<p></html><br />
[/code]</p>
<p>You will want to do some better error checking and validation, but thats it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2010/08/07/sending-text-messages-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Basic OCR</title>
		<link>http://www.johnciacia.com/2010/01/04/basic-ocr/</link>
		<comments>http://www.johnciacia.com/2010/01/04/basic-ocr/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 03:40:11 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[captcha]]></category>
		<category><![CDATA[gd]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=454</guid>
		<description><![CDATA[This is a very basic optical character recognition script written in PHP. This is untested and serves merely a proof of concept. As noted in the comments, adjusting the sample size can improve results, since with a large sample size on a small image there can be many collisions. A database is needed to compare [...]]]></description>
			<content:encoded><![CDATA[<p>This is a very basic optical character recognition script written in PHP. This is untested and serves merely a proof of concept. As noted in the comments, adjusting the sample size can improve results, since with a large sample size on a small image there can be many collisions. A database is needed to compare output results of this script with known values.</p>
<p>[code lang="PHP"]<?php</p>
<p>/* create a test image */<br />
$im = @imagecreate(100, 20) or die("Cannot Initialize new GD image stream");<br />
$background_color = imagecolorallocate($im, 255, 255, 255);<br />
$text_color = imagecolorallocate($im, 0, 0, 0);<br />
imagestring($im, 1, 5, 5,  "Hello, World!", $text_color);</p>
<p>/***<br />
 * Assumptions:<br />
 *   A monochrome image where characters are black<br />
 *   A single character is connected<br />
 *   Characters are disjointed by white space<br />
 */</p>
<p>$width = imagesx($im);<br />
$height = imagesy($im);</p>
<p>/***<br />
 * Notes:<br />
 *   The smaller the sample size the more accurate it will be,<br />
 *   however, it will take longer. Larger images can use a larger<br />
 *   sample size wihtout compromizing much accuracy.<br />
 */<br />
$x_sample = 1;<br />
$y_sample = 1;</p>
<p>$last = 0;<br />
for($i = 0; $i < $width; $i++) {<br />
    $col = array();</p>
<p>    for($j = 0; $j < $height; $j++) {<br />
        $col[$j] = imagecolorat($im, $i, $j);<br />
    }</p>
<p>    if(($current = array_sum($col)) > 0) {<br />
        if($last == 0) {<br />
            $l = 0;<br />
        }</p>
<p>        for($k = 0; $k < $height; $k++) {<br />
            if(($l % $x_sample) == 0) {<br />
                if(($k % $y_sample) == 0) {<br />
                    $sample .= $col[$k];<br />
                }<br />
            }<br />
        }</p>
<p>        $l++;<br />
        $last = $current;<br />
    } else {<br />
        $last = 0;<br />
    }</p>
<p>    if(!empty($sample) &#038;&#038; $last == 0) {<br />
        echo $sample . "\n";<br />
        $sample = "";<br />
    }<br />
}<br />
?>[/code]</p>
<p>Output (each line represents a character)</p>
<div class="quote">00000011111100000000000000001000000000000000000010000000000000000011111100000000<br />
00000000011000000000000000001011000000000000000011010000000000000000010100000000<br />
000000100001000000000000001111110000000000000000000100000000<br />
000000100001000000000000001111110000000000000000000100000000<br />
00000000011000000000000000001001000000000000000010010000000000000000011000000000<br />
000000000001000000000000000001100000000000000000010000000000<br />
00000011111100000000000000000110000000000000000001100000000000000011111100000000<br />
00000000011000000000000000001001000000000000000010010000000000000000011000000000<br />
00000000111100000000000000000100000000000000000010000000000000000000010000000000<br />
000000100001000000000000001111110000000000000000000100000000<br />
00000000011000000000000000001001000000000000000010100000000000000011111100000000<br />
00000001110100000000</div>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2010/01/04/basic-ocr/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using PHP and GD to add border to text</title>
		<link>http://www.johnciacia.com/2010/01/04/using-php-and-gd-to-add-border-to-text/</link>
		<comments>http://www.johnciacia.com/2010/01/04/using-php-and-gd-to-add-border-to-text/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 23:41:03 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[border]]></category>
		<category><![CDATA[gd]]></category>
		<category><![CDATA[image border]]></category>
		<category><![CDATA[imagettf]]></category>
		<category><![CDATA[stroke]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[text border]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=449</guid>
		<description><![CDATA[[code lang="PHP"][/code]]]></description>
			<content:encoded><![CDATA[<p>[code lang="PHP"]<?php<br />
/**<br />
 * Writes the given text with a border into the image using TrueType fonts.<br />
 * @author John Ciacia<br />
 * @param image An image resource<br />
 * @param size The font size<br />
 * @param angle The angle in degrees to rotate the text<br />
 * @param x Upper left corner of the text<br />
 * @param y Lower left corner of the text<br />
 * @param textcolor This is the color of the main text<br />
 * @param strokecolor This is the color of the text border<br />
 * @param fontfile The path to the TrueType font you wish to use<br />
 * @param text The text string in UTF-8 encoding<br />
 * @param px Number of pixels the text border will be<br />
 * @see http://us.php.net/manual/en/function.imagettftext.php<br />
 */<br />
function imagettfstroketext(&#038;$image, $size, $angle, $x, $y, &#038;$textcolor, &#038;$strokecolor, $fontfile, $text, $px) {</p>
<p>    for($c1 = ($x-abs($px)); $c1 <= ($x+abs($px)); $c1++)<br />
        for($c2 = ($y-abs($px)); $c2 <= ($y+abs($px)); $c2++)<br />
            $bg = imagettftext($image, $size, $angle, $c1, $c2, $strokecolor, $fontfile, $text);</p>
<p>   return imagettftext($image, $size, $angle, $x, $y, $textcolor, $fontfile, $text);<br />
} [/code]</p>
<p>[code lang="PHP"]<?php<br />
$img = imagecreatefrompng("/home/john/Desktop/test.png");<br />
$font_color = imagecolorallocate($img, 0, 0, 0);<br />
$stroke_color = imagecolorallocate($img, 255, 0, 0);<br />
imagettfstroketext($img, 10, 0, 10, 50, $font_color, $stroke_color, "abstract.ttf", "Hello, World!", 2);<br />
?>[/code]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2010/01/04/using-php-and-gd-to-add-border-to-text/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Basic CAPTCHA</title>
		<link>http://www.johnciacia.com/2010/01/04/basic-captcha/</link>
		<comments>http://www.johnciacia.com/2010/01/04/basic-captcha/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 23:01:18 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[captcha]]></category>
		<category><![CDATA[gd]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=443</guid>
		<description><![CDATA[Very basic code demonstrating the use of the GD library by creating a simple CAPTCHA. [code lang="PHP"][/code]]]></description>
			<content:encoded><![CDATA[<p>Very basic code demonstrating the use of the GD library by creating a simple CAPTCHA.<br />
[code lang="PHP"]<?php<br />
function randomString($length){<br />
    $pattern = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";<br />
    if(empty($length)){<br />
        $length = "10";<br />
    }</p>
<p>    for($i=0; $i<$length; $i++){<br />
        $string .= $pattern{rand(0,61)};<br />
    }</p>
<p>    return $string;<br />
}</p>
<p>function captcha() {<br />
    header("Content-type: image/png");<br />
    $im = @imagecreate(100, 50) or die("Cannot Initialize new GD image stream");<br />
    $background_color = imagecolorallocate($im, 0, 0, 0);<br />
    $text_color = imagecolorallocate($im, 255, 0, 0);<br />
    $string = randomString();<br />
    imagestring($im, 10, 5, 5,  $string, $text_color);<br />
    imagepng($im);<br />
    imagedestroy($im);<br />
}</p>
<p>captcha();<br />
?>[/code]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2010/01/04/basic-captcha/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Illogical PHP Logic</title>
		<link>http://www.johnciacia.com/2009/07/17/illogical-php-logic/</link>
		<comments>http://www.johnciacia.com/2009/07/17/illogical-php-logic/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 22:50:31 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[logic]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=218</guid>
		<description><![CDATA[In algebra, after we learn the basic distributive, commutative, and associative properties, the transitive property of equality is usually next in the curriculum. For those of you who do not recall the terminology, the transitive property of equality says if $a == $b and $b == $n then $a == $n. Using this age old [...]]]></description>
			<content:encoded><![CDATA[<p>In algebra, after we learn the basic distributive, commutative, and associative properties, the transitive property of equality is usually next in the curriculum. For those of you who do not recall the terminology, the transitive property of equality says if $a == $b and $b == $n then $a == $n. Using this age old logic, you can prove FALSE == TRUE and 0 == 1 in PHP. Here is how:</p>
<p>[code]<br />
$a = 0;<br />
$b = "Hooray for PHP logic and dynamic type casting?";<br />
var_dump(((FALSE == $a) == ($a == $b)) == ($b == TRUE));<br />
var_dump((((0 == $a) == ($a == $b)) == ($b == TRUE) == (TRUE == 1));[/code]<br />
Returns<br />
<code>bool(true)<br />
bool(true)<br />
</code><br />
Q.E.D</p>
<p>Note: I am comparing values not types. That being said, forgive me when I flame you for using PHP inappropriately. A string is NOT an integer and an integer is NOT a boolean. They should not be used as such.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2009/07/17/illogical-php-logic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting up a LAMP server with Ubuntu</title>
		<link>http://www.johnciacia.com/2009/07/05/setting-up-a-lamp-server-with-ubuntu/</link>
		<comments>http://www.johnciacia.com/2009/07/05/setting-up-a-lamp-server-with-ubuntu/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 03:03:16 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[lamp]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=199</guid>
		<description><![CDATA[Ubuntu, known for its ease of use, makes no exception for setting up an apache, mysql, and php stack. Since the release of Feisty Fawn, Ubuntu has come packed with tasksel &#8211; a user interface for installing tasks. 1. At your command prompt, run tasksel as root. john@earth:~$ sudo tasksel 2. Select LAMP server 3. [...]]]></description>
			<content:encoded><![CDATA[<p>Ubuntu, known for its ease of use, makes no exception for setting up an apache, mysql, and php stack. Since the release of Feisty Fawn, Ubuntu has come packed with <strong>tasksel</strong> &#8211; a user interface for installing tasks. </p>
<p>1. At your command prompt, run tasksel as root.</p>
<blockquote><p>john@earth:~$ sudo tasksel</p></blockquote>
<p>2. Select LAMP server<br />
<a href="http://www.johnciacia.com/wp-content/uploads/2009/07/1.png"><img src="http://www.johnciacia.com/wp-content/uploads/2009/07/1-300x198.png" alt="1" title="1" width="300" height="198" class="aligncenter size-medium wp-image-200" /></a></p>
<p>3. Continue the installation by following the prompts.</p>
<p>It works!<br />
<a href="http://www.johnciacia.com/wp-content/uploads/2009/07/4.png"><img src="http://www.johnciacia.com/wp-content/uploads/2009/07/4-300x206.png" alt="4" title="4" width="300" height="206" class="aligncenter size-medium wp-image-201" /></a></p>
<p>One tool I have difficulty living without is phpMyAdmin. From the command prompt type<br />
<blockquote>sudo apt-get install phpmyadmin</p></blockquote>
<p> Continue the installation by following the prompts. If you are using a version of Ubuntu older than 9.04 (Jaunty) you will need to add the following line to /etc/apache2/apache2.conf Continue the installation by following the prompts. Version 9.04 does this automatically. You will be able to access phpMyAdmin by browsing directly to http://<hostname>/phpmyadmin </p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2009/07/05/setting-up-a-lamp-server-with-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: www.johnciacia.com @ 2012-02-05 09:39:19 -->
