<?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>llynix.com &#187; Code</title>
	<atom:link href="http://llynix.com/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://llynix.com</link>
	<description>Code, Rants and Ramblings</description>
	<lastBuildDate>Mon, 15 Feb 2010 00:17:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The One Line Template Engine</title>
		<link>http://llynix.com/code/the-one-line-template-engine/</link>
		<comments>http://llynix.com/code/the-one-line-template-engine/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 00:17:54 +0000</pubDate>
		<dc:creator>Llynix</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://llynix.com/?p=270</guid>
		<description><![CDATA[Bought to you by vincevincevince over at webmasterworld.  
The engine itself:
print preg_replace(&#8220;/\{([^\{]{1,100}?)\}/e&#8221;,&#8221;$$1&#8243;,file_get_contents(&#8220;template.tpl&#8221;));
Format of template.tpl file:

&#60;html&#62;
&#60;head&#62;
&#60;title&#62;{title}&#60;/title&#62;
&#60;/head&#62;
&#60;body&#62;
&#60;h1&#62;{header}&#60;/h1&#62;
{text}
&#60;/body&#62;
&#60;/html&#62;

Setting variables:
$title=&#8221;Example page&#8221;;
$header=&#8221;My Examples&#8221;;
$text=&#8221;See the placeholders replaced?&#8221;;
&#8220;/\{([^\{]{1,100}?)\}/e&#8221;,&#8221;$$1&#8243;
I&#8217;m delimiting the regular expression with / / and using the modifier &#8216;e&#8217; which causes the second argument to be evaluated by php.
Explanation

The pattern looks for an opening curly-brace ( \{ ) &#8211; the [...]]]></description>
			<content:encoded><![CDATA[<p>Bought to you by vincevincevince over at <a href="http://www.webmasterworld.com/php/3444822.htm">webmasterworld</a>.  </p>
<p>The engine itself:<br />
print preg_replace(&#8220;/\{([^\{]{1,100}?)\}/e&#8221;,&#8221;$$1&#8243;,file_get_contents(&#8220;template.tpl&#8221;));</p>
<p>Format of template.tpl file:<br />
<code><br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;{title}&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;h1&gt;{header}&lt;/h1&gt;<br />
{text}<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
</code><br />
Setting variables:<br />
$title=&#8221;Example page&#8221;;<br />
$header=&#8221;My Examples&#8221;;<br />
$text=&#8221;See the placeholders replaced?&#8221;;</p>
<p>&#8220;/\{([^\{]{1,100}?)\}/e&#8221;,&#8221;$$1&#8243;<br />
I&#8217;m delimiting the regular expression with / / and using the modifier &#8216;e&#8217; which causes the second argument to be evaluated by php.</p>
<p>Explanation</p>
<p><quote><br />
The pattern looks for an opening curly-brace ( \{ ) &#8211; the end of the pattern is a closing curly-brace ( \} ).</p>
<p>In between the two braces I look for any character which isn&#8217;t an opening curly-brace [^\{], avoiding mistaken nesting of tags.</p>
<p>I match between 1 and 100 of these non-{ characters by writing {1,100} and then I make the match non-greedy (try to find the shortest strings between { and }, not the longest) by adding a?. (? after *, + or {a,b} expressions changes them to non-greedy &#8211; in other situations? means 0 or 1 of the preceding).</p>
<p>The full string of non-{ characters is matched and stored as string $1 by surrounding that part of the pattern with brackets ().</p>
<p>Finally, the second argument of the preg_replace is &#8220;$$1&#8243;, using variable variables. If the pattern encounters &#8220;{title}&#8221; then the matched string $1 is &#8220;title&#8221; and so $$1 is $title.<br />
</quote></p>
]]></content:encoded>
			<wfw:commentRss>http://llynix.com/code/the-one-line-template-engine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Php Parsing Script</title>
		<link>http://llynix.com/code/php-parsing-script/</link>
		<comments>http://llynix.com/code/php-parsing-script/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 03:49:58 +0000</pubDate>
		<dc:creator>Llynix</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://llynix.com/?p=241</guid>
		<description><![CDATA[I was looking for a way to see how fast my pages were parsing.  Came up with this code.  Please note this only works for php5.
Put this at the beginning of your pages:

&#60;?php $starttime = microtime(1); ?&#62;

Put this at the end of your pages:

&#60;?php
  $endtime = microtime(1);
  $totaltime = $endtime - [...]]]></description>
			<content:encoded><![CDATA[<p>I was looking for a way to see how fast my pages were parsing.  Came up with this code.  Please note this only works for php5.</p>
<p>Put this at the beginning of your pages:<br />
<code><br />
&lt;?php $starttime = microtime(1); ?&gt;<br />
</code><br />
Put this at the end of your pages:<br />
<code><br />
&lt;?php<br />
  $endtime = microtime(1);<br />
  $totaltime = $endtime - $starttime;<br />
?&gt;<br />
&lt;!-- This page was generated in &lt;?php echo $totaltime; ?&gt; seconds. --&gt;<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://llynix.com/code/php-parsing-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sideways Text</title>
		<link>http://llynix.com/articles/sideways-text/</link>
		<comments>http://llynix.com/articles/sideways-text/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 17:01:48 +0000</pubDate>
		<dc:creator>Llynix</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[HTML/CSS]]></category>

		<guid isPermaLink="false">http://llynix.com/?p=227</guid>
		<description><![CDATA[Just upgraded to Firefox 3.5 and I have to test out the goods.
Sideways Text
If your on a cutting edge browser the text should appear sideways.  I had to tweak a bit to get it to display properly, so obviously there are a few bugs or misconceptions.  The code for the curious is:

&#60;div style="height:100px;"&#62;
&#160;&#160;&#60;br/&#62;&#60;br/&#62;
&#160;&#160;&#60;span [...]]]></description>
			<content:encoded><![CDATA[<p>Just upgraded to Firefox 3.5 and I have to test out the goods.</p>
<div style="height:100px;"><br/><br/><span style="-webkit-transform: rotate(-90deg); -moz-transform: rotate(-90deg);">Sideways Text</span></div>
<p>If your on a cutting edge browser the text should appear sideways.  I had to tweak a bit to get it to display properly, so obviously there are a few bugs or misconceptions.  The code for the curious is:</p>
<p><code><br />
&lt;div style="height:100px;"&gt;<br />
&nbsp;&nbsp;&lt;br/&gt;&lt;br/&gt;<br />
&nbsp;&nbsp;&lt;span style="-webkit-transform: rotate(-90deg); -moz-transform: rotate(-90deg);"&gt;Sideways Text&lt;/span&gt;<br />
&lt;/div&gt;<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://llynix.com/articles/sideways-text/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IE8 Compatibility View</title>
		<link>http://llynix.com/code/ie8-compatibility-view/</link>
		<comments>http://llynix.com/code/ie8-compatibility-view/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 21:48:52 +0000</pubDate>
		<dc:creator>Llynix</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[HTML/CSS]]></category>

		<guid isPermaLink="false">http://llynix.com/?p=179</guid>
		<description><![CDATA[The latest incarnation of Internet Explorer 8 adds a new twist to standards compliant web pages.  When viewing a webpage in standards mode the user is presented with a compatibility icon that can be toggled to view the page using the Internet Explorer 7 engine.
However, as web master you have ultimate control.  By [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_181" class="wp-caption aligncenter" style="width: 457px"><a href="http://llynix.com/wp-content/uploads/2009/06/compatibility.jpg"><img src="http://llynix.com/wp-content/uploads/2009/06/compatibility.jpg" alt="Compatibility View" title="compatibility" width="447" height="89" class="size-full wp-image-181" /></a><p class="wp-caption-text">Compatibility View</p></div><br />
The latest incarnation of Internet Explorer 8 adds a new twist to standards compliant web pages.  When viewing a webpage in standards mode the user is presented with a compatibility icon that can be toggled to view the page using the Internet Explorer 7 engine.</p>
<p>However, as web master you have ultimate control.  By using the X-UA-Compatible meta tag in your documents you can choose which mode you want your page rendered in and hide the button from users.</p>
<p><code><br />
&lt;meta http-equiv="X-UA-Compatible" content="IE=Edge" /&gt;<br />
Always uses the latest available rendering engine.<br />
&lt;meta http-equiv="X-UA-Compatible" content="IE=8" /&gt;<br />
Display using the IE8 rendering engine.<br />
&lt;meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /&gt;<br />
Display standards mode documents in IE7 standards mode, quirks mode documents in IE7 quirks mode.<br />
&lt;meta http-equiv="X-UA-Compatible" content="IE=7" /&gt;<br />
Use IE7 standards mode.<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://llynix.com/code/ie8-compatibility-view/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Odd Programming Languages</title>
		<link>http://llynix.com/articles/odd-programming-languages/</link>
		<comments>http://llynix.com/articles/odd-programming-languages/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 17:00:34 +0000</pubDate>
		<dc:creator>Llynix</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Other]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://llynix.com/code/other/odd-programming-languages/</guid>
		<description><![CDATA[

Piet Program

A few weeks ago a ran across an old bookmark outlining the esoteric programing language Piet.  I spent a little time trying to grok this language along with a few others that I had run across.
Normally when any programmer decides to tackle a language such as this it isn&#8217;t for any purpose other [...]]]></description>
			<content:encoded><![CDATA[<div style='float:right;padding:10px;text-align:center;'>
<img src='http://llynix.com/wp-content/uploads/2008/07/piet-1.gif' alt='A Piet Program' /><br />
<span>Piet Program</span>
</div>
<p>A few weeks ago a ran across an old bookmark outlining the esoteric programing language <a href="http://www.dangermouse.net/esoteric/piet.html">Piet</a>.  I spent a little time trying to grok this language along with a few others that I had run across.</p>
<p>Normally when any programmer decides to tackle a language such as this it isn&#8217;t for any purpose other then education and perhaps masochism.  But I believe it helps in thinking outside the box and it provides a groundwork for greater programing topics such as <a href="http://esoteric.voxelperfect.net/wiki/Turing-complete">Turing completeness</a>.</p>
<p>I originally thought I&#8217;d write a simple list of odd programing languages I&#8217;ve found, but shortly into my search for languages I ran across a wiki about <a href="http://esoteric.voxelperfect.net/wiki/Main_Page">esoteric programming languages</a> which seemed to do the job for me.</p>
<p>But just for fun I&#8217;ll post a few languages I&#8217;ve run across in my time.</p>
<dl>
<dt><a href="http://en.wikipedia.org/wiki/Brainfuck">Brainfuck</a></dt>
<dd>A language created by Urban Müller so that the compiler can be made as small as possible.  He succeeded in writing a 240 byte compiler and others have since written some that are less then 200 bytes.  Brainfuck consists of eight commands notated by the symbols &lt;&gt;+-.,[] all other characters are ignored.</dd>
<dt><a href="http://www.catb.org/~esr/intercal/">INTERCAL</a></dt>
<dd>A tounge-in-cheak language which was designed to be as unlike any other programming language as possible.  From the manual: &#8216;The full name of the compiler is &#8220;Compiler Language With No Pronounceable  Acronym&#8221;,  which  is,  for  obvious  reasons, abbreviated &#8220;INTERCAL&#8221;.&#8217;  To give an example the &#8216;PLEASE&#8217; modifier in INTERCAL provides two paths of rejection.  If a programmer does not use the modifier often enough the program is considered rude and will not run.  If there are too many the program is considered too polite and will not run.</dd>
<dt><a href="http://www.dangermouse.net/esoteric/piet.html">Piet</a></dt>
<dd>A language whose programs are written to look like abstract art.  It utilizes eighteen colors which are cyclically related with the hue and lightness of the color, along with black and white which are used to control program flow.</dd>
<dt><a href="http://compsoc.dur.ac.uk/whitespace/">Whitespace</a></dt>
<dd>Whitespace is a language which uses only spaces, tabs and newlines as its commands unlike other languages who usually ignore these characters.</dd>
<dt><a href="http://en.wikipedia.org/wiki/Malbolge">Malbolge</a></dt>
<dd>A language designed to be as difficult as posible.  The name is from the eigth layer of hell in Dante&#8217;s Inferno.</dd>
<dt><a href="http://lolcode.com">LOLCODE</a></dt>
<dd>A new joke language which is actively being developed.</dd>
</dl>
]]></content:encoded>
			<wfw:commentRss>http://llynix.com/articles/odd-programming-languages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Caps Lock to Shift</title>
		<link>http://llynix.com/code/caps-lock-to-shift/</link>
		<comments>http://llynix.com/code/caps-lock-to-shift/#comments</comments>
		<pubDate>Sun, 17 Feb 2008 14:12:09 +0000</pubDate>
		<dc:creator>Llynix</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Other]]></category>

		<guid isPermaLink="false">http://llynix.com/code/other/caps-lock-to-shift/</guid>
		<description><![CDATA[Title says it all, found it on the net and it&#8217;s been invaluable on the laptop.  Posting it for posterity.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,2a,00,3a,00,00,00,00,00

]]></description>
			<content:encoded><![CDATA[<p>Title says it all, found it on the net and it&#8217;s been invaluable on the laptop.  Posting it for posterity.</p>
<pre>
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,2a,00,3a,00,00,00,00,00
</pre>
]]></content:encoded>
			<wfw:commentRss>http://llynix.com/code/caps-lock-to-shift/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript Countdown with PHP Fall Back</title>
		<link>http://llynix.com/code/javascript-countdown-with-php-fall-back/</link>
		<comments>http://llynix.com/code/javascript-countdown-with-php-fall-back/#comments</comments>
		<pubDate>Tue, 01 Jan 2008 04:22:34 +0000</pubDate>
		<dc:creator>Llynix</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[JS]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://llynix.com/code/js/javascript-countdown-with-php-fall-back/</guid>
		<description><![CDATA[Perhaps you have an event that happens on your webpage at a certain time, or you want to give a countdown to when the world ends.  With this piece of JavaScript you can do this, and a PHP fall back will allow non JavaScript enabled browsers to still view the relevant information.
Let&#8217;s start with [...]]]></description>
			<content:encoded><![CDATA[<p>Perhaps you have an event that happens on your webpage at a certain time, or you want to give a countdown to when the world ends.  With this piece of JavaScript you can do this, and a PHP fall back will allow non JavaScript enabled browsers to still view the relevant information.</p>
<p>Let&#8217;s start with the JavaScript.</p>
<p>One of the issues here is that we don&#8217;t just want to display how much time is left, but also update that display continuously to provide our timer.  There are two JavaScript functions that provide this ability,  setTimeout which evaluates a function after a set amount of time and setInterval which continuously evaluates a function after a set amount of time.</p>
<p>Another issue we have to look out for is timing and accuracy.  The examples on the internet all ran a small amount of code and then ran setTimeout after one second or very close to it.  The problem is we don&#8217;t know exactly how long the code we are running takes, and this could and is different on every computer that runs the code.</p>
<p>So we need a slightly better approach.  Here I&#8217;ve assigned a global variable which holds the time when the timer is complete and five times every second the display is updated to show the correct value.  Perhaps we can take this further and set our interval uniquely each time based on millisecond comparison of the time?  I leave that up to the reader.</p>
<p>The code is fairly self explanatory.  The bulk of it is formating code to make the countdown look pretty.  I&#8217;m not even sure it&#8217;s 100%.  Onward with the code.</p>
<pre>
var launchtime = '';

// converts seconds into seconds, minutes, hours, days
function sec_to(secs, num1, num2) {
  return ((Math.floor(secs/num1))%num2).toString();
}

// Our actual function, cb_id is the id of the element to modify
// secs is the time until the event, and message is displayed after time has elapsed.
function countback(cb_id, secs, message) {
  // Our time initialization
  timenow = Math.floor(new Date().getTime() / 1000);
  if(launchtime == '') launchtime = timenow + secs;
  secs = launchtime - timenow;

  // We set up our default text strings
  var DisplayStr = '';
  if(message == undefined) message = 'Times Up!';

  // if we are there display our message
  if (secs &lt; 0) {
    document.getElementById(cb_id).innerHTML = message;
    return;
  }

  // breakdown our time.
  var days = sec_to(secs,86400,100000);
  var hours = sec_to(secs,3600,24);
  var minutes = sec_to(secs,60,60);
  var seconds = sec_to(secs,1,60);

  // just some fancy code to make the time look pretty
  if (days &gt; 0) DisplayStr += days + ' Day';
  if (days &gt; 1) DisplayStr += 's';

  if (days &gt; 0) DisplayStr += ', ';
  if (hours &gt; 0) DisplayStr += hours + ' Hour';
  if (hours &gt; 1) DisplayStr += 's';

  if (hours &gt; 0) DisplayStr += ', ';
  if (minutes &gt; 0) DisplayStr += minutes + ' Minute';
  if (minutes &gt; 1) DisplayStr += 's';

  if (minutes &gt; 0) DisplayStr += ', ';
  if (seconds &gt; 0) DisplayStr += seconds + ' Second';
  if (seconds &gt; 1) DisplayStr += 's';

  // Display our pretty time string
  document.getElementById(cb_id).innerHTML = DisplayStr;

  // Run this function five times every second
  setTimeout("countback(\"countback\"," + (secs) + ",\"" + (message) + "\")", 200);
}
</pre>
<p>Now for the people who prefer a server side approach.  This function pretty much mirrors the JavaScript approach.  The one occlusion is the auto-updating feature of the client side script.  Our only real recourse here is a <a href="http://en.wikipedia.org/wiki/Meta_refresh">meta refresh</a> tag on the top of our page.  But at the very least it provides the proper time.</p>
<p>Here is the code:</p>
<pre>
&lt;?php 

function countback($seconds, $message = 'Times Up!') {
  $displaystr = '';
  if ($seconds &lt; 0) return($message);

  $days = (floor($seconds/86400))%100000;
  $hours = (floor($seconds/3600))%24;
  $minutes = (floor($seconds/60))%60;
  $seconds = $seconds%60;

  if ($days &gt; 0) $displaystr .= $days . ' Day';
  if ($days &gt; 1) $displaystr .= 's';

  if ($days &gt; 0) $displaystr .= ', ';
  if ($hours &gt; 0) $displaystr .= $hours . ' Hour';
  if ($hours &gt; 1) $displaystr .= 's';

  if ($hours &gt; 0) $displaystr .= ', ';
  if ($minutes &gt; 0) $displaystr .= $minutes . ' Minute';
  if ($minutes &gt; 1) $displaystr .= 's';

  if ($minutes &gt; 0) $displaystr .= ', ';
  if ($seconds &gt; 0) $displaystr .= $seconds . ' Second';
  if ($seconds &gt; 1) $displaystr .= 's';

  return($displaystr);
}

?&gt;
</pre>
<p>That&#8217;s it in a nutshell.  You can see a working example of it <a href="http://llynix.com/examples/timer/">here</a>.  I&#8217;d like to thank Robert Hashemain for his <a href="http://www.hashemian.com/tools/javascript-countdown.htm">javascript countdown</a> which served as the inspiration for mine.</p>
]]></content:encoded>
			<wfw:commentRss>http://llynix.com/code/javascript-countdown-with-php-fall-back/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Photo Slideshow &#8211; Part Two &#8211; Simple Slideshow</title>
		<link>http://llynix.com/code/photo-slideshow-part-2-simple-slideshow/</link>
		<comments>http://llynix.com/code/photo-slideshow-part-2-simple-slideshow/#comments</comments>
		<pubDate>Tue, 18 Sep 2007 04:18:06 +0000</pubDate>
		<dc:creator>Llynix</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://llynix.com/writings/photo-slideshow-part-2-simple-slideshow/</guid>
		<description><![CDATA[After I got my photos resized it was time to move on to a simple slideshow. It doesn&#8217;t get more simple then this.  I&#8217;ll go with the code first then provide a full explanation. 

&#60;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&#62;
&#60;html&#62;
&#60;head&#62;
  &#60;title&#62;Simple SlideShow&#60;/title&#62;
  &#60;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&#62;
&#60;/head&#62;
&#60;?php
  //the first thing we [...]]]></description>
			<content:encoded><![CDATA[<p>After I got my <a href="http://llynix.com/code/php/photo-gallery-part-one-batch-resize/">photos resized</a> it was time to move on to a simple slideshow. It doesn&#8217;t get more simple then this.  I&#8217;ll go with the code first then provide a full explanation. </p>
<pre>
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Simple SlideShow&lt;/title&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;/head&gt;
&lt;?php
  //the first thing we need to do is determine if we are on index or if we are on a picture
  $picture_num = (isset($_GET['pic'])) ? $_GET['pic']: 1;
  //now we'll also need to read the full directory
  $files = scandir('images/700w', 1);
  $total_num = count($files) - 2;

  $prev = ($picture_num == 1) ? $total_num : $picture_num - 1;
  $next = ($picture_num == $total_num) ? 1 : $picture_num + 1;
?&gt;

&lt;body style="margin:0 auto;text-align:center;"&gt;
    &lt;div class='navigation'&gt;
      &lt;a href="index.php?pic=&lt;?php echo $prev; ?&gt;"&gt;&lt; Previous&lt;/a&gt; |
      &lt;?php echo $picture_num . ' of ' . $total_num; ?&gt; |
      &lt;a href="index.php?pic=&lt;?php echo $next; ?&gt;"&gt;Next &gt;&lt;/a&gt;
    &lt;/div&gt;

    &lt;div id='photo'&gt;
      &lt;a href="images/originals/&lt;?php echo $files[$picture_num - 1]; ?&gt;"&gt;
        &lt;img src="images/700w/&lt;?php echo $files[$picture_num - 1]; ?&gt;" alt="Picture" style="border:none;"&gt;
      &lt;/a&gt;
    &lt;/div&gt;

    &lt;div class='navigation'&gt;
      &lt;a href="index.php?pic=&lt;?php echo $prev; ?&gt;"&gt;&lt; Previous&lt;/a&gt; |
      &lt;?php echo $picture_num . ' of ' . $total_num; ?&gt; |
      &lt;a href="index.php?pic=&lt;?php echo $next; ?&gt;"&gt;Next &gt;&lt;/a&gt;
    &lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>This simple slideshow uses a GET request to do it&#8217;s work.  This is going to dirty up our URL&#8217;s but it follows KISS.  If &#8216;pic&#8217; is not set it is set to 1.</p>
<p><a href="http://us3.php.net/manual/en/function.scandir.php">Scandir</a> reads into an array the files and directories from a directory.  I cheat here and read in the directory backwards to avoid numbering issues and to get rid of the . and .. problem.  Don&#8217;t put any directories in the originals or you&#8217;ll end up with a broken picture.</p>
<p>We subtract two from the total number of entries to get the number of pictures we have to show.  I then set my previous and next buttons.  This isn&#8217;t as simple as subtracting or adding one.  We need to cycle around if we are at the beginning or end.</p>
<p>With that information it&#8217;s a simple matter of putting it on the screen.  I have a small amount of CSS in there to center and remove a border.  There are a lot of features that could be added and a few quirks to iron out.  What do you guys think?</p>
]]></content:encoded>
			<wfw:commentRss>http://llynix.com/code/photo-slideshow-part-2-simple-slideshow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Photo Slideshow &#8211; Part One &#8211; Batch Resize</title>
		<link>http://llynix.com/code/photo-gallery-part-one-batch-resize/</link>
		<comments>http://llynix.com/code/photo-gallery-part-one-batch-resize/#comments</comments>
		<pubDate>Fri, 14 Sep 2007 05:14:30 +0000</pubDate>
		<dc:creator>Llynix</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Photos]]></category>

		<guid isPermaLink="false">http://llynix.com/code/php/photo-gallery-part-one-batch-resize/</guid>
		<description><![CDATA[The idea is simple, I want to be able to upload a directory full of pictures and have a slideshow.   I have a few groups of photos to share with the world and more and more I&#8217;m keen on the idea of coding it myself rather then use a service like flickr or [...]]]></description>
			<content:encoded><![CDATA[<p>The idea is simple, I want to be able to upload a directory full of pictures and have a slideshow.   I have a few groups of photos to share with the world and more and more I&#8217;m keen on the idea of coding it myself rather then use a service like <a href="http://flickr.com/">flickr</a> or <a href="http://picasa.google.com/">Picasa</a>.</p>
<p>The first step was to upload my photos.  I created a directory &#8216;images&#8217; and inside that directory I created &#8216;originals&#8217; and &#8216;700w&#8217;.  I then uploaded all my pictures to the originals directory.</p>
<p>I then created a script to automatically resize all the images in my directory.</p>
<pre>
&lt;?php
//it can take a bit to resize a bunch of images..
//php will by default only run for 30 seconds
//this removes that limitation
set_time_limit(0);

//open our directory
if ($handle = opendir('images/originals')) {
    //go through our directory filenames one by one
    while (false !== ($file = readdir($handle))) {
        //ignore our . and .. directories
        if ($file != "." &#038;&#038; $file != "..") {
            //open our image
            $image = imagecreatefromjpeg('images/originals/'."$file");
            if ($image === false) { closedir($handle); die ('Unable to open image'); }

            //get our original image dimensions
            $owidth = imagesx($image);
            $oheight = imagesy($image);

            //our new dimensions, hardcoded for a 700 width
            //second line adjusts the height to keep the same aspect ratio
            $nwidth = 700;
            $nheight =  $newheight=($oheight/$owidth)*$nwidth;

            //create a new blank image the right size
            $tmpimg=imagecreatetruecolor($nwidth,$nheight);
            //and resize/resample our original into it
            imagecopyresampled($tmpimg,$image,0,0,0,0,$nwidth,$nheight,$owidth,$oheight);

            //write to file
            $filename = "images/700w/". $file;
            imagejpeg($tmpimg,$filename);

            //clean up
            imagedestroy($image);
            imagedestroy($tmpimg);
        }
    }
    closedir($handle);
}
?&gt;
</pre>
<p>I saved this as &#8216;resize.php&#8217; and ran it with &#8216;php resize.php&#8217;.  After about a minute my 700w directory was full of resized images, and I&#8217;m all set up for the slideshow I want.</p>
]]></content:encoded>
			<wfw:commentRss>http://llynix.com/code/photo-gallery-part-one-batch-resize/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript Error Check for File Uploads</title>
		<link>http://llynix.com/code/javascript-error-check-for-file-uploads/</link>
		<comments>http://llynix.com/code/javascript-error-check-for-file-uploads/#comments</comments>
		<pubDate>Tue, 26 Jun 2007 21:11:49 +0000</pubDate>
		<dc:creator>Llynix</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[JS]]></category>

		<guid isPermaLink="false">http://llynix.com/code/js/javascript-error-check-for-file-uploads/</guid>
		<description><![CDATA[If your uploading a file of any decent size it will only annoy the user if they upload something wrong and get told afterwards.  What we&#8217;d like to do is scan that file and make sure it&#8217;s kosher before it&#8217;s even uploaded, but unfortunately that isn&#8217;t possible with Javascript due to built in security [...]]]></description>
			<content:encoded><![CDATA[<p>If your uploading a file of any decent size it will only annoy the user if they upload something wrong and get told afterwards.  What we&#8217;d like to do is scan that file and make sure it&#8217;s kosher before it&#8217;s even uploaded, but unfortunately that isn&#8217;t possible with Javascript due to built in security restrictions.</p>
<p>Really all we know about that file is it&#8217;s name before we receive it.  The only thing we can do is check the file extension to see if it is aligned with our wishes.  Included here is a very simple file upload form with minimal PHP and the proper Javascript to complete this check.</p>
<p>Here is the HTML/Javascript </p>
<pre>
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Jpeg Upload&lt;/title&gt;
  &lt;meta http-equiv="content-type" content="text/html; charset=UTF-8" /&gt;
  &lt;script type="text/javascript" language="JavaScript"&gt;&lt;!--
    function check_extension(id) {
      var filename = document.getElementById(id).value;
      var fileext = filename.substring(filename.lastIndexOf('.')+1).toLowerCase();
      var extension = new Array('jpg','jpeg');

       for(var i = 0; i &lt; extension.length; i++) {
         if(fileext == extension[i]) { return true; }
       }
       alert("Your upload form contains an unapproved file name.");
       return false;
    }
  //--&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;

  &lt;form enctype="multipart/form-data" method="post" action="index.php" onsubmit="return check_extension('image_file');"&gt;
    &lt;label for="image_file"&gt;Jpeg Image to upload: &lt;input type="file" name="image_file" id="image_file" /&gt;&lt;/label&gt;
    &lt;input type="submit" name="submit" value="upload" /&gt;
  &lt;/form&gt;

&lt;body&gt;
&lt;html&gt;
</pre>
<p>It should be fairly straightforward.  In the form remember to set the onsubmit=&#8221;return check_extension(&#8216;image_file&#8217;);&#8221; where image_file is the name of your file input.  You can modify extensions in the Javascript to your preferences.</p>
<p>For completeness here is a <a href="http://llynix.com/examples/jsextupload/">working example</a> and the <a href="http://llynix.com/examples/jsextupload/index.phps">source</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://llynix.com/code/javascript-error-check-for-file-uploads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
