<?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>Ink Grass &#187; Programming</title>
	<atom:link href="http://inkgrass.lazymoon.org/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://inkgrass.lazymoon.org</link>
	<description>A blog about web design, programming, graphics and everything inbetween.</description>
	<lastBuildDate>Mon, 18 Jan 2010 14:13:46 +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>Using GET data with PHP include()</title>
		<link>http://inkgrass.lazymoon.org/2007/11/12/using-get-data-with-php-include/</link>
		<comments>http://inkgrass.lazymoon.org/2007/11/12/using-get-data-with-php-include/#comments</comments>
		<pubDate>Mon, 12 Nov 2007 13:13:00 +0000</pubDate>
		<dc:creator>Lachlan</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://inkgrass.lazymoon.org/2007/11/12/using-get-data-with-php-include/</guid>
		<description><![CDATA[Earlier today I thought I&#8217;d edit the title-page wallpaper script, and make it print friendly error messages. Previously it would just die in a very unhelpful way when an error was encountered. It didn&#8217;t occur to me before-hand, but my custom error handling script was very antiquated. For one, it had to be called with [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier today I thought I&#8217;d edit the title-page wallpaper script, and make it print friendly error messages. Previously it would just die in a very unhelpful way when an error was encountered. It didn&#8217;t occur to me before-hand, but my custom error handling script was very antiquated. For one, it had to be called with GET information.</p>
<p>This works well for <em>.htaccess</em> redirects and error documents (which was why I designed it this way a year or so ago), but it is a bad way of calling the error functions from within a running script. For one, PHP won&#8217;t allow you to pass GET values in the filename:</p>
<pre>include('error.php?code=404'); // will not work</pre>
<p>Since I didn&#8217;t have the time to overhaul the entire script, I checked for an easy work around. PHP recommends you use URL wrapping instead.</p>
<pre>include('http://www.lazymoon.org/error.php?code=404');</pre>
<p>But I didn&#8217;t like this idea, because if my host disables URL wrapping I&#8217;ll be left with broken scripts. Not to mention it takes longer to process.</p>
<p>The easy solution, for those who are interested, is just to set the GET information manually before including the file.</p>
<pre>$_GET['code'] = 404;
include('error.php);</pre>
<p>This does the job nicely.</p>
]]></content:encoded>
			<wfw:commentRss>http://inkgrass.lazymoon.org/2007/11/12/using-get-data-with-php-include/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>GD Library Garbage Collection</title>
		<link>http://inkgrass.lazymoon.org/2007/11/08/gd-library-garbage-collection/</link>
		<comments>http://inkgrass.lazymoon.org/2007/11/08/gd-library-garbage-collection/#comments</comments>
		<pubDate>Thu, 08 Nov 2007 05:53:15 +0000</pubDate>
		<dc:creator>Lachlan</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://inkgrass.lazymoon.org/2007/11/08/gd-library-garbage-collection/</guid>
		<description><![CDATA[If you have visited the LazyMoon title page (with a recent browser and JavaScript active), you would have noticed the ability to dynamically create a wallpaper form the now symbolic silhouette design. One thing very few people notice is that the wallpapers are automatically sized to match your screen resolution. Meaning I haven&#8217;t just designed [...]]]></description>
			<content:encoded><![CDATA[<p>If you have visited the LazyMoon title page (with a recent browser and JavaScript active), you would have noticed the ability to dynamically create a wallpaper form the now symbolic silhouette design. One thing very few people notice is that the wallpapers are automatically sized to match your screen resolution. Meaning I haven&#8217;t just designed every possible combination, but programmed a script to create your wallpaper when you request it.</p>
<p>This consumes an unholy amount of memory. To the extent where I suspect my host may get a little upset if too many people request a wallpaper at once.</p>
<p>What is concerning is that PHP doesn&#8217;t automatically garbage collect GD resources stored in memory. If someone cancels their request or the script terminates early, that resource will not be destroyed. This only has to happen a handful of times before the memory is filled and I get a unhappy letter with some very large numbers.</p>
<p>If you use the GD library and don&#8217;t have a garbage collection function, you are playing dangerously. To get around this, we can write a function and use PHP&#8217;s <code><a href="http://au2.php.net/manual/en/function.register-shutdown-function.php">register_shutdown_function</a></code> to manually free un-destroyed resources.</p>
<p>The code snippet I use is:</p>
<pre>function gd_garbage() {
    global $img;
    if (isset($img)) {
        foreach ($img as $i) {
            if (get_resource_type($i) == 'gd') {
                imagedestroy($i);
            }
        }
        unset($img);
    }
}

register_shutdown_function("gd_garbage");</pre>
<p>For this to work, we have to store all image resources in an array named <var>$img</var>. However, we shouldn&#8217;t rely on garbage collection alone to clean up GD resources, for two very important reasons:</p>
<ol>
<li>The function may fail,</li>
<li>Leaving GD images open until the script terminates is a considerable waste of memory.</li>
</ol>
<p>It is always recommend that you destroy your images the moment you are done with them, and let the garbage collection handle any mishaps along the way.</p>
]]></content:encoded>
			<wfw:commentRss>http://inkgrass.lazymoon.org/2007/11/08/gd-library-garbage-collection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rounding To Grouping</title>
		<link>http://inkgrass.lazymoon.org/2007/11/03/rounding-to-grouping/</link>
		<comments>http://inkgrass.lazymoon.org/2007/11/03/rounding-to-grouping/#comments</comments>
		<pubDate>Sat, 03 Nov 2007 02:53:34 +0000</pubDate>
		<dc:creator>Lachlan</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://inkgrass.lazymoon.org/2007/11/03/rounding-to-integers/</guid>
		<description><![CDATA[There may come a time when you have to round a number up or down to certain multiples of another number. Since us Australians haven&#8217;t had one-cent pieces in quite some time, most purchases are rounded up or down to multiples of five cents.
Enough of my side-story. Here&#8217;s some simple code to get the job [...]]]></description>
			<content:encoded><![CDATA[<p>There may come a time when you have to round a number up or down to certain multiples of another number. Since us Australians haven&#8217;t had one-cent pieces in quite some time, most purchases are rounded up or down to multiples of five cents.</p>
<p>Enough of my side-story. Here&#8217;s some simple code to get the job done:</p>
<p><strong>Java:</strong></p>
<pre>import java.lang.Math;

public float roundToGrouping(float number, float grouping) {
    return Math.round(number/grouping)*grouping;
}</pre>
<p><strong>PHP:</strong></p>
<pre>function roundToGrouping($number, $grouping) {
    return round($number/$grouping)*$grouping;
}</pre>
<p><strong>JavaScript:</strong></p>
<pre>function roundToGrouping(number, grouping) {
    return Math.round(number/grouping)*grouping;
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://inkgrass.lazymoon.org/2007/11/03/rounding-to-grouping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
