<?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>Just Relax Now</title>
	<atom:link href="http://blog.relaxnow.nl/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.relaxnow.nl</link>
	<description>Software engineering and life...</description>
	<lastBuildDate>Mon, 21 Mar 2011 21:14:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Sorting wallpapers by ratio with Euclid&#8217;s algorithm and PHP</title>
		<link>http://blog.relaxnow.nl/2011/03/21/sorting-wallpapers-by-ratio-with-euclids-algorithm-and-php/</link>
		<comments>http://blog.relaxnow.nl/2011/03/21/sorting-wallpapers-by-ratio-with-euclids-algorithm-and-php/#comments</comments>
		<pubDate>Mon, 21 Mar 2011 19:42:50 +0000</pubDate>
		<dc:creator>boy</dc:creator>
				<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.relaxnow.nl/?p=143</guid>
		<description><![CDATA[I have a confession to make&#8230; I am a sucker for cool desktop wallpapers. Right now on my MacBook Pro is a brilliant Fight Club background (made from the art of Adam from 52 Bad Dudes) and for my second monitor the funny Office Space / XP crossover wallpaper, popular on reddit right now. I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>I have a confession to make&#8230; I am a sucker for cool desktop wallpapers.</p>
<p>Right now on my MacBook Pro is a brilliant <a href="http://www.meh.ro/wp-content/uploads/2010/12/meh.ro6127.png" target="_blank">Fight Club background</a> (made from the art of Adam from <a href="http://www.theskidshop.bigcartel.com/">52 Bad Dudes</a>) and for my second monitor <a href="http://www.flickr.com/photos/burtgummer/4266149702/">the funny Office Space / XP crossover wallpaper</a>, popular on <a href="http://reddit.com/r/wallpaper">reddit</a> right now.</p>
<p>I&#8217;m very much enjoying these wallpapers for now, but chances are that within the month I&#8217;ll get bored with them and find another to take their place.</p>
<p>I&#8217;ve had this addiction for several years now and as a good geek, instead of ever actually cleaning up, I&#8217;ve just migrated crap from hard disk to bigger hard disk. So in the process I&#8217;ve gathered quite a few wallpapers.</p>
<p>This used to work beautifully when I only had desktop machines, but when I switched to using laptops for work and (accidentally) ordered a widescreen monitor for home, my pretty 4:3 wallpapers didn&#8217;t look so hot anymore when they were scaled out to 16:9.</p>
<p>So I started collecting 16:9 wallpapers&#8230; and dropping them in the same folder as my old 4:3 wallpapers.</p>
<p>And now I have a mess of wallpapers that I have to sift through to find one that matches the ratio I want whenever I want to dig up an old wallpaper (like this cool <a href="http://wallpapergravity.com/wallpapers1/107/107043.jpg">&#8220;Why are you wearing that stupid man suit?&#8221; wallpaper</a> from <a href="http://www.imdb.com/title/tt0246578/">Donny Darko</a>).</p>
<p>Now if I was a normal person I could:</p>
<ol>
<li>Live with it</li>
<li>Find some small utility to sort it for me</li>
<li>Sort everything by hand</li>
</ol>
<p>But being a PHP programmer I had to write a simple script.</p>
<h2>Problem</h2>
<p>Given a single folder of images, sort them according to ratio where any ratio with less than X, a predetermined significant amount of, images get lumped together in a single &#8216;other&#8217; category.</p>
<h2>Solution</h2>
<p>It&#8217;s pretty easy to get a list of images in a directory and with <a href="http://php.net/gd">GD</a> it&#8217;s pretty easy to get the image dimensions, however what I did not know was how to get the ratio. I tried to think how I would do this back in school with fractions, determining that 6/8th is the same as 3/4th but basically I&#8217;d just been thought to work on trial and error&#8230; something that I didn&#8217;t really feel like programming. There has to be an algorithm for this!</p>
<p>So after a little googling I&#8217;d determined I needed the &#8216;greatest common divisor&#8217; and as usual there were snippets out there that did this for me, hell I could even use <a href="http://www.wolframalpha.com/input/?i=greatest+common+divisor+1280+x+1024">WolframAlpha</a> to do it for me (although that didn&#8217;t seem to give me a usable answer&#8230; I was probably asking it wrong?).</p>
<p>Unfortunately the first PHP snippet I came across was so obfuscated (really people, a function name ghd? I won&#8217;t even grace it with the minute amount of PageRank I have) that I figured it would be more fun to write this myself, fortunately <a href="http://www.wikihow.com/Find-the-Greatest-Common-Divisor-of-Two-Integers">WikiHow has an awesome article on this algorithm</a> called &#8216;<a href="http://en.wikipedia.org/wiki/Euclidean_algorithm">Euclid&#8217;s algorithm</a>&#8216; (and as usual Wikipedia has a boatload of information on it).</p>
<h2>End result</h2>
<pre>&lt;?php

define("MINIMUM_NUMBER_OF_IMAGES_FOR_VALID_RATIO", 10);
define("WEIRD_RATIO_FOLDERNAME", 'other');

/**
 * @var array All files in the current directory
 */
$files = glob('*.*');

$imagesByRatio = array();
foreach ($files as $image) {
 $imageData = getimagesize($image);
 if ($imageData === false) {
 echo "GD thinks $image is not a valid image." . PHP_EOL;
 continue;
 }
 $imageWidth = $imageData[0];
 $imageHeight = $imageData[1];

 $divisor = getGreatestCommonDivisor($imageWidth, $imageHeight);
 $ratioWidth = $imageWidth / $divisor;
 $ratioHeight = $imageHeight / $divisor;
 $ratioDescription = "$ratioWidth:$ratioHeight";

 $imagesByRatio[$ratioDescription][] = $image;
 echo "'$image' is a $ratioDescription image" . PHP_EOL;
}

foreach ($imagesByRatio as $ratio=&gt;$images) {
 // If there are enough images for this ratio, then it gets it's own folder
 if (count($images) &gt;= MINIMUM_NUMBER_OF_IMAGES_FOR_VALID_RATIO) {
 // Replace colon by x for better compatibility with FAT filesystems
 $dirName = str_replace(':','x', $ratio);
 }
 else {
 // Else it gets to be in the 'weird-ratios folder'
 $dirName = WEIRD_RATIO_FOLDERNAME;
 }

 // Create the directory if it doesn't exist already
 if (!file_exists($dirName)) {
 echo "Creating $dirName" . PHP_EOL;
 mkdir($dirName);
 }

 // Copy over all the images with this ratio
 foreach ($images as $image) {
 echo "Copying '$image' to '$dirName/$image'" . PHP_EOL;
 copy($image, "$dirName/$image");
 }
}

/**
 * Get the Greatest Common Divisor for 2 numbers
 *
 * @example getGreatestCommonDivisor(6,8) -&gt; 2
 * @link http://www.wikihow.com/Find-the-Greatest-Common-Divisor-of-Two-Integers
 *
 * @throws Exception
 * @param int $a First number
 * @param int $b Second number
 * @return int Greatest common divisor
 */
function getGreatestCommonDivisor($a, $b)
{
 assert('is_int($a) &amp;&amp; is_int($b)');
 $a = (int)abs($a);
 $b = (int)abs($b);
 if ($a === 0 OR $b === 0) {
 throw new Exception("Cannot find greatest common divisor of 0");
 }

 if ($a &gt; $b) {
 $dividend = $a;
 $divisor = $b;
 }
 else {
 $dividend = $a;
 $divisor = $b;
 }

 //$quotient = (int)($dividend / $divisor);
 $remainder = $dividend % $divisor;

 if ($remainder &gt; 0) {
 return getGreatestCommonDivisor($divisor, $remainder);
 }
 else {
 return $divisor;
 }
}
</pre>
<h2>Conclusion</h2>
<p>Math is fun!</p>
<p>Also, I have a lot of 5:4 and 8:5 wallpapers&#8230; weird ratio :-S.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.relaxnow.nl/2011/03/21/sorting-wallpapers-by-ratio-with-euclids-algorithm-and-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Welcome!</title>
		<link>http://blog.relaxnow.nl/2009/08/13/welcome/</link>
		<comments>http://blog.relaxnow.nl/2009/08/13/welcome/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 20:46:12 +0000</pubDate>
		<dc:creator>boy</dc:creator>
				<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://blog.relaxnow.nl/?p=83</guid>
		<description><![CDATA[Welcome to my (rebooted) blog. Here I will be posting about software development and life in general. What&#8217;s changed? I removed a semi-draft post I had on here (sorry commenters! I&#8217;ll see if I can polish it up and put it back on). I updated to the latest greatest of WordPress I picked one of [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to my (rebooted) blog.</p>
<p>Here I will be posting about software development and life in general.</p>
<h2>What&#8217;s changed?</h2>
<ul>
<li>I removed a semi-draft post I had on here (sorry commenters! I&#8217;ll see if I can polish it up and put it back on).</li>
<li>I updated to the latest greatest of WordPress</li>
<li>I picked one of the cool free themes of WordPress (SimpleX)</li>
<li>I turned on Akismet and WP-Twitter.</li>
<li>Then I proceeded to hack the javascript and stylesheets until wp-twitter and SimpleX played nice together (really, wp-twitter using @\S as a regex to check for usernames?).</li>
<li>Then hacked <a href="http://www.relaxnow.nl">http://www.relaxnow.nl</a> and <a href="http://www.boybaukema.nl">http://www.boybaukema.nl</a> to redirect with a 301 to <a href="http://blog.relaxnow.nl">http://blog.relaxnow.nl</a>.</li>
</ul>
<h2>What&#8217;s to come?</h2>
<p>Hopefully (not making any promises):</p>
<ul>
<li>Posts on aspects of software development with PHP</li>
<li>Posts on software development in general</li>
<li>General posts on technology and life stuff.</li>
</ul>
<p>Just click on the orange RSS icon in the top right corner and add the feed to<a href="http://reader.google.com"> your feed reader</a> and you should be (pleasantly) surprised soon.</p>
<h2>When?</h2>
<p>Jeez man&#8230; stop asking all these hard questions&#8230; we&#8217;ll see. It&#8217;s not called Relax Now for nothing <img src='http://blog.relaxnow.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.relaxnow.nl/2009/08/13/welcome/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

